Re: phi div by 0 error
by hdb (Monsignor) on Sep 01, 2013 at 14:24 UTC

A faster way is to use $phi = (1+sqrt(5))/2;
However, if you insist of using the ratios of Fibonacci numbers (it would have been nice to mention that), then you need to create 20 of them (rather than 10) if you always divide the even numbered ones by the odd numbered ones. So let your first loop run until 20 instead of 10.
UPDATE: A postfix loop solution for you:
use strict;
use warnings;
my @list = ( 1, 1 );
push @list, $list[1]+$list[2] for 0..17;
print +($list[2*$_+1] / $list[2*$_]) . "\n" for 0..9;
 [reply] [d/l] [select] 

 [reply] 

 [reply] [d/l] 

 [reply] 

 [reply] 

Re: phi div by 0 error
by Athanasius (Chancellor) on Sep 01, 2013 at 14:27 UTC

print "Evens = $Evens, Odds = $Odds\n";
and you’ll see that on the sixth iteration, $Evens is 10 and $Odds is 11 — both referring to uninitialised elements of array @list. So the division becomes 0 / 0, which gives the error.
hdb has shown how to fix this.
Hope that helps,
 [reply] [d/l] [select] 
Re: phi div by 0 error
by Laurent_R (Canon) on Sep 01, 2013 at 16:32 UTC

Just for fun, with a recursive definition of the Fibonacci function:
#!/usr/bin/perl
use strict;
use warnings;
use Memoize;
memoize 'fibo';
my $val = shift;
printf "%.12f\n", fibo($val+1)/fibo($val);
sub fibo {
my $n = shift;
return $n if ($n < 2);
return fibo ($n1) + fibo ($n2);
}
This gives the following result:
$ perl fibo3.pl 32
1.618033988750
 [reply] [d/l] [select] 
Re: phi div by 0 error
by Athanasius (Chancellor) on Sep 02, 2013 at 02:42 UTC

#! perl
use strict;
use warnings;
use bignum;
my ($even, $odd) = (1, 1);
for (1 .. 47)
{
$even += $odd;
$odd += $even;
print "phi = ", ($odd / $even), "\n";
}
47 iterations gives phi correct to 38 decimal places:
phi = 1.61803398874989484820458683436563811772
Update: Timed (with Time::HiRes) at less than 0.3 seconds.
Hope that helps,
 [reply] [d/l] [select] 
Re: phi div by 0 error
by perlaintdead (Scribe) on Sep 01, 2013 at 21:01 UTC

heres my new shiny one if you are intersted. you might have to adjust the for loop if you don't want to wait a few hours
use strict;
use warnings;
use Math::BigFloat;
use Math::BigInt;
my @list;
my $a = Math::BigInt>new(1);
my $b = Math::BigInt>new(1);
my $c = Math::BigInt>new(0);
for(my $i = 0;$i < 1000;$i++){
$c = $a>badd($b);
$a = $b;
$b = $c;
push @list, $c;
}
my $Evens = 2;
my $Odds = 1;
my $Interval = 2;
for(my $i = 0;$i < ($#list/2);$i++){
$Odds += $Interval;
$Evens += $Interval;
my $x = Math::BigFloat>new($list[$Odds]);
$x>bround(100000);
print $x>bdiv($list[$Evens]);
print "\n";
}
 [reply] [d/l] 
Re: phi div by 0 error
by perlaintdead (Scribe) on Sep 01, 2013 at 15:20 UTC

1.61803398874989134108149616839120102549529239736894442545810864027200
+4672370011
5821867348448257489376743928929399432794736492740848900619637044468374
+2207769895
5284871351149952419914396715116304293153432202518784423148528900571338
+6036307288
2666653413158187588889523960353654538133067018527952357922321879257356
+7283883127
7094213145824463279151550928901338078022671782538720109128857973217206
+4435763570
4834109645509800631132924524020295001913106367168146749062307167701770
+4214956950
4993984233902344817301165562098038711338438280454536671077633488806776
+2538380409
4923932589997007833231933734402447427253643144652108581074116780141710
+8024560671
5128413634249080667591897167775382994790390597191607264786128916197903
+0377805716
0904108406395045059538778428623236698441468433287357807746043807953509
+2604173695
2869797766388741261679692402860638102715386529683999970737044861693007
+8122405879
3542093499009637260570055508183563356247329736259263178694538356045137
+3449390853
2681011158895040808014625634585023423690443466557686937200000000000000
+0000000000
 [reply] [d/l] 

 [reply] 

 [reply] [d/l] 
