As you've found, Seqs cannot contain more than Int.MaxValue elements. Until this feature is fixed, don't use a Seq. You can
1) use a while-loop
2) use a for-loop without a sequence
but with these ways you can't use the methods of Scala collections like foldLeft
in your example.
So what you need is an Iterator
. e.g.
def bigIterator(start: BigInt, end: BigInt, step: BigInt = 1) =
Iterator.iterate(start)(_ + step).takeWhile(_ <= end)
then
bigIterator(0, BigInt("3000000000")).foldLeft(BigInt(0))(_ + _)
etc will work. Note: if you don't need the full range of BigInt
, use Long
instead as it's significantly faster.