Successive prime differences: Difference between revisions

Content added Content deleted
Line 640: Line 640:


findPrimes :: [Int] -> [[Int]] -> Result
findPrimes :: [Int] -> [[Int]] -> Result
findPrimes [] _ = []
findPrimes [] _ = []
findPrimes primes diffs = loopDiffs diffs <> findPrimes (tail primes) diffs
findPrimes primes diffs = loopDiffs diffs <> findPrimes (tail primes) diffs
where
where loopDiffs [] = []
loopDiffs [] = []
loopDiffs (d:ds)
loopDiffs (d : ds)
| subs successive == d = (show d, successive) : loopDiffs ds
| otherwise = loopDiffs ds
| subs successive == d = (show d, successive) : loopDiffs ds
where successive = take (length d + 1) primes
| otherwise = loopDiffs ds
where
subs = map (uncurry (-)) . init . tail . (\xs -> zip (xs <> [0]) (0 : xs))
successive = take (length d + 1) primes
subs = map (uncurry (-)) . init . tail . (\xs -> zip (xs <> [0]) (0 : xs))


showGroup :: Result -> String -> IO ()
showGroup :: Result -> String -> IO ()
showGroup result diffs = do
showGroup result diffs = do
putStrLn $ "Differences of " ++ diffs ++ ": " ++ show (length groups)
putStrLn $ "Differences of " ++ diffs ++ ": " ++ show (length groups)
putStrLn
putStrLn $ "First: " ++ firstGroup groups ++ "\nLast: " ++ lastGroup groups ++ "\n"
$ "First: "
where groups = foldr (\(a, b) c -> if a == diffs then b : c else c) [] result
firstGroup = show . head
++ firstGroup groups
++ "\nLast: "
lastGroup = show . last
++ lastGroup groups
++ "\n"
where
groups = foldr (\(a, b) c -> if a == diffs then b : c else c) [] result
firstGroup = show . head
lastGroup = show . last


main :: IO ()
main :: IO ()
main = mapM_ (showGroup result . show) diffs
main = mapM_ (showGroup result . show) diffs
where
where (diffs, result) = groups [[2], [1], [2,2], [2,4], [4,2], [6,4,2]]
(diffs, result) = groups [[2], [1], [2, 2], [2, 4], [4, 2], [6, 4, 2]]
groups diffs = (diffs, findPrimes (takeWhile (<1_000_000) primes) diffs)</lang>
groups diffs = (diffs, findPrimes (takeWhile (< 1_000_000) primes) diffs)</lang>
{{out}}
{{out}}
<pre>
<pre>