How to List Integer Only / Returns Integer?

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:

# 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:

# 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

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

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

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
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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	 	 (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.

1 Like

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

1 Like

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

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

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

why should I do this instead of isinteger ?

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

1 Like

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.

1 Like

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

if rem(q1*p2,p1)==0 && rem(q*p1,p)==0
1 Like

Thanks for the advice. 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.

2 Likes

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

Then, how to calculate inaccuracy?

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:

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)

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 :

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

That is just a short way of writing

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.