A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Zeckendorf in Ruby page! Here, you'll find the source code for this program as well as a description of how the program works.
# frozen_string_literal: true
USAGE = "Usage: please input a non-negative integer"
def usage!
warn USAGE
exit 1
end
def parse_input(str)
usage! if str.nil? || str.strip.empty?
n = Integer(str)
usage! if n < 0
n
rescue ArgumentError, NoMethodError
usage!
end
def fibs_upto(n)
fibs = [1, 2]
fibs << fibs[-1] + fibs[-2] while fibs[-1] <= n
fibs.pop if fibs[-1] > n
fibs
end
def zeckendorf(n)
return [] if n == 0
fibs = fibs_upto(n)
result = []
i = fibs.length - 1
while n > 0
if fibs[i] <= n
result << fibs[i]
n -= fibs[i]
i -= 2
else
i -= 1
end
end
result
end
input = ARGV.first
n = parse_input(input)
puts zeckendorf(n).join(", ")
Zeckendorf in Ruby was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.