Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

phi div by 0 error

by perlaintdead (Scribe)
on Sep 01, 2013 at 14:12 UTC ( #1051800=perlquestion: print w/ replies, xml ) Need Help??
perlaintdead has asked for the wisdom of the Perl Monks concerning the following question:

Oh wise monks, i must ask to much of you once again. I wrote a script to search for Phi and got a few errors.

Use of uninitialized value in division (/) at F:\scripts\phi.pl line 2 +8. Use of uninitialized value in division (/) at F:\scripts\phi.pl line 2 +8. Illegal division by zero at F:\scripts\phi.pl line 28.

here's the code:

use strict; use warnings; my @list; my $a=1; my $b=1; my $c=0; for(my $i = 0;$i < 10;$i++){ $c=$a+$b; $a=$b; $b=$c; push @list, $c; } my $Evens = -2; my $Odds = -1; my $Interval = 2; for(my $i = 0;$i < 10;$i++){ $Odds += $Interval; $Evens += $Interval; print( ($list[$Odds] / $list[$Evens]) . "\n"); }

Comment on phi div by 0 error
Select or Download Code
Re: phi div by 0 error
by hdb (Prior) 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;

      thanks. I actually solved this one before saw your post on a hunch because i saw it outputed exactly half of what i was expecting. I'll have to play around with that sqrt trick.

      (1+sqrt(5))/2 came out to 3.23606797749979 AKA not phi

        just needed another parentheses ((sqrt(5)+1)/2)

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

    Put this print statement in the second for loop:

    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,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: phi div by 0 error
by perlaintdead (Scribe) on Sep 01, 2013 at 15:20 UTC

    Implemented Math::BigFloat and set the for loop iteration to 1000. I got this.

    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

      I progressively got more and more extreme. I now have 97705 digits of Phi.

Re: phi div by 0 error
by Laurent_R (Parson) 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 ($n-1) + fibo ($n-2); }

    This gives the following result:

    $ perl fibo3.pl 32 1.618033988750
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"; }
Re: phi div by 0 error
by Athanasius (Monsignor) on Sep 02, 2013 at 02:42 UTC

    Simplify! :-)

    #! 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,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1051800]
Approved by hdb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2014-12-26 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls