Do you know where your variables are? PerlMonks

### rgiskard's scratchpad

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[2] = \$buffer[0] + \$buffer[1];

# calculate phi
( \$phi, \$convergence ) =
calcPhi( \$buffer[0], \$buffer[1], \$buffer[2] );

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

print "Iter: \$pad\$iter\tPHI=\$phi\t".
"DELTA\$convergence\tBuffer:(\$buffer[0],".
" \$buffer[1],"." \$buffer[2])\n";

++\$iter;

# shift the buffer
\$buffer[0] = \$buffer[1];
\$buffer[1] = \$buffer[2];
}
}

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

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

my \$phi         = \$ratio[1];
my \$convergence = \$ratio[1] - \$ratio[0];

return ( \$phi, \$convergence );

}

__END__

=pod

=head1 NAME

phi.pl - find phi using the fibonacci sequence

=head1 SYNOPSIS

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).

=head1 QUESTIONS

What was I thinking when I wrote this ?

=cut

[download]```
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)
[download]```
Log In?
 Username: Password:

What's my password?
Create A New User
Chatterbox?
NodeReaper practices with his stiletto

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-08-20 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Asked to put a square peg in a round hole, I would:

Results (190 votes). Check out past polls.

Notices?