20070406, 15:26  #1 
Mar 2007
Estonia
89_{16} Posts 
Perl help with bigint
Hey, this isn't related to primes, but maybe you can still help me :)
I made a little program to solve this: http://projecteuler.net/index.php?se...problems&id=56 But I am having issues with the bigint support. If you run this code, it will output lines in this form: 99  96  3.8104711810455e+191  NaN it means: 99**96=3.8104711810455e+191 and the sum of all digits from the answer equals NaN, since it can't understand e+191. But I included $win2=99**99; in the beginning and let it be printed in the end, and it prints out fine, with all the digits. So why doesn't it handle the other big numbers correctly? :/ Thx for any help! Code:
#!/usr/bin/perl strict; use bigint; @ab = (1..99); @bb = (1..99); $counta = 0; $countb = 0; $win = 0; $win2 = 99**99; #this is the debug until (($counta ge 99) && ($countb le 99)) { until ($countb == 99) { $num = @ab[$counta]**@bb[$countb]; @num = split //, "$num"; until ($add == length($num)) { $try += @num[$add]; $add++; } print "@ab[$counta]  @bb[$countb]  $num  $try\n"; $add=0; $countb++; if ($try gt $win) {$win = $try;} $try = 0; } $counta++; $countb=0; } print "Biggest: $win and Debug: $win2\n"; Last fiddled with by kuratkull on 20070406 at 15:27 
20070407, 07:54  #2 
Sep 2002
Database er0rr
111000001010_{2} Posts 
Does "$try" have a valid initialization?
Last fiddled with by paulunderwood on 20070407 at 07:55 
20070407, 08:34  #3 
Mar 2007
Estonia
137 Posts 
Yes $try is correct, but I figured out my problem, I used arrays when there was no need:
I changed: $num = @ab[$counta]**@bb[$countb]; to $num = $counta**$countb; and it worked :) Mods may lock this. PS! Thanks anyway Paul :) 
20070407, 08:36  #4  
Aug 2004
130_{10} Posts 
Quote:
the second line seems to say you want Perl to use strict mode, but the way to do that is to write "use strict". If you just do that, then of course Perl complains about all the undeclared variables. Once you've declared the variables with "my" it all seems to work: Code:
#!/usr/bin/perl use strict; use bigint; my @ab = (1..99); my @bb = (1..99); my $counta = 0; my $countb = 0; my $win = 0; my $win2 = 99**99; #this is the debug until (($counta ge 99) && ($countb le 99)) { until ($countb == 99) { my $num = @ab[$counta]**@bb[$countb]; my @num = split //, "$num"; my $add; my $try; until ($add == length($num)) { $try += @num[$add]; $add++; } print "@ab[$counta]  @bb[$countb]  $num  $try\n"; $add=0; $countb++; if ($try gt $win) {$win = $try;} $try = 0; } $counta++; $countb=0; } print "Biggest: $win and Debug: $win2\n"; 99  96  3.8104711810455e+191  59 ... HTH Chris 

20070417, 21:19  #5 
Feb 2004
France
2·457 Posts 
PARI/gp
Haven't you tried PARI/gp, instead of Perl ?!
T. 
20070417, 21:55  #6 
Mar 2007
Estonia
89_{16} Posts 
I just looked it up on Google, and it seems suitable for the task. But I am interested in using Perl for this, since I plan to take my knowledge of Perl to a very high level :)
Solving these mathematical problems daily has given a magnificent boost to learning Perl. 
20070418, 07:51  #7  
Feb 2004
France
2×457 Posts 
Quote:
Regards, Tony 

20070418, 11:29  #8 
Aug 2005
Brazil
2·181 Posts 
Out of curiosity, what's your score?
It may help me if you're ahead, because then I'll at least get an incentive to solve some of the harder ones. EDIT: Found ya! http://projecteuler.net/index.php?se...e&profile=9634 Now let's go to work, because there are still 10 problems to solve.... Last fiddled with by fetofs on 20070418 at 11:31 
20070418, 11:42  #9 
Mar 2007
Estonia
137 Posts 
Yes, Perl can seem too cluttered for the untrained eye, no offence :)
But every programmer has a different coding style(mine is especially messy), but It can be made to look nicer with "perltidy", which lines up the code and such. But I tried learning Java a year or so back, it didn't fascinate me as much when I was learning it from the "Java for Dummies" (e)book, so I quit. Actually I learned Perl, to code a spambot trap(generates random email addresses and links loop back to the original file: http://kuratkull.pri.ee/userdata/index.html ), but never really understood its potential. A few months later I discovered Project Euler(maybe from mersenneforum?) and then I had a reason to learn it. So now, here I am, with 45 solved problems in Proj.Eul. using Perl :) fetofs, found you too :) Which problems you need help with? I'll take the liberty to ask for hints from you, to problems unsolved by me. EDIT: Yes I have thought about learning Python...and C, and C++ and Haskell. But it's Perls turn at the moment. Last fiddled with by kuratkull on 20070418 at 11:45 Reason: details details... 
20070418, 12:49  #10 
Aug 2005
Brazil
2×181 Posts 
Well, I'm pretty clueless at anything I haven't solved yet. As you haven't solved the easy problem 24, I'll give you a hint:
Computing all permutations is overkill here. You can even do it by hand, however. What is the number of permutations starting with 0? Remember, you want the millionth one... Proceed in this way to find the number digit by digit. Last fiddled with by fetofs on 20070418 at 12:57 
20070418, 14:00  #11  
Mar 2007
Estonia
137 Posts 
Quote:
Note: I'm in the 12th grade, so I haven't really had the chance to work out algorhythms and such :) A few more hints should do it :) I really want to solve this(even if partly). Thx. I'll look for a problem to help you with. 

Thread Tools  
Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Detecting bigInt roots  mathPuzzles  Computer Science & Computational Number Theory  9  20170701 03:35 
HI needing help on installing Perl/ntheory  Trejack  Information & Answers  5  20160417 03:02 
Factoring polynomials in perl  chris2be8  Programming  12  20150826 16:23 
Bigint problem with snfs latest snapshot  VJS  Factoring  0  20060710 22:25 
What is a good version of PERL to run on a win2000 pc?  Fusion_power  Programming  1  20040902 19:59 