#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    New bird perl cgi.pm question on param( )


    i'm trying to do f(n) =f(n-1) + f(n-2) 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 !
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    837
    Rep Power
    496
    So, you are trying to implement the Fibonacci series.
    a[i] = a[i-1] + a[i-2]
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    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[i-1] + a[i-2]
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,941
    Rep Power
    1225
    There are several Fibonacci modules on cpan which would be of interest.

    http://search.cpan.org/search?query=Fibonacci+&mode=all
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    837
    Rep Power
    496
    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($n-1) + fib($n-2);
    }
    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 04:50 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    837
    Rep Power
    496
    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).
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    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($n-1) + fib($n-2);
    }
    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';
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    837
    Rep Power
    496
    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.

IMN logo majestic logo threadwatch logo seochat tools logo