Created
March 26, 2014 16:38
-
-
Save swans-one/9787585 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
function fibonacci_1(n::Integer) | |
if n in [0,1] | |
return 1 | |
else | |
low = BigInt(1) | |
high = BigInt(1) | |
for i in 2:n | |
high,low = low,high | |
high = low + high | |
end | |
return high | |
end | |
end | |
function fibonacci_2(n::Integer) | |
if n in [0,1] | |
return 1 | |
else | |
low = BigInt(1) | |
high = BigInt(1) | |
for i in 2:n | |
low, high = next_fib(low, high) | |
end | |
return high | |
end | |
end | |
function next_fib(low, high) | |
return high, high + low | |
end | |
@time one = fibonacci_1(100000) | |
@time two = fibonacci_2(100000) | |
println(one == two) | |
# eriks-air:Sandbox erik$ julia fib.jl | |
# elapsed time: 1.101957641 seconds (438506792 bytes allocated) | |
# elapsed time: 0.460583948 seconds (437653280 bytes allocated) | |
# true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment