Padovan n-step number sequences: Difference between revisions

m
→‎{{header|Haskell}}: Tidied and updated output.
m (→‎{{header|Haskell}}: Tidied and updated output.)
Line 625:
=={{header|Haskell}}==
<lang haskell>import Data.Bifunctor (second)
import Data.List (transpose, uncons, unfoldr)
 
------------------ PADOVAN N-STEP SERIES -----------------
Line 632:
padovans n
| 0 > n = []
| otherwise = unfoldr f(recurrence n) $ take (succ n) xs
where
f =
( fmap
. second
. flip (<>)
. pure
. sum
. take n
)
<*> uncons
xs
| 3 > n = repeat 1
| otherwise = padovans $ pred n
 
recurrence :: Int -> [Int] -> Maybe (Int, [Int])
recurrence n =
( fmap
. second
. flip (<>)
. pure
. sum
. take n
)
<*> uncons
 
--------------------------- TEST -------------------------
Line 651 ⟶ 653:
main =
putStrLn $
"Padovan N-step series:\n\n"
fTable
"Padovan<> N-step series:"spacedTable
show justifyRight
(justifyRight 4 ' ' . show( =<<)fmap
(take 15 . padovans) ( \n ->
[2show n <> " ..-> 8"]
<> fmap show (take 15 $ padovans n)
)
<*> ((" ->" <>)[2 . fxShow . f)8]
f = )
 
------------------------ FORMATTING ----------------------
 
fTablespacedTable ::
(Int -> Char -> String -> String) -> [[String]] -> String
String ->
spacedTable aligned rows =
(a -> String) ->
let columnWidths =
(b -> String) ->
(a -> b) -> fmap
(maximum . fmap length)
[a] ->
(transpose rows)
String
in unlines $
fTable s xShow fxShow f xs =
( fmap
unlines $
(unwords . zipWith (`aligned` ' ') columnWidths)
s :
fmap rows
( ((<>) . justifyRight w ' ' . xShow)
<*> ((" ->" <>) . fxShow . f)
)
xs
where
w = maximum (length . xShow <$> xs)
 
justifyRight :: Int -> a -> [a] -> [a]
Line 682:
{{Out}}
<pre>Padovan N-step series:
 
2 -> 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37
32 -> 1 1 1 2 2 3 4 5 6 7 9 12 1316 1921 28 41 60 88 12937
43 -> 1 1 1 2 3 4 6 5 7 11 17 9 2613 19 4028 41 61 60 94 14488 221129
54 -> 1 1 1 2 3 5 7 11 8 12 19 3017 26 4740 61 74 11694 182144 286221
65 -> 1 1 1 2 3 5 8 12 13 20 32 19 5130 47 8174 129116 205182 326286
76 -> 1 1 1 2 3 5 8 13 20 21 33 5332 51 8581 136129 218205 349326
87 -> 1 1 1 2 3 5 8 13 21 33 3453 85 54136 218 87 140 225 362</pre>349
8 -> 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362</pre>
 
=={{header|JavaScript}}==
9,655

edits