Efficient fibonacci series in ruby..
'''author -> shivamzaz @imsec
tag -> effiecient fibonacci series
level -> cakewalk'''
p=gets().to_i
l=[]
l<<0
l<<1
q=gets().to_i #qth num of fibonacci.
for i in p..1000 #1000 optional means upto series...
if(((Math.sqrt(5*i*i+4)).floor)==(Math.sqrt((5*i*i+4)).ceil) || (Math.sqrt((5*i*i-4)).floor)==(Math.sqrt((5*i*i-4)).ceil))
l<<i
end
end
puts(l[q-1]) #print qth fibonacci num.
'''output:
1
3
1
'''author -> shivamzaz @imsec
tag -> effiecient fibonacci series
level -> cakewalk'''
p=gets().to_i
l=[]
l<<0
l<<1
q=gets().to_i #qth num of fibonacci.
for i in p..1000 #1000 optional means upto series...
if(((Math.sqrt(5*i*i+4)).floor)==(Math.sqrt((5*i*i+4)).ceil) || (Math.sqrt((5*i*i-4)).floor)==(Math.sqrt((5*i*i-4)).ceil))
l<<i
end
end
puts(l[q-1]) #print qth fibonacci num.
'''output:
1
3
1
No comments:
Post a Comment