in reply to Re: OT: Finding Factor Closest To Square Root
in thread OT: Finding Factor Closest To Square Root
Can you tell us, why you need to find that one? May be we can come up with non-exhaustive alternatives.OK, but I was so enjoying the direction the discussion was going, and I didn't want the thread to lose the focus on the problem I stated. But here goes...
At various times I've been bitten by the Fibonacci bug, like many others. I've tried my hand at programming F(n), as well as some other interesting things like a prime tester .
Poking around on Mathworld, I read the Fibonacci page. One of the formulas is
whereF(k*n) = L(k)*F(k*(n-1)) - ((-1)**k)*F(k*(n-2))
and substitutingL(k) = F(n-1) + F(n+1)
For computing F(k*n), this is most efficient when k==n, givingF(k*n) = (F(n-1)+F(n+1))*F(k*(n-1)) - ((-1)**k)*F(k*(n-2))
If N is prime, useF(n**2) = (F(n-1)+F(n+1))*F(n*(n-1)) - ((-1)**n)*F(n*(n-2))
As an exercise in programming and improving my grasp of math and computational complexity, I was going to benchmark several methods of computing F(N). Included in this is the standard recursive solution (which is too slow, but we can estimate it's behavior for large N), the closed form using phi, and various identities.F(2*k+1) = F(k+1)**2 + F(k)**2
The identity for F(k*n) seemed to be the most promising in this area, though computing sqrt(N), and/or factoring N, would seem to negate any benefits. Perhaps a quick check to see if N has any small factors, otherwise use the F(2*k+1) identity for odd N, or one of the F(2*k) formulas for even N. (There's a nice identity for N % 3 == 0 too.)
Update: fixed missing parens in formulae.
-QM
--
Quantum Mechanics: The dreams stuff is made of
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: OT: Finding Factor Closest To Square Root
by chas (Priest) on Feb 20, 2005 at 22:48 UTC | |
by QM (Parson) on Feb 21, 2005 at 01:26 UTC |