I'll search and update a similar discussion we had about this.
UPDATE: here it is Infinity and Inf?
IMHO a bug or at least a reasonable feature request.
Cheers Rolf
A two part answer to your question:
As I thought, the range operator only returns "normal" integers, and 145**145 causes an overflow and becomes INF.
But copying to another var helps.
my $answer = 1; for my $j ( 1 .. 999 ) { print "$i: $answer\n"; $i=$j+0; # $i is now bigint $answer += $i ** $i; }
For the records, I didn't say brute force is the best way to solve an Euler question... ;)
The second part of the answer is that foreach doesn't do a scalar assignment to $i, where bigint could intercept by auto-blessing $i into Math::BigInt. Foreach makes $i an alias of the integer values from the range, such that the **-operator isn't overloaded and produces an overflow.
So the only way to solve this is to make the range operator operating on big-ints on demand.
In reply to Re: c-style for loop versus list for loop, and bigint
by LanX
in thread c-style for loop versus list for loop, and bigint
by AR
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |