vladb has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
There was a node on node on the Discussions page that mentioned an encryption contest held by a university.
As it appeared, the contest was pretty basic (by far way tooo simple). Here's the code that is supposed to give the right answer to this question "FIND THE SUM OF ALL DECIMAL DIGITS APPEARING IN THE NATURAL NUMBERS FROM ONE TO ONE MILLION INCLUSIVE":
Thanks,
I was positive the code had to produce the right answer, however, it didn't. Instead, it came up with this number: 27000000. Is this a problem with Perl or the code? Or should I use a Big integer module in order to yield the right number?my ($s,$x,$i); for ($i=0; $i<1e6; $i++) { # print "$i:"; # get all digits in a number for ($i=~/./g) { # print "$_ "; # sum all digits $s += $_; } # print " ($s)\n"; }; print"\nAnswer: $s\n";
Thanks,
"There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith |
Back to
Seekers of Perl Wisdom