mersenneforum.org Perl help with bigint
 User Name Remember Me? Password
 Register FAQ Search Today's Posts Mark Forums Read

 2007-04-06, 15:26 #1 kuratkull     Mar 2007 Estonia 8E16 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 2007-04-06 at 15:27
 2007-04-07, 07:54 #2 paulunderwood     Sep 2002 Database er0rr 3,673 Posts Does "$try" have a valid initialization? Last fiddled with by paulunderwood on 2007-04-07 at 07:55  2007-04-07, 08:34 #3 kuratkull Mar 2007 Estonia 14210 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 :)
2007-04-07, 08:36   #4
Chris Card

Aug 2004

2×5×13 Posts

Quote:
 Originally Posted by kuratkull 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
Hi kuratkull,

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

 2007-04-17, 21:19 #5 T.Rex     Feb 2004 France 22·229 Posts PARI/gp Haven't you tried PARI/gp, instead of Perl ?! T.
2007-04-17, 21:55   #6
kuratkull

Mar 2007
Estonia

2·71 Posts

Quote:
 Originally Posted by T.Rex Haven't you tried PARI/gp, instead of Perl ?! T.
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.

2007-04-18, 07:51   #7
T.Rex

Feb 2004
France

22×229 Posts

Quote:
 Originally Posted by kuratkull 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.
OK. I understand. I never learnt Perl. I only was an expert in Object Oriented languages, like Smalltalk-80 and Java, and I built my own Object language inside a Prolog compiler, LONG time ago, before Java appeared. So, what I know about Perl (I had to read, use and modify a big Perl program written by a colleague, a Perl expert) is that it really looks like a mess, from an object-oriented language point of view. So, good luck ! By the way, I also hate some of the choices they made when designing the syntax of the PARI/gp language too, and I must confess that the syntax of Smalltalk is ... very special/strange, but well organized. And I LOVED the Prolog language. So, maybe I'm a purist ... Maybe there are too many languages these times. Too much people like to create his own language ... What about Python ?
Regards,
Tony

 2007-04-18, 11:29 #8 fetofs     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 2007-04-18 at 11:31
 2007-04-18, 11:42 #9 kuratkull     Mar 2007 Estonia 2168 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 2007-04-18 at 11:45 Reason: details details...
 2007-04-18, 12:49 #10 fetofs     Aug 2005 Brazil 16A16 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 2007-04-18 at 12:57
2007-04-18, 14:00   #11
kuratkull

Mar 2007
Estonia

2×71 Posts

Quote:
 Originally Posted by fetofs 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.
Thx, but I still don't understand it. I tried and I figured out that there are 36288008(1*2*3*4*5*6*7*8*9*10) possible permutations(i think), but nothing else.
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 mathPuzzles Computer Science & Computational Number Theory 9 2017-07-01 03:35 Trejack Information & Answers 5 2016-04-17 03:02 chris2be8 Programming 12 2015-08-26 16:23 VJS Factoring 0 2006-07-10 22:25 Fusion_power Programming 1 2004-09-02 19:59

All times are UTC. The time now is 06:55.

Tue May 11 06:55:21 UTC 2021 up 33 days, 1:36, 1 user, load averages: 1.73, 1.87, 2.02

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.