Consecutive primes with ascending or descending differences: Difference between revisions

→‎{{header|Haskell}}: added solution
(→‎{{header|Haskell}}: added solution)
Line 475:
752207 (44) 752251 (12) 752263 (10) 752273 (8) 752281 (6) 752287 (4) 752291 (2) 752293
</pre>
 
=={{header|Haskell}}==
Uses prime generator implemented in the [[Sieve_of_Eratosthenes#Improved_efficiency_Wheels]].
<lang haskell>-- generates consecutive subsequences defined by given equivalence relation
consecutives equiv = filter ((> 1) . length) . go []
where
go r [] = [r]
go [] (h:t) = go [h] t
go (y:ys) (h:t) | y `equiv` h = go (h:y:ys) 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 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
$ consecutives (\(_,a) (_,b) -> a `ord` b)
$ differences
$ takeWhile (< n) primesW
differences l = zip l $ zipWith (-) (tail l) l</lang>
 
<pre>λ> reverse <$> consecutives (<) [1,0,1,2,3,4,3,2,3,4,5,6,7,6,5,4,3,4,5]
[[0,1,2,3,4],[2,3,4,5,6,7],[3,4,5]]
 
λ> task (<) 1000000
[128981,128983,128987,128993,129001,129011,129023,129037]
 
λ> task (>) 1000000
[322171,322193,322213,322229,322237,322243,322247,322249]</pre>
 
=={{header|Julia}}==
Anonymous user