Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

rgiskard's scratchpad

by rgiskard (Hermit)
on Nov 05, 2007 at 19:30 UTC ( [id://649083]=scratchpad: print w/replies, xml ) 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
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)
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found