-
-
Save hyuki/2c0bf72064c16ff92b0bb648619b81dd to your computer and use it in GitHub Desktop.
『数学ガール』の最後で求めた式とフィボナッチ数列の比較(nが大きくなるとフィボナッチ数列が圧倒的に大きくなる)
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
# cf. https://twitter.com/hyuki/status/1394478452854587394 | |
FIB = {} | |
def fib(n) | |
if FIB[n.to_s] | |
return FIB[n.to_s] | |
end | |
if n <= 0 or n == 1 | |
v = 1.0 | |
FIB[n.to_s] = v | |
return v | |
else | |
v = fib(n - 1) + fib(n - 2) | |
FIB[n.to_s] = v | |
return v | |
end | |
end | |
K = Math::sqrt(6) * Math::PI / 3.0 | |
def val(n) | |
Math::exp(K * Math::sqrt(n)) | |
end | |
1001.times do |n| | |
printf("fib(#{n}) = %.0f\nval(#{n}) = %.0f\n\n", fib(n), val(n)) | |
end |
Author
hyuki
commented
May 18, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment