I'm using Binet's Formula to calculate Fibonacci number for large n
my code:
#!/usr/bin/env python3
def calc_fib(n):
if (n <= 1):
return n
root_5 = 5 ** 0.5
phi_n = ((root_5 + 1) / 2) ** n
alpha_n = ((root_5 - 1) / 2) ** n
fn = round((phi_n - alpha_n) / root_5)
return fn
n = int(input())
print(calc_fib(n))
$ ./fibonacci.py 200 280571172992512015699912586503521287798784. (wrong)
the correct result is: 280571172992510140037611932413038677189525
the problem is that for very large n, say n = 200, the result is not accurate, I think because of the floating-point calculations, How can I change my code so I can get more accurate results?
