Skip to content

Instantly share code, notes, and snippets.

@swans-one
Created March 26, 2014 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swans-one/9787585 to your computer and use it in GitHub Desktop.
Save swans-one/9787585 to your computer and use it in GitHub Desktop.
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