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. |
import Data.Maybe (fromJust) |
||
import Data.Maybe |
|||
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 |
|||
⚫ | |||
⚫ | |||
seqEKG |
seqEKG :: Int -> Int -> [Int] |
||
seqEKG k n = reverse (seqEKGRec k n []) |
|||
main :: IO () |
|||
⚫ | |||
main = |
|||
mapM_ |
|||
(\x -> |
|||
⚫ | |||
print (seqEKG x 20)) |
|||
[2, 5, 7, 9, 10] >> |
[2, 5, 7, 9, 10] >> |
||
putStr "EKG(5) and EKG(7) converge at " >> |
|||
print |
|||
print ((+1) $ fromJust $ findIndex (isPrefixOf (take 20 (repeat True))) |
|||
((+ 1) $ |
|||
⚫ | |||
fromJust $ |
|||
</lang> |
|||
findIndex |
|||
(isPrefixOf (replicate 20 True)) |
|||
⚫ | |||
{{out}} |
{{out}} |
||
⚫ | |||
<pre> |
|||
⚫ | |||
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}}== |