in reply to how to speed up program dealing with large numbers?

Well, Thank you everyone for all your help and info. Incase anyone was wondering here is how my code looks now:

#!/usr/bin/perl use strict; use warnings; my %cache; sub fib { my $n = shift; return $n if $n < 2; return ( $cache{ $n - 1 } ||= fib($n - 1) ) + ( $cache{ $n - 2 } ||= fib($n - 2) ); } print "Fibonnaci Fun\n\n"; print "Pick a number that represents which value of the Fibonnaci sequ +ence you want to start with\n"; print "ie: 1 would be 1, and 2 would be 1, and 3 would be 2 and so on. +\n"; print "Your choice: "; chomp(my $value = <>); print "\nNow pick a number to repesent the range you want.\n"; print "If you want to use values 1 through 10 you you need only enter +10,\n"; print "assuming of course you entered 1 above.\n"; print "Your choice: "; chomp(my $temp = <>); my $range = $value + $temp; my $phiApprox; my $fibValue1; my $fibValue2; for my $i ($value .. $range){ $fibValue1 = fib($i); $fibValue2 = fib($i - 1); $phiApprox = 0 if($i <= 1); $phiApprox = $fibValue1 / $fibValue2 if($i >= 2); print "\n"; print "$fibValue1 / $fibValue2 = $phiApprox"; } print "\n";

Replies are listed 'Best First'.
Re^2: how to speed up program dealing with large numbers?
by GrandFather (Saint) on Mar 22, 2010 at 20:03 UTC

    A slight tidy up:

    #!/usr/bin/perl use strict; use warnings; use bignum; print "Fibonnaci Fun\n\n"; print "Pick a number that represents which value of the Fibonnaci sequ +ence you want to start with\n"; print "ie: 1 would be 1, and 2 would be 1, and 3 would be 2 and so on. +\n"; print "Your choice: "; chomp (my $start = <>); print "\nNow pick a number to repesent the range you want.\n"; print "If you want to use values 1 through 10 you you need only enter +10,\n"; print "assuming of course you entered 1 above.\n"; print "Your choice: "; chomp(my $range = <>); print "\n"; for my $fibNum ($start .. $start + $range){ my $fibValue1 = fib($fibNum); my $fibValue2 = fib($fibNum - 1); my $phiApprox = $fibNum <= 1 ? '***' : $fibValue1 / $fibValue2; print "$fibValue1 / $fibValue2 = $phiApprox\n"; } my %cache; sub fib { my ($n) = @_; return $n if $n < 2; --$n; return ($cache{$n} ||= fib($n)) + ($cache{$n - 1} ||= fib($n - 1)) +; }

    Prints (for input numbers 90, 10):

    Fibonnaci Fun Pick a number that represents which value of the Fibonnaci sequence yo +u want to start with ie: 1 would be 1, and 2 would be 1, and 3 would be 2 and so on. Your choice: 90 Now pick a number to repesent the range you want. If you want to use values 1 through 10 you you need only enter 10, assuming of course you entered 1 above. Your choice: 10 2880067194370816120 / 1779979416004714189 = 1.618033988749894848204586 +834365638117579 4660046610375530309 / 2880067194370816120 = 1.618033988749894848204586 +834365638117774 7540113804746346429 / 4660046610375530309 = 1.618033988749894848204586 +8343656381177 12200160415121876738 / 7540113804746346429 = 1.61803398874989484820458 +6834365638117728 19740274219868223167 / 12200160415121876738 = 1.6180339887498948482045 +86834365638117717 31940434634990099905 / 19740274219868223167 = 1.6180339887498948482045 +86834365638117721 51680708854858323072 / 31940434634990099905 = 1.6180339887498948482045 +8683436563811772 83621143489848422977 / 51680708854858323072 = 1.6180339887498948482045 +8683436563811772 135301852344706746049 / 83621143489848422977 = 1.618033988749894848204 +58683436563811772 218922995834555169026 / 135301852344706746049 = 1.61803398874989484820 +458683436563811772 354224848179261915075 / 218922995834555169026 = 1.61803398874989484820 +458683436563811772

    You might have noticed the use of bignum btw.


    True laziness is hard work

      thank you for the tips, one question though

      I don't quite understand this line:

      my $phiApprox = $fibNum <= 1 ? '***' : $fibValue1 / $fibValue2;

      In paticular this part:

       ? '***' :

      Obviously it makes $phiApprox = '***' when $fibNum is <= 1, else it performs the arithmatic. But I don't really understand why, I would never have thought of that code.

        $phiApprox is not defined in that case so rather than invent an number for it to be I used *** to indicate an unknown value.


        True laziness is hard work