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 | |
by Solarplight (Sexton) on Mar 23, 2010 at 09:31 UTC | |
by GrandFather (Saint) on Mar 23, 2010 at 09:50 UTC |