Created
June 9, 2015 14:05
-
-
Save teknocat/04ea62a6ec312d66cc6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://www.softantenna.com/wp/software/5-programming-problems/ | |
# https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
# | |
# 問題3 | |
# 最初の100個のフィボナッチ数のリストを計算する関数を記述せよ。 | |
# 定義では、フィボナッチ数列の最初の2つの数字は0と1で、次の数は前の2つの合計となる。 | |
# 例えば最初の10個のフィボナッチ数列は、0, 1, 1, 2, 3, 5, 8, 13, 21, 34となる。 | |
@fib = [0, 1] | |
@count = 100 | |
#@count = 10 | |
# @param [Array] fib | |
# @param [Fixnum] count | |
def func1(fib, count) | |
return if fib.length == count | |
fib.push(fib[-2] + fib[-1]) | |
func1(fib, count) | |
fib | |
end | |
result = func1(@fib.dup, @count) | |
p "count = #{@count}" | |
p "result = #{result}" | |
p "length = #{result.length}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment