Created
February 9, 2013 10:08
-
-
Save passcod/4744766 to your computer and use it in GitHub Desktop.
Fastfib
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 fastfib(n){ | |
var i=0, fibs = [0,1]; | |
for (; i < n; i += 1) { | |
fibs.push(fibs[0] + fibs[1]); | |
fibs.shift(); | |
} | |
return fibs[0]; | |
} | |
var n = 0, last = 0; | |
for (var n = 1; n < 1000000; n += 1) { | |
last += fastfib(200) / (n*n); | |
} | |
print(last); |
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
# Python 2 | |
def fastfib(n): | |
fibs = [0,1] | |
for i in xrange(0, n): | |
fibs.append(fibs[0] + fibs[1]) | |
fibs.pop(0) | |
return fibs[0] | |
last = 0 | |
for n in xrange(1, 1000000): | |
last += fastfib(200) / (n*n) | |
print last |
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
def fastfib n | |
fibs = [0,1] | |
n.times { | |
fibs.push fibs[0] + fibs[1] | |
fibs.shift | |
} | |
fibs[0] | |
end | |
last = 0 | |
1.step(1000000) { |n| | |
last += fastfib(200) / (n*n) | |
} | |
puts last |
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
# Python 3 | |
def fastfib(n): | |
fibs = [0,1] | |
for i in range(0, n): | |
fibs.append(fibs[0] + fibs[1]) | |
fibs.pop(0) | |
return fibs[0] | |
last = 0 | |
for n in range(1, 1000000): | |
last += fastfib(200) / (n*n) | |
print(last) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment