November 1st, 2012, 05:13 PM

New bird perl cgi.pm question on param( )
i'm trying to do f(n) =f(n1) + f(n2) in the web,
1,1,2,3,5,8,13,21,....
this is the plan,
for the webpage, it has only 1 button "next page".
and will display "1 1"
my goal is, when i click "next", hope to see "1 2".
certainly if click again, "2 3".
=====
Now in my code, when i do click,
if($mycgi>param("my_button") eq "next page")
{
$var1 = $mycgi>param("first_var");
$var2 = $mycgi>param("second_var");
}
$var1 = $var1 + $var2;
print $mycgi>h4($var2);
print $mycgi>h4($var1);
$mycgi>param("first_var" => $var2);
$mycgi>param("second_var" => $var1);
=====
But it doesn't work, anything missing ?
Thanks for help !
November 1st, 2012, 05:49 PM

So, you are trying to implement the Fibonacci series.
a[i] = a[i1] + a[i2]
November 1st, 2012, 05:54 PM

yes, i want to display it in the web, how to do that pls ?
Originally Posted by Laurent_R
So, you are trying to implement the Fibonacci series.
a[i] = a[i1] + a[i2]
November 1st, 2012, 05:57 PM

November 2nd, 2012, 03:21 AM

You probably want to use an array to store the members of the series.
The easiest way to compute the fibonacci series is to use a recursive definition, but it may become very slow for large numbers, so you would have to store the intermediate values or to use the Memoize standard module.
Without memoization:
Code:
sub fib {
my $n = shift;
return 1 if $n == 1;
return fib($n1) + fib($n2);
}
But this would probably take centuries to compute fib(60) and billions of years to compute fib(100).
So add memoization or store in an array the values calculated so far.
For example, add this in the main section of your program:
Code:
use Memoize;
memoize 'fib';
Last edited by Laurent_R; November 2nd, 2012 at 05:50 AM.
November 2nd, 2012, 06:11 AM

As an example of the exponential explosion of the fib function above without using memoize: calculating fib(20) took 0.044 seconds on my server, fib(30) took 3.36 sec. and fib(35) took more than 35 seconds.
Code:
$ time perl fibo.pl 20
10946
real 0m0.044s
user 0m0.035s
sys 0m0.008s
$ time perl fibo.pl 30
1346269
real 0m3.358s
user 0m3.245s
sys 0m0.008s
$ time perl fibo.pl 35
14930352
real 0m35.809s
user 0m35.387s
sys 0m0.008s
Once fixed with the memoize module:
Code:
use strict;
use warnings;
use Memoize;
memoize 'fib';
my $value = shift;
print fib($value);
sub fib {
my $n = shift;
return 1 if $n < 2;
return fib($n  1) + fib($n  2);
}
the calculation of fib(35) lasted 0.074 seconds.
Code:
time perl fibo.pl 35
14930352
real 0m0.074s
user 0m0.017s
sys 0m0.009s
Having said that, if you use larger numbers you will soon have a warning on deep recursion:
Code:
time perl fibo.pl 100
Deep recursion on anonymous subroutine at fibo.pl line 13.
Deep recursion on anonymous subroutine at fibo.pl line 13.
5.73147844013817e+20
real 0m0.068s
user 0m0.020s
sys 0m0.009s
So, if there is any chance that this function is called with large numbers (expecially if you rely on user input), then don't use recursion and do a loop instead. Notice that the Fibonacci grow extremely fast (fib 200 is 4.53973694165308e+41) and you will be soon have numbers exceeding the capacity of the machine. So it is probably good to add a code line preventing the use of numbers larger than a certain limit (or use one of the big integer modules).
November 2nd, 2012, 10:16 AM

Hi Laurent,
Thanks very much to your answers.
The actual problem is how to do this in the web side.
If i use cookies, i can keep the old value to use for the next time.
But want to use param( ) instead. So far no luck on that.
Have a nice day !
Originally Posted by Laurent_R
You probably want to use an array to store the members of the series.
The easiest way to compute the fibonacci series is to use a recursive definition, but it may become very slow for large numbers, so you would have to store the intermediate values or to use the Memoize standard module.
Without memoization:
Code:
sub fib {
my $n = shift;
return 1 if $n == 1;
return fib($n1) + fib($n2);
}
But this would probably take centuries to compute fib(60) and billions of years to compute fib(100).
So add memoization or store in an array the values calculated so far.
For example, add this in the main section of your program:
Code:
use Memoize;
memoize 'fib';
November 2nd, 2012, 12:44 PM

You don't really need to keep the old values for reuse. You can calculate it on the fly the way I showed. It is very fast if you use memoization.