I want to add up all numbers from 1 to 1000000.
range = 1..1000000
println range.each { it * it }.sum()
As a result I get 1784293664 instead of 500000500000. I assume, I need a BigInteger. But when I try
range = 1..1000000
println range.each { it * it }.sum().toBigInteger()
nothing changes.
Where is my mistake?
Your range is of type
IntRange. Here’s a correct one: