in reply to c-style for loop versus list for loop, and bigint

IIRC the range operator works internally only on unsigned integers, i.e. 32 bits or 64 bits depending on your compilation. I suppose bigint doesn't influence it.

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

UPDATE:

A two part answer to your question:

Part1:

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... ;)

Part2:

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.