EKG sequence convergence: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Suggested edit (OP may prefer to revert): applied hlint, hindent, specified imports, signatures)
Line 254: Line 254:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang Haskell>
<lang Haskell>import Data.List (findIndex, isPrefixOf, tails)
import Data.List
import Data.Maybe (fromJust)
import Data.Maybe


seqEKGRec :: Int -> Int -> [Int] -> [Int]
seqEKG_rec _ 0 l = l
seqEKGRec _ 0 l = l
seqEKG_rec k n [] = seqEKG_rec k (n - 2) [k, 1]
seqEKGRec k n [] = seqEKGRec k (n - 2) [k, 1]
seqEKG_rec k n l@(h:t) =
seqEKGRec k n l@(h:t) =
seqEKG_rec k (n - 1)
seqEKGRec
([head (filter (\i -> (notElem i l) && ((gcd h i) > 1)) [2..])] ++ l)
k
(n - 1)
(head (filter (\i -> notElem i l && (gcd h i > 1)) [2 ..]) : l)


seqEKG k n = reverse (seqEKG_rec k n [])
seqEKG :: Int -> Int -> [Int]
seqEKG k n = reverse (seqEKGRec k n [])


main :: IO ()
main = mapM_ (\x -> (putStr "EKG (" >> (putStr . show $ x) >> putStr ") is " >> print (seqEKG x 20)))
main =
mapM_
(\x ->
putStr "EKG (" >> (putStr . show $ x) >> putStr ") is " >>
print (seqEKG x 20))
[2, 5, 7, 9, 10] >>
[2, 5, 7, 9, 10] >>
putStr "EKG(5) and EKG(7) converge at " >>
putStr "EKG(5) and EKG(7) converge at " >>
print
print ((+1) $ fromJust $ findIndex (isPrefixOf (take 20 (repeat True)))
((+ 1) $
(tails (zipWith (\x y -> (x==y)) (seqEKG 7 80) (seqEKG 5 80))))
fromJust $
</lang>
findIndex
(isPrefixOf (replicate 20 True))
(tails (zipWith (==) (seqEKG 7 80) (seqEKG 5 80))))</lang>
{{out}}
{{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]
<pre>
EKG (2) is [1,2,4,6,3,9,12,8,10,5,15,18,14,7,21,24,16,20,22,11]
EKG (5) is [1,5,10,2,4,6,3,9,12,8,14,7,21,15,18,16,20,22,11,33]
EKG (5) is [1,5,10,2,4,6,3,9,12,8,14,7,21,15,18,16,20,22,11,33]
EKG (7) is [1,7,14,2,4,6,3,9,12,8,10,5,15,18,16,20,22,11,33,21]
EKG (7) is [1,7,14,2,4,6,3,9,12,8,10,5,15,18,16,20,22,11,33,21]
EKG (9) is [1,9,3,6,2,4,8,10,5,15,12,14,7,21,18,16,20,22,11,33]
EKG (9) is [1,9,3,6,2,4,8,10,5,15,12,14,7,21,18,16,20,22,11,33]
EKG (10) is [1,10,2,4,6,3,9,12,8,14,7,21,15,5,20,16,18,22,11,33]
EKG (10) is [1,10,2,4,6,3,9,12,8,14,7,21,15,5,20,16,18,22,11,33]
EKG(5) and EKG(7) converge at 21
EKG(5) and EKG(7) converge at 21</pre>
</pre>


=={{header|J}}==
=={{header|J}}==