Skip to content

Instantly share code, notes, and snippets.

@SirVer
Created August 15, 2012 22:30
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 SirVer/3364304 to your computer and use it in GitHub Desktop.
Save SirVer/3364304 to your computer and use it in GitHub Desktop.
iterfib in various languages
-- iterfib.cc --
#include <iostream>
#include <stdint.h>
using namespace std;
uint64_t fib(int k) {
uint64_t ln = 1;
uint64_t n = 1;
uint64_t temp;
for (int i = 2; i < k; ++i) {
temp = ln;
ln = n;
n += temp;
}
return n;
}
int main(int argc, char const *argv[])
{
uint64_t k = 0;
for (size_t i = 0; i < 1000000; ++i) {
k |= fib(92);
}
cout << k << endl;
}
-- iterfib.py --
#!/usr/bin/env python
# encoding: utf-8
def fib(k):
ln, n = 1, 1
for i in xrange(2,k):
ln, n = n, ln + n
return n
fib(92)
fib(92)
fib(92)
fib(92)
fib(92)
fib(92)
import time
s = time.clock()
fib(92)
print "Elapsed: %.2f musecs" % ((time.clock() - s)*1e6)
-- iterfib.jl --
function fib(k::Uint64)
ln::Uint64, n::Uint64 = 1, 1
for i=2:(k-1)
ln, n = n, ln + n
end
return n
end
const x = convert(Uint64, 92)
fib(x)
fib(x)
fib(x)
fib(x)
fib(x)
fib(x)
t = @elapsed fib(x)
print("Elapsed: ", t*1e6, " musecs\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment