Python has a lot of features that are convenient for math. One of them is operator overloading, or changing how +
, -
, *
, etc., work. We can use this to make numbers of the form easy to work with. Using this feature, we can make a custom Z5
object (so called because the mathematical way of describing this group of numbers is ) that represents one of these numbers.
If we play our cards right, we never even need to compute . Let's look at the full unrounded version of Binet's formula:
Similarly to how complex conjugates work, it turns out that will simply be with the sign of the coefficient flipped. This is why Binet's formula always gives an integer result: the integral parts cancel, and the will cleanly cancel without ever having to be computed. Dividing by powers of 2 is very simple for computers, because that's just chopping off zeros in binary. This lets us implement a new Fibonacci function, as seen opposite.1
- I do some work to avoid needing a big division by at the end, to keep the intermediate steps smaller. It's probably more fair to say that
Z5(a, b)
represents .↩
498454011879264 498454011879264
81104 81104
Python has a lot of features that are convenient for math. One of them is operator overloading, or changing how +
, -
, *
, etc., work. We can use this to make numbers of the form easy to work with. Using this feature, we can make a custom Z5
object (so called because the mathematical way of describing this group of numbers is ) that represents one of these numbers.
If we play our cards right, we never even need to compute . Let's look at the full unrounded version of Binet's formula:
Similarly to how complex conjugates work, it turns out that will simply be with the sign of the coefficient flipped. This is why Binet's formula always gives an integer result: the integral parts cancel, and the will cleanly cancel without ever having to be computed. Dividing by powers of 2 is very simple for computers, because that's just chopping off zeros in binary. This lets us implement a new Fibonacci function, as seen opposite.1
- I do some work to avoid needing a big division by at the end, to keep the intermediate steps smaller. It's probably more fair to say that
Z5(a, b)
represents .↩
498454011879264 498454011879264
81104 81104