# \[SOLVED\] Overflow error

**URL:** <https://forum.crystal-lang.org/t/solved-overflow-error/1888>\
**Category:** Help & Support\
**Created:** [April 1, 2020, 5:38pm UTC](https://forum.crystal-lang.org/t/solved-overflow-error/1888 "2020-04-01T17:38:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevensonmt](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/stevensonmt/32/771_2.png) [@stevensonmt](https://forum.crystal-lang.org/u/stevensonmt)\
**Post date:** [April 1, 2020, 5:38pm UTC](https://forum.crystal-lang.org/t/solved-overflow-error/1888/1 "2020-04-01T17:38:04Z")

</div>

I’m getting an overflow error that I can’t figure out a way out of.  
Code should be [here](https://play.crystal-lang.org/#/r/8td3).  
The problematic portion appears to be this call in the sieve function:

```auto
.each { |i| 
        (i*i..limit).step(2*i).each { |j| prime_indices[j] = false }
    }

```

I don’t fully understand the error message, which on my local machine is

> Arithmetic overflow (OverflowError)  
> from src/prime\_factors.cr:0:43 in ‘-\>’  
> from /usr/lib/crystal/iterator.cr:255:3 in ‘next’  
> from src/prime\_factors.cr:489:15 in ‘sieve’  
> from src/prime\_factors.cr:266:5 in ‘factors’  
> from spec/prime\_factors\_spec.cr:30:49 in ‘-\>’  
> from /usr/lib/crystal/spec/example.cr:255:3 in ‘internal\_run’  
> from /usr/lib/crystal/spec/example.cr:35:16 in ‘run’  
> from /usr/lib/crystal/spec/context.cr:296:23 in ‘internal\_run’  
> from /usr/lib/crystal/spec/context.cr:291:7 in ‘run’  
> from /usr/lib/crystal/spec/context.cr:48:23 in ‘run’  
> from /usr/lib/crystal/spec/dsl.cr:270:7 in ‘-\>’  
> from /usr/lib/crystal/kernel.cr:255:3 in ‘run’  
> from /usr/lib/crystal/crystal/main.cr:47:5 in ‘main’  
> from /usr/lib/crystal/crystal/main.cr:106:3 in ‘main’  
> from \_\_libc\_start\_main  
> from \_start  
> from ???

How do I interpret the error message to figure out the necessary change to the sieve function?

+++++++++++++++++++

Figured it out. It was the `i*i` call that was potentially overflowing. instead of checking `i*i < limit` I needed to check `i < Math.sqrt(limit)`.
