Consecutive primes with ascending or descending differences: Difference between revisions

m
→‎{{header|Haskell}}: Specified import expression
m (→‎{{header|Haskell}}: Removed a typo (trailing W prevented compilation after `primes`) added explicit module import)
m (→‎{{header|Haskell}}: Specified import expression)
Line 478:
=={{header|Haskell}}==
Uses prime generator implemented in the [[Sieve_of_Eratosthenes#Improved_efficiency_Wheels]].
<lang haskell>import Data.Numbers.Primes (primes)
 
-- generates consecutive subsequences defined by given equivalence relation
Line 484:
where
go r [] = [r]
go [] (h : t) = go [h] t
go (y : ys) (h:t) | y `equiv` h = go (h:y:ys) t)
| y `equiv` h = go (h : y : | otherwise = (y:ys) : go [h] t
| otherwise = (y : ys) : go [h] t
 
-- finds maximal values in a list and returns the first one
maximumBy g (h : t) = foldr f h t
where
where f r x = if g r < g x then x else r
 
-- the task implementation
task ord n = reverse $ p + s : p : (fst <$> rest)
where
(p, s) : rest = maximumBy length
maximumBy length $
$ consecutives (\(_,a) (_,b) -> a `ord` b)
consecutives (\(_, a) (_, b) -> a $`ord` differencesb) $
differences $ takeWhile (< n) primes
takeWhile (< n) primes
differences l = zip l $ zipWith (-) (tail l) l</lang>
 
9,657

edits