# calculate the fibonacci to 'x' number of places and return it as an array
def fibonacci(limit)
# creates a new array
array = array.new
# our first number
num1 = 1
# our second number
num2 = 1
# our next number
nextnum = 0
# loop through until we reach our limit
# note: we need to subtract 2 because we will add two numbers to the beginning later
while nextnum < (limit - 2)
# our third number will be made by adding our first and second number together
num3 = num1 num2;
# our new first number is our old second number
num1 = num2;
# our new second number is our old third number
num2 = num3;
# insert our new number into our array
array.insert(nextnum, num3)
# this will be our next number
nextnum = 1 # you can also use: nextnum = nextnum.next
# exit the 'while' loop
end
# insert the number 1 into the beginning of our array
array.insert(0, 1)
# insert the number 1 into the 2nd position of our array
array.insert(1, 1)
# return our array
return array
# exit the method
end
用户登录
还没有账号?立即注册
用户注册
投稿取消
| 文章分类: |
|
还能输入300字
上传中....
简简单单单