Jump to content

EKG sequence convergence: Difference between revisions

m
→‎{{header|Haskell}}: Applied Ormolu, tidied.
(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 - 1)
( head
(head (filter (\i -> notElem i l && (gcd h i > 1)) [2 ..]) : l)
( 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 (" >> (putStr . show $ x) >> putStr ") is " >>
print >> (seqEKGputStr x. 20)show $ x)
[2, 5, 7, 9, 10] >> putStr ") is "
>> print (seqEKG x 20)
putStr "EKG(5) and EKG(7) converge at " >>
print )
((+[2, 1)5, $7, 9, 10]
>> putStr "EKG(5) and EKG(7) converge at " >>
fromJust $
>> findIndexprint
(isPrefixOf (replicate 20succ True))$
fromJust $
(tails (zipWith (==) (seqEKG 7 80) (seqEKG 5 80))))</lang>
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]
9,655

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.