Last active
March 11, 2016 14:12
-
-
Save jbaum98/c34187474bde48d6177d to your computer and use it in GitHub Desktop.
Fibonacci in different languages
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
#include <stdio.h> | |
int fib(int n) { | |
int twoBack = 0, | |
oneBack = 1, | |
nSoFar; | |
for (nSoFar = 1; nSoFar < n; nSoFar++) { | |
int oldOneBack = oneBack; | |
oneBack += twoBack; | |
twoBack = oldOneBack; | |
} | |
return oneBack; | |
} | |
int main() { | |
printf("%d\n", fib(30)); | |
} |
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
#!/usr/bin/env clisp | |
(defun fib (n) | |
(fibIter n 1 0 1)) | |
(defun fibIter (n nSoFar twoBack oneBack) | |
(if | |
(= n nSoFar) oneBack | |
(fibIter n (+ nSoFar 1) oneBack (+ oneBack twoBack)))) | |
(format t "~A~%" (fib 30)) |
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
#!/usr/bin/env python | |
def fib(n): | |
twoBack = 0 | |
oneBack = 1 | |
for nSoFar in range(1, n): | |
oldOneBack = oneBack | |
oneBack += twoBack | |
twoBack = oldOneBack | |
return oneBack | |
if __name__ == "__main__": | |
print fib(30) |
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
#include <stdio.h> | |
int fib(int n) { | |
if (n == 0) { | |
return 0; | |
} else if (n == 1) { | |
return 1; | |
} else { | |
return fib(n-1) + fib(n-2); | |
} | |
} | |
int main() { | |
printf("%d\n", fib(30)); | |
} |
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
#!/usr/bin/env clisp | |
(defun fib (n) | |
(cond | |
((= n 0) 0) | |
((= n 1) 1) | |
(t (+ (fib (- n 1)) (fib (- n 2)))))) | |
(format t "~A~%" (fib 30)) |
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
#!/usr/bin/env python | |
def fib(n): | |
if n == 0: | |
return 0 | |
elif n == 1: | |
return 1 | |
else: | |
return fib(n-1) + fib(n-2) | |
if __name__ == "__main__": | |
print fib(30) |
Author
jbaum98
commented
Mar 11, 2016
Language | Time |
---|---|
C | 0.028 |
Fast C | 0.002 |
Python | 0.631 |
Fast Python | 0.040 |
Lisp | 2.828 |
Lisp Fast | 0.036 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment