Fibonacci n-step number sequences: Difference between revisions
→{{header|Ruby}}: polished code somewhat
(→{{header|Ruby}}: Removed reference to Ruby 1.9; use sum method.) |
(→{{header|Ruby}}: polished code somewhat) |
||
Line 3,903:
=={{header|Ruby}}==
<lang ruby>def anynacci(start_sequence, count)
n = start_sequence.length # Get the n-step for the type of fibonacci sequence
result = start_sequence.dup # Create a new result array with the values copied from the array that was passed by reference
▲ (n+1..count).each do # Loop for the remaining results up to count
result << result.last(n).sum # Get the last n element from result and append its total to Array
end
▲ result # Return result
end
naccis = { lucus: [2,1],
fibonacci: [1,1],
Line 3,924 ⟶ 3,922:
decanacci: [1,1,2,4,8,16,32,64,128,256] }
▲ puts naccis.map {|name, seq| "%12s : %p" % [name, anynacci(seq, count)]}
{{out}}
<lang ruby> lucus : [2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199, 322, 521, 843]
|