Using Crystal 0.31.1 on Linux.
There seems to be an overflow issue with Array.product
This creates a Int32 value, and works at runtime:
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23]
modpn = primes.product #=> 223_092_870
This produces a runtime overflow because .product
won’t create an Int64.
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
modpn = primes.product #=> should be 6_469_693_230
How do I elegantly make this produce the correct answers?
Some of the following
primes.product(1i64)
primes.map(&.to_i64).product
You could just do [2, 3, 5, 7, 11, 13, 17, 19, 23] of Int64
.
Yes, that did it for that.
Now have to fix other overflows from using that result.
(This is a translation of Ruby code).
OK, I worked out all the type
mismatches and have the whole code working now.
It would be nice to have a tool to check Ruby code to identify potential type
casting issues.
Ruby spoils you for doing math, as you never have to worry about number sizes.