# Overflow problem using ary.product

**URL:** <https://forum.crystal-lang.org/t/overflow-problem-using-ary-product/1305>\
**Category:** Help & Support\
**Created:** [October 30, 2019, 8:16pm UTC](https://forum.crystal-lang.org/t/overflow-problem-using-ary-product/1305 "2019-10-30T20:16:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [October 30, 2019, 8:16pm UTC](https://forum.crystal-lang.org/t/overflow-problem-using-ary-product/1305/1 "2019-10-30T20:16:52Z")

</div>

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:

```auto
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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [October 30, 2019, 8:34pm UTC](https://forum.crystal-lang.org/t/overflow-problem-using-ary-product/1305/2 "2019-10-30T20:34:58Z")

</div>

Some of the following

```crystal
primes.product(1i64)
primes.map(&.to_i64).product

```

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [October 30, 2019, 8:38pm UTC](https://forum.crystal-lang.org/t/overflow-problem-using-ary-product/1305/3 "2019-10-30T20:38:53Z")

</div>

You could just do `[2, 3, 5, 7, 11, 13, 17, 19, 23] of Int64`.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [October 30, 2019, 8:53pm UTC](https://forum.crystal-lang.org/t/overflow-problem-using-ary-product/1305/4 "2019-10-30T20:53:15Z")

</div>

Yes, that did it for that.  
Now have to fix other overflows from using that result.  
(This is a translation of Ruby code).

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [October 30, 2019, 9:06pm UTC](https://forum.crystal-lang.org/t/overflow-problem-using-ary-product/1305/5 "2019-10-30T21:06:10Z")

</div>

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.
