# Exact sqrt in Julia?

**URL:** <https://discourse.julialang.org/t/exact-sqrt-in-julia/45140>\
**Category:** General Usage\
**Tags:** question, precision\
**Created:** [August 18, 2020, 2:57am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140 "2020-08-18T02:57:15Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [August 18, 2020, 2:57am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/1 "2020-08-18T02:57:16Z")

</div>

I want to check if an expression evaluates to Integer result. I know this is never the best solution, but I ran out of ideas and not able to come up with an algorithm to solve the problem in a reasonable time. The only way I have now is bruteforce. I used `setprecision` with `big` without getting the correct results, then I tried `SymPy`. The latter calculated the result correctly but is too slow to solve the full-size problem. Any ideas, thank you.

```julia
function sLength(w,v,y) 
    x = w * y // (v + w)
    s = sqrt(x^2 + w^2) + sqrt((y-x)^2 + v^2)
    s == trunc(s)
end 

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [August 18, 2020, 3:07am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/2 "2020-08-18T03:07:03Z")

</div>

`isinteger(2.5)` `isinteger(2.0)` but you can run into precisions issues.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [August 18, 2020, 3:08am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/3 "2020-08-18T03:08:14Z")

</div>

reminds me of a project euler problem

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [August 18, 2020, 3:14am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/4 "2020-08-18T03:14:35Z")

</div>

Not exactly, but yes, it is part of a bigger problem that I have mathematically reduced to this formula but can’t go further. I always find it fun to tackle a hard problem and reduce it to a simple solution.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [August 18, 2020, 3:18am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/5 "2020-08-18T03:18:07Z")

</div>

`isinteger` is nice but the problem with precision remains.

---

<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:** [August 18, 2020, 3:19am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/6 "2020-08-18T03:19:18Z")

</div>

Does [Integer square root - Wikipedia](https://en.wikipedia.org/wiki/Integer_square_root) help at all?

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [August 18, 2020, 3:21am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/7 "2020-08-18T03:21:45Z")

</div>

Julia already has a `isqrt()`, but the problem here is the sum of the two roots is to be integer, not each one individually.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [August 18, 2020, 3:24am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/8 "2020-08-18T03:24:09Z")

</div>

> [@Seif\_Shebl](#):
>
> `sqrt(x^2 + w^2) + sqrt((y-x)^2 + v^2)`

`(x^2 + w^2)` this should be a fraction and `(y-x)^2 + v^2` should be a fraction.

I think you can check the `numerator(fraction1)` and `denominator(fraction2)` and work ou if the whole should be frac

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [August 18, 2020, 3:56am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/9 "2020-08-18T03:56:02Z")

</div>

Yes, it works. Thank you.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [August 18, 2020, 5:37am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/10 "2020-08-18T05:37:06Z")

</div>

What are the signs of the numbers ? If `v` and `w` are of the same sign, then `s == sqrt(y^2 + (v+w)^2)`. The problem is then whether `y` and `v+w` are members of a pythagorean triple. (Assuming `y` and `v+w` are integers)

If `v` and `w` are of opposite signs, and `v + w != 0`, then  
`s == sqrt(y^2 + (v+w)^2) * abs((v-w)/(v+w))`

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [August 18, 2020, 6:02am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/11 "2020-08-18T06:02:41Z")

</div>

Yes, all numbers are positive and I reached the same conclusion as you did. Only a small problem remains; how can I generate the triples incrementally? That’s, normally loops finish the last index first, I want all indices to grow together, one after another in an alternating way. Probably I want a priority queue or something?

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [August 18, 2020, 6:53am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/12 "2020-08-18T06:53:52Z")

</div>

Well, I did it like this:

```julia
for k = 1:M
    for j = 1:k 
        for i = 1:j  
            use indices (i,j,k)
            ... 
        end 
    end     
end

```

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [August 18, 2020, 7:04am UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/13 "2020-08-18T07:04:37Z")

</div>

If your goal is to generate all triples that satisfy the condition, then the fastest way would be to first search for y and z where `sqrt(y^2 + z^2)` is integer, and then generate all `(v,w)` that satisfy `v + w == z`.

Something like

```julia
for y = 1:M
    for z = 1:y
        if y^2 + z^2 is a square
            for v = 1:z-1
                w = z - v
                output(y, v, w)
            end
        end
    end
end

```

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [August 18, 2020, 2:52pm UTC](https://discourse.julialang.org/t/exact-sqrt-in-julia/45140/14 "2020-08-18T14:52:09Z")

</div>

> [@Seif\_Shebl](#):
>
> how can I generate the triples incrementally?

> **[Formulas for generating Pythagorean triples](https://en.wikipedia.org/wiki/Formulas_for_generating_Pythagorean_triples)**
>
> Besides Euclid's formula, many other formulas for generating Pythagorean triples have been developed.
>  
> Euclid's, Pythagoras' and Plato's formulas for calculating triples have been described here: .mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em} The methods below appear in various sources, often without attribution as to their ...

Then, as Per says, you have to partition one of the triples as `v + w`
