Numbers:
0, 1, 1, 2, 3, 5, 8, 13...

def fibonacci(n)
a = 0
b = 1
*# Compute Fibonacci number in the desired position.*
n.times do
temp = a
a = b
*# Add up previous two numbers in sequence.*
b = temp + b
end
return a
end
*# Write first 15 Fibonacci numbers in sequence.*
15.times do |n|
result = fibonacci(n)
puts result
end0
1
1
2
3
5
8
13
21
34
55
89
144
233
377

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

This page was last updated on Sep 15, 2023 (edit).