Five programming problems every Software Engineer should be able to solve in less than 1 hour
Geoff Canyon
gcanyon at gmail.com
Sat May 9 19:32:22 EDT 2015
Problem 3
Write a function that computes the list of the first 100 Fibonacci numbers.
By definition, the first two numbers in the Fibonacci sequence are 0 and 1,
and each subsequent number is the sum of the previous two. As an example,
here are the first 10 Fibonnaci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, and
34.
I didn't hard code, instead taking an argument for the number of Fibonnaci
numbers to return:
function fib N
put "0,1" into R
if N <= 2 then return item 1 to N of R
put 0 into F1
put 1 into F2
repeat N - 2
put F1 + F2 into F3
put "",F3 after R
put F2 into F1
put F3 into F2
end repeat
return R
end fib
Test Data
0
1
2
20
Test Results
0
0,1
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181
More information about the use-livecode
mailing list