# Sqrt not giving the correct value

**URL:** <https://discourse.julialang.org/t/sqrt-not-giving-the-correct-value/89318>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 26, 2022, 6:32pm UTC](https://discourse.julialang.org/t/sqrt-not-giving-the-correct-value/89318 "2022-10-26T18:32:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giulianno\_G](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giulianno_g/32/43866_2.png) [@Giulianno\_G](https://discourse.julialang.org/u/Giulianno_G)\
**Post date:** [October 26, 2022, 6:32pm UTC](https://discourse.julialang.org/t/sqrt-not-giving-the-correct-value/89318/1 "2022-10-26T18:32:50Z")

</div>

Hello, I’m new to Julia and I was trying to use for Fermat Factoring problem  
I’m having issues with the sqrt not returning the correct value

```julia
function fermat(n::Int64)
    x = isqrt(n); y = 0.1
    while y%1 != 0
        x+=1
        y = sqrt(x^2 - n)
    end
    println((x-y) ,", ", (x+y))
end

```

This code works for small values like 187, and 161423 but when I use n = 171190005457, I was supposed to get 17 and 10070000321. Which I know that there are a few erros, because it doesn’t return me prime numbers at high values.  
I tried calculating the sqrt(5035000169^2 - 171190005457) but it doesn’t return me the correct value.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [October 26, 2022, 6:38pm UTC](https://discourse.julialang.org/t/sqrt-not-giving-the-correct-value/89318/2 "2022-10-26T18:38:14Z")

</div>

It seems like you are getting integer overflow here, maybe try running it with a type supporting larger values such as Int128 or BigInt?

```julia
julia> 5035000169^2 - 171190005457
6904482456930471488

julia> big(5035000169)^2 - 171190005457
25351226530640023104

julia> typemax(Int)
9223372036854775807

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 26, 2022, 6:44pm UTC](https://discourse.julialang.org/t/sqrt-not-giving-the-correct-value/89318/3 "2022-10-26T18:44:04Z")

</div>

in general be very careful when trying to run number theory stuff in languages with machine number types as default (or put it a different way, you don’t have to be as careful when using Mathematica)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 26, 2022, 6:45pm UTC](https://discourse.julialang.org/t/sqrt-not-giving-the-correct-value/89318/4 "2022-10-26T18:45:31Z")

</div>

i.e. you generally need to use `BigInt` types if you want to do number theory.

A few languages do this for you (Mathematica, Maple, and other computer algebra systems, and Python), but you pay a big performance price for this if you are working with ordinary integers (e.g. loop counters, array indices, etcetera), so performance-oriented languages typically require you to use bignums (arbitrary precision arithmetic) explicitly.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 26, 2022, 7:08pm UTC](https://discourse.julialang.org/t/sqrt-not-giving-the-correct-value/89318/5 "2022-10-26T19:08:49Z")

</div>

Note that in addition to the integer overflow issue, floating point precision means that you shouldn’t expect `sqrt` to do what you want. Instead, you should use `y = isqrt(x^2-n)` and check if `y*y == x^2-n`

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 26, 2022, 8:28pm UTC](https://discourse.julialang.org/t/sqrt-not-giving-the-correct-value/89318/6 "2022-10-26T20:28:57Z")

</div>

You can use Int128(171190005457), that type is in Base. I’m not sure it’s good advice since it can overflow too, unlike BigInt, though increasingly rare, with larger types. It’s going to be much faster… and e.g.:

You can also use:

> **[GitHub - rfourquet/BitIntegers.jl: Fixed-width integers similar to builtin ones](https://github.com/rfourquet/BitIntegers.jl)**
>
> Fixed-width integers similar to builtin ones. Contribute to rfourquet/BitIntegers.jl development by creating an account on GitHub.

> `Int1024`, `UInt1024`

You can also use integer types which check for overflow, and/or use those types.

And do:

```julia
function fermat(n::Integer)
[..]
y = isqrt(x^2 - n)

julia> fermat(Int128(171190005457)) # correct answers(?)
412909, 414595

```

Off-topic, googling a bit I found what I didn’t know of (their bold):

> **[Fermat – Computing Architecture for Big Data and AI](https://fermatinternational.com/)**

> Fermat hardware goes beyond Von Neumann architecture to allow **Big Data processing without any bottleneck** […]
> 
> Up to **30x faster**  
> than conventional architectures
