# How to List Integer Only / Returns Integer?

**URL:** <https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 27, 2022, 1:58pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432 "2022-08-27T13:58:30Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [August 27, 2022, 1:58pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/1 "2022-08-27T13:58:30Z")

</div>

Hi all,

I have this simple array (p,q) and want to calculate (p1,q1) and (p2,q2) by calculating (p1,q1) first then (p2,q2). The rule is simple pq’ = qp’

I try to create a simple looping here:

```julia
# Operations on Rational Numbers for equivalence relation pq' = qp'

p=2
q=5

println("i \t chosen pair \t ordered pair-1 ordered pair-2")
for i = 1:10
    p1 = p + i
    q1 = (q*p1)/p
    for j = 1:10
    p2 = p1 + i
    q2 = (q1*p2)/p1   
    println("$i \t ($p,$q) \t \t ($p1,$q1) \t \t ($p2,$q2)")
    end
end

```

The problem is:  
**I only want to return integer** , and my looping is not accurate, I want to list all the possible triplets such as this:

`(2,5)≈(6,15)≈(−4,−10)`

I try to use `if` but it is not working to returns integer:

```julia
# Operations on Rational Numbers for equivalence relation pq' = qp'

p=2
q=5

println("i \t chosen pair \t ordered pair-1 ordered pair-2 ")
for i = 1:10
    p1 = p + i
    q1 = (q*p1)/p
    for j = 1:10
    p2 = p1 + i
    q2 = (q1*p2)/p1   
        if typeof(p1 && q1 && p2 && q2 == Int64)    
        println("$i \t ($p,$q) \t \t ($p1,$q1) \t \t ($p2,$q2)")
        end
    end
end

```

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [August 27, 2022, 2:09pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/2 "2022-08-27T14:09:51Z")

</div>

Is the problem that you expect `/` will perform integer division, but it uses floating point instead? If so, try `fld`.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [August 27, 2022, 2:11pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/3 "2022-08-27T14:11:44Z")

</div>

Because of the divisions, the integers are converted to floats.  
You can check if a number is an integer by `isinteger`.

So if you replace your loop like this it should work

```julia
for i = 1:10
    p1 = p + i
    q1 = (q*p1)/p
    for j = 1:10
        p2 = p1 + i
        q2 = (q1*p2)/p1   
        if all(isinteger.((p1,q1,p2,q2)))
            println("$i \t ($p,$q) \t \t ($p1,$q1) \t \t ($p2,$q2)")
        end
    end
end

```

> **Output**
>
> ```julia
> 2 (2,5) (4,10.0) (6,15.0)
> 2 (2,5) (4,10.0) (6,15.0)
> 2 (2,5) (4,10.0) (6,15.0)
> 2 (2,5) (4,10.0) (6,15.0)
> 2 (2,5) (4,10.0) (6,15.0)
> 2 (2,5) (4,10.0) (6,15.0)
> 2 (2,5) (4,10.0) (6,15.0)
> 2 (2,5) (4,10.0) (6,15.0)
> 2 (2,5) (4,10.0) (6,15.0)
> 2 (2,5) (4,10.0) (6,15.0)
> 4 (2,5) (6,15.0) (10,25.0)
> 4 (2,5) (6,15.0) (10,25.0)
> 4 (2,5) (6,15.0) (10,25.0)
> 4 (2,5) (6,15.0) (10,25.0)
> 4 (2,5) (6,15.0) (10,25.0)
> 4 (2,5) (6,15.0) (10,25.0)
> 4 (2,5) (6,15.0) (10,25.0)
> 4 (2,5) (6,15.0) (10,25.0)
> 4 (2,5) (6,15.0) (10,25.0)
> 4 (2,5) (6,15.0) (10,25.0)
> 6 (2,5) (8,20.0) (14,35.0)
> 6 (2,5) (8,20.0) (14,35.0)
> 6 (2,5) (8,20.0) (14,35.0)
> 6 (2,5) (8,20.0) (14,35.0)
> 6 (2,5) (8,20.0) (14,35.0)
> 6 (2,5) (8,20.0) (14,35.0)
> 6 (2,5) (8,20.0) (14,35.0)
> 6 (2,5) (8,20.0) (14,35.0)
> 6 (2,5) (8,20.0) (14,35.0)
> 6 (2,5) (8,20.0) (14,35.0)
> 8 (2,5) (10,25.0) (18,45.0)
> 8 (2,5) (10,25.0) (18,45.0)
> 8 (2,5) (10,25.0) (18,45.0)
> 8 (2,5) (10,25.0) (18,45.0)
> 8 (2,5) (10,25.0) (18,45.0)
> 8 (2,5) (10,25.0) (18,45.0)
> 8 (2,5) (10,25.0) (18,45.0)
> 8 (2,5) (10,25.0) (18,45.0)
> 8 (2,5) (10,25.0) (18,45.0)
> 8 (2,5) (10,25.0) (18,45.0)
> 10 (2,5) (12,30.0) (22,55.0)
> 10 (2,5) (12,30.0) (22,55.0)
> 10 (2,5) (12,30.0) (22,55.0)
> 10 (2,5) (12,30.0) (22,55.0)
> 10 (2,5) (12,30.0) (22,55.0)
> 10 (2,5) (12,30.0) (22,55.0)
> 10 (2,5) (12,30.0) (22,55.0)
> 10 (2,5) (12,30.0) (22,55.0)
> 10 (2,5) (12,30.0) (22,55.0)
> 10 (2,5) (12,30.0) (22,55.0)
> 
> ```

Also, `j` is not used in the code, so maybe there is a typo.

---

<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:** [August 27, 2022, 2:18pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/4 "2022-08-27T14:18:39Z")

</div>

> [@feanor12](#):
>
> isinteger

Don’t do this. Do proper integer division with `div` (or `fld`).

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [August 27, 2022, 2:19pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/5 "2022-08-27T14:19:20Z")

</div>

It is great but the looping repeating the same ordered triplets such as:

`(2,5) ≈ (6,15) ≈ (10,25)`

 ![Capture d’écran_2022-08-27_21-17-05](https://global.discourse-cdn.com/julialang/original/3X/b/a/ba48b7650146827d7228cdd2032b584036b6bb21.png)

How to make it only printed once, maybe use `if` function?

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [August 27, 2022, 2:19pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/6 "2022-08-27T14:19:52Z")

</div>

why should I do this instead of `isinteger` ?

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [August 27, 2022, 2:20pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/7 "2022-08-27T14:20:28Z")

</div>

I think in the second loop you should add `j` instead of `i`, then every line is different.

---

<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:** [August 27, 2022, 2:24pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/8 "2022-08-27T14:24:36Z")

</div>

Well, maybe I should read your code more carefully to understand what you are doing. But since you are working with integers, you should use proper integer arithmetic. Use `div` and `rem` for dividing integers and for checking if one integer is divisible by another.

Float operations have rounding errors, and is just basically the wrong domain.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [August 27, 2022, 2:25pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/9 "2022-08-27T14:25:22Z")

</div>

For the check, you could also use `rem` to see if there is no remainder, like @DNF mentioned.

```julia
if rem(q1*p2,p1)==0 && rem(q*p1,p)==0

```

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [August 27, 2022, 2:25pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/10 "2022-08-27T14:25:39Z")

</div>

Thanks for the advice. `isinteger` is working great now.

---

<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:** [August 27, 2022, 2:32pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/11 "2022-08-27T14:32:30Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> `isinteger` is working great now.

Well, I am warning you against it. It will be slow, and it could be inaccurate for large integers.

And it’s just generally not the correct approach. I advise you to learn about integer arithmetic. It is powerful and extremely efficient.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [August 27, 2022, 2:42pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/12 "2022-08-27T14:42:24Z")

</div>

> [@DNF](#):
>
> . It will be slow, and it could be inaccurate for large integers.

How to calculate the time? I try to use `@btime` and it is not working…

Then, how to calculate inaccuracy?

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 27, 2022, 2:57pm UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/13 "2022-08-27T14:57:11Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> I want to list all the possible triplets such as this:
> 
> `(2,5)≈(6,15)≈(−4,−10)`

There are an infinite number of them. How many and which ones do you wish to print?

For eg. this will print several of such triplets:

```juila
julia> let (p, q) = (2, 5), i = 1
         for coeff1 in -5:5
           coeff1 in (0, 1) && continue
           for coeff2 in coeff1+1:5
             coeff2 in (0, 1) && continue
             println("$i \t ($p, $q) \t $(coeff1.*(p, q)) \t $(coeff2.*(p, q))")
             i += 1
           end
         end
       end
1 (2, 5) (-10, -25) (-8, -20)
2 (2, 5) (-10, -25) (-6, -15)
3 (2, 5) (-10, -25) (-4, -10)
.
.
.
34 (2, 5) (6, 15) (8, 20)
35 (2, 5) (6, 15) (10, 25)
36 (2, 5) (8, 20) (10, 25)

```

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [August 28, 2022, 5:38am UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/14 "2022-08-28T05:38:53Z")

</div>

Around 30 list of triplets (the chosen pair (p,q) and the other two that you have calculated ) is enough. Wow you can find negative ordered pairs, although I didn’t get your code :

```julia
for coeff1 in -5:5
           coeff1 in (0, 1) && continue

```

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 28, 2022, 5:56am UTC](https://discourse.julialang.org/t/how-to-list-integer-only-returns-integer/86432/15 "2022-08-28T05:56:51Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> ```julia
> for coeff1 in -5:5
> coeff1 in (0, 1) && continue
> 
> ```

That is just a short way of writing

```julia
for coeff1 in -5:5
   if coeff1 in (0, 1)
      continue
   end 

```

i.e. we’ll be multiplying `p` and `q` by this `coeff1`, but if `coeff1` is `1` then we’ll just get the same original `p` and `q`, and if `coeff1` is `0` then the result will be `(0, 0)` which we don’t want. So we skip both of those values by checking whether `coeff1 in (0, 1)`, and if it is we continue with the next iteration of the loop.
