How to vary increase of x as n increments through the Fibonacci series?

783 Views Asked by At

Excuse my incorrect use of terminology, I hope my question is clear:

I am coding a Python module which tests whether a given number is a member of the Fibonacci series. No problem with that. Additionally, should a number not be a member of the series, I would like to test whether it is significantly close to its nearest Fibonacci neighbor. Here an increasing deviation margin of what is considered "close" is needed, along the lines of:

deviation margin (x) increases as given number (n) increases

For my purposes 9 is significantly close to Fibonacci number 8 but 10 is not. 1600 is significantly close to Fibonacci number 1597 but 1610 is not, etc. So, the test for "significant closeness" is applying a deviation margin of 1 at lower numbers and an increasing deviation margin as the series increments up to infinity.

I figured a logical candidate for inclusion in the test would be Standard Deviation. So I have calculated a coarse margin as follows:

margin = (StdDev / ( n + closestfibneighbor)) * StdDev

This does not give me good control over the margin rate of increase and I am sure there is a more appropriate function to express the growth in margin caused by increments in x. Please feel free to elaborate on the mathematics of this - I am seeking a general solution and not a Python-specific function.

2

There are 2 best solutions below

7
On BEST ANSWER

The Fibonacci numbers, the larger ones in particular, are very close to being given by the formula $$ F_n\approx\frac1{\sqrt5} \left(\frac{1+\sqrt5}2\right)^n $$ ($F_n$ is always the closest integer to the r.h.s.). In light of this I would also consider calculating, given input $x$, how far the ratio $$ r(x):=\frac{\log(x\cdot\sqrt5)}{\log\big((1+\sqrt5)/2\big)} $$ is from being an integer. In other words, I might use the number $$ s(x)=\left|r(x)-round(r(x))\right| $$ for measuring how far $x$ is from being a Fibonacci number.

The point is that a test based on Binet's formula should be faster than anything recursive. Of course, you need very precise floating point operations to be able to use that as a test, whether an integer $x$ actually is a Fibonacci number: compute $r(x)$, and find whether $$ x=round\left(\frac1{\sqrt5} \left(\frac{1+\sqrt5}2\right)^{round(r(x))}\right). $$

1
On

A suitable solution (suggested by Rahul Narain) is to simply calculate the margin as a fixed percentage of each successive Fibonacci number in the series:

margin = 1 + 0.01n

This gives the desired outcome of allowing a margin of ~1 at the lowest Fib numbers and an increasing margin as the series increments. In the spirit of the Fib series I calculate the margin as 13% of n instead of 10%.

Using standard deviation as a variable in this specific context is not elegant since a percentage of Std Dev will have to be applied in a working solution anyway.