# Calculating a large sum of positive and negative integers gives wrong answer

**URL:** <https://discourse.julialang.org/t/calculating-a-large-sum-of-positive-and-negative-integers-gives-wrong-answer/111334>\
**Category:** General Usage\
**Tags:** question, potential-bug\
**Created:** [March 8, 2024, 3:51am UTC](https://discourse.julialang.org/t/calculating-a-large-sum-of-positive-and-negative-integers-gives-wrong-answer/111334 "2024-03-08T03:51:57Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 8, 2024, 9:13am UTC](https://discourse.julialang.org/t/calculating-a-large-sum-of-positive-and-negative-integers-gives-wrong-answer/111334/8 "2024-03-08T09:13:29Z")

</div>

Just a few comments:

> [@SherlockSage](#):
>
> `for p in primes(floor(Int32, sqrt(Int64(max_n))))`

Are you on a 32-bit system, because on mine, there’s no `primes` method for `Int32`:

```julia
julia> primes(Int32(11))
ERROR: MethodError: no method matching primes(::Int32)

Closest candidates are:
  primes(::Int64)
   @ Primes C:\Users\DNF\.julia\packages\Primes\Yo1YT\src\Primes.jl:130
  primes(::Int64, ::Int64)
   @ Primes C:\Users\DNF\.julia\packages\Primes\Yo1YT\src\Primes.jl:114

```

I was wondering why you wrapped your literals like this: `Int64(2)`, since that’s not normally needed.

Secondly, it’s safer to use `isqrt(n)` rather than `floor(Int, sqrt(n))`. I struggled a bit to find an example, but here’s one from an old thread using an `Int128`:

> [@Simple prime number test 1000 slower than trivial python?](https://discourse.julialang.org/t/simple-prime-number-test-1000-slower-than-trivial-python/15504/4):
>
> And secondly: `Int(floor(sqrt(n)))`, gives you the wrong number:
> 
> ```julia
> julia> n = 10000001829379821798314198278324234
> 10000001829379821798314198278324234
> 
> julia> Int(floor(sqrt(n))) # you should rather use floor(Int, sqrt(n)), actually
> 100000009146898688
> 
> julia> isqrt(n) # this is the largest integer m such that m^2 < n
> 100000009146898690
> 
> ```

---

_[View the full topic](https://discourse.julialang.org/t/calculating-a-large-sum-of-positive-and-negative-integers-gives-wrong-answer/111334)._
