EKG sequence convergence: Difference between revisions
m
→{{header|Haskell}}: Applied Ormolu, tidied.
Alextretyak (talk | contribs) (Added 11l) |
m (→{{header|Haskell}}: Applied Ormolu, tidied.) |
||
Line 340:
<lang Haskell>import Data.List (findIndex, isPrefixOf, tails)
import Data.Maybe (fromJust)
----------------------- EKG SEQUENCE ---------------------
seqEKGRec :: Int -> Int -> [Int] -> [Int]
seqEKGRec _ 0 l = l
seqEKGRec k n [] = seqEKGRec k (n - 2) [k, 1]
seqEKGRec k n l@(h : t) =
seqEKGRec
k
(pred n
( head
( filter
(((&&) . flip notElem l) <*> ((1 <) . gcd h))
[2 ..]
) :
l
)
seqEKG :: Int -> Int -> [Int]
seqEKG k n = reverse (seqEKGRec k n [])
--------------------- CONVERGENCE TEST -------------------
main :: IO ()
main =
mapM_
( \x ->
putStr "EKG ("
>> print (seqEKG x 20)
putStr "EKG(5) and EKG(7) converge at " >>▼
fromJust $▼
>>
▲ fromJust $
findIndex
(isPrefixOf (replicate 20 True))
( tails
( zipWith
(==)
(seqEKG 7 80)
(seqEKG 5 80)
)
)
)</lang>
{{out}}
<pre>EKG (2) is [1,2,4,6,3,9,12,8,10,5,15,18,14,7,21,24,16,20,22,11]
|