| Description: | This node was taken out by the NodeReaper on Jun 04, 2021 at 12:10 UTC You may view the original node and the consideration vote tally. |
| Replies are listed 'Best First'. | |
|---|---|
|
RE (tilly) 1: Fibonnaci
by tilly (Archbishop) on Oct 03, 2000 at 03:20 UTC | |
| [reply] [d/l] |
by Adam (Vicar) on Oct 03, 2000 at 03:49 UTC | |
| [reply] |
by tilly (Archbishop) on Oct 03, 2000 at 04:32 UTC | |
| [reply] [d/l] |
by Adam (Vicar) on Oct 03, 2000 at 19:57 UTC | |
|
RE: Fibonnaci
by Adam (Vicar) on Oct 03, 2000 at 03:44 UTC | |
Which is very (very) very slow. Try finding Fib(30). My method uses an iterative approach, here I apply it to the subroutine style as before: Which is significantly faster. You could even implement it with a cache and then do all your work directly on the cache. This has the added bonus of not having to re-calculate the whole sequence every time you call Fib in a program. All of the above code has been tested. But I offer no garauntees. | [reply] [d/l] [select] |
|
RE: Fibonnaci
by AgentM (Curate) on Oct 05, 2000 at 22:32 UTC | |
AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere. | [reply] |
by Adam (Vicar) on Oct 05, 2000 at 23:52 UTC | |
Unfortunatly, Computers have a hard time with irrational numbers (like sqrt of 5) so this results in things like 75025.0000000001 which is ok for our purposes... but if you ever use this you might want to pass it through int. In case you were wondering, 73 is an arbitrary stopping place. I picked it because Fib(74) is too big a number for Perl, and results in the scientific notation: 1.30496954492866e+015 which just isn't as much fun. Update: Of course, what we really need is a benchmark! So here is the output: And that's just for Fib(30)! I also tried benchmarking the recursive version... hahahaha. So that it wouldn't take all day I dropped the count down to 10 iterations... still Fib(30) though. No Benchmark post is truly complete with out the code used:
| [reply] [d/l] [select] |
by tilly (Archbishop) on Oct 06, 2000 at 01:29 UTC | |
This is where F is the Fibonacci function, F(0)=0, F(1)=F(2)=1. In particular you use the special cases: and to calculate the powers of two and one off from the powers of two, then when you are done go to the general formula to calculate the number. Now why would one do this? Well it turns out that certain numbers can be tested for primality by answering a divisibility question about some close relative of the Fibonacci numbers (eg the Lucas numbers), so most of the "record primes" that you hear about involved some very big Fibonacci numbers being calculated somewhere. A good exercise for anyone who is interested, use the very slow Math::BigInt to come up with a function to calculate very large Fibonacci numbers. | [reply] [d/l] [select] |