Consecutive primes with ascending or descending differences: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: added solution) |
m (→{{header|Haskell}}: Removed a typo (trailing W prevented compilation after `primes`) added explicit module import) |
||
Line 478: | Line 478: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Uses prime generator implemented in the [[Sieve_of_Eratosthenes#Improved_efficiency_Wheels]]. |
Uses prime generator implemented in the [[Sieve_of_Eratosthenes#Improved_efficiency_Wheels]]. |
||
<lang haskell>import Data.Numbers.Primes |
|||
⚫ | |||
⚫ | |||
consecutives equiv = filter ((> 1) . length) . go [] |
consecutives equiv = filter ((> 1) . length) . go [] |
||
where |
where |
||
Line 485: | Line 487: | ||
go (y:ys) (h:t) | y `equiv` h = go (h:y:ys) t |
go (y:ys) (h:t) | y `equiv` h = go (h:y:ys) t |
||
| otherwise = (y:ys) : go [h] t |
| otherwise = (y:ys) : go [h] t |
||
-- finds maximal values in a list and returns the first one |
-- finds maximal values in a list and returns the first one |
||
maximumBy g (h:t) = foldr f h t |
maximumBy g (h:t) = foldr f h t |
||
where f r x = if g r < g x then x else r |
where f r x = if g r < g x then x else r |
||
-- the task implementation |
-- the task implementation |
||
task ord n = reverse $ p+s : p : (fst <$> rest) |
task ord n = reverse $ p+s : p : (fst <$> rest) |
||
Line 496: | Line 498: | ||
$ consecutives (\(_,a) (_,b) -> a `ord` b) |
$ consecutives (\(_,a) (_,b) -> a `ord` b) |
||
$ differences |
$ differences |
||
$ takeWhile (< n) |
$ takeWhile (< n) primes |
||
differences l = zip l $ zipWith (-) (tail l) l</lang> |
differences l = zip l $ zipWith (-) (tail l) l</lang> |
||