perlquestion
vladb
Hi,
There was a node on [id://140679|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":
<code>
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";
</code>
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?
<br><br>
Thanks,
<br><br>
<table border=1 align=center>
<tr><td>
<i>"There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith</i>
</td></tr>
</table>