 Syntactic Confectionery Delight PerlMonks

by rgiskard (Hermit)
 on Nov 05, 2007 at 19:30 UTC Need Help??

```use strict;
use bignum;

iter_fib(0);

sub iter_fib {
my @buffer      = ( 1, 1, 2 );
my \$phi         = 0;
my \$convergence = -1;
my \$iter        = 3;

while ( !\$convergence->is_zero() ) {

# calculate next fibbonacci number
\$buffer = \$buffer + \$buffer;

# calculate phi
( \$phi, \$convergence ) =
calcPhi( \$buffer, \$buffer, \$buffer );

# report
my \$pad = ( \$iter < 10 ) ? '0' : '';

"DELTA\$convergence\tBuffer:(\$buffer,".
" \$buffer,"." \$buffer)\n";

++\$iter;

# shift the buffer
\$buffer = \$buffer;
\$buffer = \$buffer;
}
}

sub calcPhi {
my \$argPrevPrev = shift;
my \$argPrev     = shift;
my \$argCurr     = shift;

my @ratio = ( \$argPrev / \$argPrevPrev,
\$argCurr / \$argPrev );

my \$phi         = \$ratio;
my \$convergence = \$ratio - \$ratio;

return ( \$phi, \$convergence );

}

__END__

=pod

phi.pl - find phi using the fibonacci sequence

Iterates through the fibbonacci numbers,
keeping a buffer of three numbers.  Then it uses the
ratio of the current and previous fibonacci numbers to
calculate the golden ratio, a constant named phi.
Convergence is also calculated as a difference in
ratios.  The program stops when it determines no
changes have occurred in successive steps (aka. it has
converged on PHI as far as the datatypes will allow).

What was I thinking when I wrote this ?

=cut
Last 2 lines of output on me compy:
```Iter: 96    PHI=1.61803398874989484820458683436563811772    DELTA-0.00
+0000000000000000000000000000000000001    Buffer:(19740274219868223167
+, 31940434634990099905, 51680708854858323072)
Iter: 97    PHI=1.61803398874989484820458683436563811772    DELTA0
+Buffer:(31940434634990099905, 51680708854858323072, 83621143489848422
+977)

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2023-09-26 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?