Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,32 @@
{-# LANGUAGE NumericUnderscores #-}
import Data.Numbers.Primes (primes)
type Result = [(String, [Int])]
oneMillionPrimes :: Integral p => [p]
oneMillionPrimes = takeWhile (<1_000_000) primes
getGroups :: [Int] -> Result
getGroups [] = []
getGroups ps@(n:x:y:z:xs)
| x-n == 6 && y-x == 4 && z-y == 2 = ("(6 4 2)", [n, x, y, z]) : getGroups (tail ps)
| x-n == 4 && y-x == 2 = ("(4 2)", [n, x, y]) : getGroups (tail ps)
| x-n == 2 && y-x == 4 = ("(2 4)", [n, x, y]) : ("2", [n, x]) : getGroups (tail ps)
| x-n == 2 && y-x == 2 = ("(2 2)", [n, x, y]) : ("2", [n, x]) : getGroups (tail ps)
| x-n == 2 = ("2", [n, x]) : getGroups (tail ps)
| x-n == 1 = ("1", [n, x]) : getGroups (tail ps)
| otherwise = getGroups (tail ps)
getGroups (x:xs) = getGroups xs
groups :: Result
groups = getGroups oneMillionPrimes
showGroup :: String -> IO ()
showGroup group = do
putStrLn $ "Differences of " ++ group ++ ": " ++ show (length r)
putStrLn $ "First: " ++ show (head r) ++ "\nLast: " ++ show (last r) ++ "\n"
where r = foldr (\(a, b) c -> if a == group then b : c else c) [] groups
main :: IO ()
main = showGroup "2" >> showGroup "1" >> showGroup "(2 2)" >> showGroup "(2 4)" >> showGroup "(4 2)"
>> showGroup "(6 4 2)"

View file

@ -0,0 +1,35 @@
{-# LANGUAGE NumericUnderscores #-}
import Data.Numbers.Primes (primes)
-- Type alias dictionary. Key is the difference group and value is a successive prime group.
type Result = [(String, [Int])]
findPrimes :: [Int] -> [[Int]] -> Result
findPrimes [] _ = []
findPrimes primes diffs = loopDiffs diffs <> findPrimes (tail primes) diffs
where
loopDiffs ds = [(show d, successive)
| d <- ds,
let successive = take (length d + 1) primes,
subs successive == d]
subs = map (uncurry (-)) . init . tail . (\xs -> zip (xs <> [0]) (0 : xs))
showGroup :: Result -> String -> IO ()
showGroup result diffs = do
putStrLn $ "Differences of " ++ diffs ++ ": " ++ show (length groups)
putStrLn
$ "First: "
++ firstGroup groups
++ "\nLast: "
++ lastGroup groups
++ "\n"
where
groups = [b | (a, b) <- result, a == diffs]
firstGroup = show . head
lastGroup = show . last
main :: IO ()
main = mapM_ (showGroup result . show) diffs
where
(diffs, result) = groups [[2], [1], [2, 2], [2, 4], [4, 2], [6, 4, 2]]
groups diffs = (diffs, findPrimes (takeWhile (< 1_000_000) primes) diffs)