Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 1st, 2012, 04:13 PM
ningji ningji is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 ningji User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 52 sec
Reputation 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 !

Reply With Quote
  #2  
Old November 1st, 2012, 04:49 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 513 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Days 20 h 27 m 59 sec
Reputation Power: 405
So, you are trying to implement the Fibonacci series.
a[i] = a[i-1] + a[i-2]

Reply With Quote
  #3  
Old November 1st, 2012, 04:54 PM
ningji ningji is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 ningji User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 52 sec
Reputation Power: 0
yes, i want to display it in the web, how to do that pls ?


Quote:
Originally Posted by Laurent_R
So, you are trying to implement the Fibonacci series.
a[i] = a[i-1] + a[i-2]

Reply With Quote
  #4  
Old November 1st, 2012, 04:57 PM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,653 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 6 h 48 m
Reputation Power: 1170
There are several Fibonacci modules on cpan which would be of interest.

http://search.cpan.org/search?query=Fibonacci+&mode=all

Reply With Quote
  #5  
Old November 2nd, 2012, 02:21 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 513 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Days 20 h 27 m 59 sec
Reputation Power: 405
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.

Reply With Quote
  #6  
Old November 2nd, 2012, 05:11 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 513 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Days 20 h 27 m 59 sec
Reputation Power: 405
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).

Reply With Quote
  #7  
Old November 2nd, 2012, 09:16 AM
ningji ningji is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 ningji User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 52 sec
Reputation 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 !

Quote:
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';

Reply With Quote
  #8  
Old November 2nd, 2012, 11:44 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 513 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Days 20 h 27 m 59 sec
Reputation Power: 405
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > New bird perl cgi.pm question on param( )

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap