Created
September 22, 2011 23:24
-
-
Save gkmngrgn/1236330 to your computer and use it in GitHub Desktop.
fibonacci in erlang & python
This file contains hidden or 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
-module(fibonacci). | |
-export([calculate/1]). | |
fibonacci(_, _, Count, Count) -> | |
ok; | |
fibonacci(A, B, Num, Count) -> | |
io:format("~w~n", [A]), | |
fibonacci(B, A + B, Num, Count + 1). | |
calculate(Number) -> | |
io:format("Calculating Fibonacci Serial..~n"), | |
fibonacci(0, 1, Number, 0). |
This file contains hidden or 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
import sys | |
class Fibonacci(object): | |
def __init__(self, limit): | |
self.limit = limit | |
self.start_list = [0, 1] | |
def calculate(self): | |
result = self.start_list | |
print("Calculating Fibonacci Serial..") | |
for i in range(self.limit - len(result)): | |
next_value = sum(result[-2:]) | |
result.append(next_value) | |
return result | |
def print_result(self): | |
result = self.calculate() | |
for value in result: | |
print(value) | |
if __name__ == '__main__': | |
try: | |
limit = int(sys.argv[1]) | |
fibonacci = Fibonacci(limit) | |
fibonacci.print_result() | |
except ValueError, e: | |
print("First argument should be integer.") | |
sys.exit(e) | |
except IndexError, e: | |
print("An argument required for setting limit.") | |
sys.exit(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment