Python Tutorial
Fibonacci series is an integer sequence where every number after the first two is the sum of the two preceding ones.
0 1 1 2 3 5 8 13 21 34 ... etc
Fibonacci n'th term is calculated as sum of n-1'th term and n-2'th term.
F{n}=F{n-1}+F{n-2} where F{0} = 0 and F{1} = 1
#!/usr/bin/python def get_fibonacci_series(terms): a, b = 0, 1 while a < terms: print(a, end=' ') a, b = b, a+b print() if __name__ == "__main__": terms = float(input("Enter number of terms: ")); get_fibonacci_series(terms)Output:
$ python3 fibonacci_series.py Enter number of terms: 6 0 1 1 2 3 5
#!/usr/bin/python def calculate_fibonacci_val(num): if num > 1: return calculate_fibonacci_val(num-2) + calculate_fibonacci_val(num-1) else: return num def get_fibonacci_series(limit): result = [] count = 0 while(count < limit): result.append(calculate_fibonacci_val(count)) count += 1 return result limit = 10 print(get_fibonacci_series(limit))Output:
$ python fibonacci_series.py [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
#!/usr/bin/python def get_fibonacci_series(limit): result = [] count = 1 temp1 = count -1 temp2 = count result.append(temp1) result.append(temp2) while(count < (limit-1)): next_val = temp1 + temp2 temp1 = temp2 temp2 = next_val; result.append(next_val) count += 1 return result limit = 10 print(get_fibonacci_series(limit))Output:
$ python fibonacci_series.py [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
#!/usr/bin/python def get_fibonacci_series(limit): result = [] count = 1 temp1 = count -1 temp2 = count result.append(temp1) result.append(temp2) for count in range(count, limit-1): next_val = temp1 + temp2 temp1 = temp2 temp2 = next_val; result.append(next_val) return result limit = 10 print(get_fibonacci_series(limit))Output:
$ python fibonacci_series.py [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Python Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page