Skip to content

Instantly share code, notes, and snippets.

@ajbeach2
Created June 15, 2015 22:52
Show Gist options
  • Save ajbeach2/2260198a832f891ad306 to your computer and use it in GitHub Desktop.
Save ajbeach2/2260198a832f891ad306 to your computer and use it in GitHub Desktop.
def fib_mod(a,b,x,sequence = [])
if x == 1
result = a
elsif x == 2
result = b
else
result1 = sequence[x - 1] ||= fib_mod(a,b,x - 1, sequence)
result2 = sequence[x - 2] ||= fib_mod(a,b,x - 2, sequence)
result = result1**2 + result2
end
sequence[x] = result
return result
end
args = gets
puts fib_mod(*args.split(' ').map{|x| x.to_i})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment