H34VENT

p10 algo

Nov 8th, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. class Fibonacci:
  2. def __init__(self):
  3. self.fibcount = 10
  4. self.firstfib = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
  5.  
  6. def fib(self, n):
  7. if n <= self.fibcount:
  8. return self.firstfib[n-1]
  9. else:
  10. for i in range(self.fibcount, n):
  11. self.firstfib.append(self.firstfib[-2] + self.firstfib[-1])
  12. self.fibcount = n
  13. return self.firstfib[-1]
  14.  
  15. def fiblist(self, n):
  16. if n <= self.fibcount:
  17. return self.firstfib[:n]
  18. else:
  19. self.fib(n)
  20. return self.firstfib
  21.  
  22. fibo = Fibonacci()
  23. inp = int(input("Masukkan Jumlah Deret Angka : "))
  24. print("Hasil : ")
  25. print(', '.join(map(str, fibo.fiblist(inp))))
Add Comment
Please, Sign In to add comment