Consecutive primes with ascending or descending differences: Difference between revisions
Consecutive primes with ascending or descending differences (view source)
Revision as of 09:53, 2 November 2021
, 2 years ago→{{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
| y `equiv` h = go (h : y :
| 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
-- the task implementation
task ord n = reverse $ p + s : p : (fst <$> rest)
where
(p, s) : rest =
maximumBy length $
consecutives (\(_, a) (_, b) -> a
takeWhile (< n) primes
differences l = zip l $ zipWith (-) (tail l) l</lang>
|