7
$\begingroup$

Problem Statement:

Given N you are to count the number of pairwise coprime triples which satisfy $1≤a,b,c≤N$.

Example:

For example N=3, valid triples are (1,1,1),(1,1,2),(1,2,1),(2,1,1),(1,1,3),(1,3,1),(3,1,1),(1,2,3),(1,3,2),(2,1,3),(2,3,1),(3,2,1),(3,1,2)

Hence answer for N=3 is 13.

Source: own problem.

Solution so far:

I have found the oeis series A256390 but Reyna & Heyman's work seems too overwhelming for me to understand.If someone can explain that in simple terms with an example it would be great or if you have an alternate solution you are welcome too.

I have found a solution. I will move the following to answer once I have better understanding. For now documenting my understandings here:

I found a code (modified) This is a deterministic approach and had nothing to do with Reyna & Heyman's approximation. For N=6 answer should be 64 .

Now let's see his implementation. He's constructing graph with just 7 edges

enter image description here

I:mu[1] * f[1] * f[1] * f[1]=216
I:mu[2] * f[2] * f[2] * f[2]=-27
I:mu[3] * f[3] * f[3] * f[3]=-8
I:mu[4] * f[4] * f[4] * f[4]=0
I:mu[5] * f[5] * f[5] * f[5]=-1
I:mu[6] * f[6] * f[6] * f[6]=1
3B:(mu[1] * f[2] + mu[2] * f[1]) * f[2] * f[2]=-27
3B:(mu[1] * f[3] + mu[3] * f[1]) * f[3] * f[3]=-16
3B:(mu[1] * f[5] + mu[5] * f[1]) * f[5] * f[5]=-5
3B:(mu[1] * f[6] + mu[6] * f[1]) * f[6] * f[6]=7
3B:(mu[2] * f[3] + mu[3] * f[2]) * f[6] * f[6]=-5
3B:(mu[2] * f[6] + mu[6] * f[2]) * f[6] * f[6]=2
3B:(mu[3] * f[6] + mu[6] * f[3]) * f[6] * f[6]=1
6A:mu[3] * mu[2] * mu[1] * 1 * 3 * mark[1]=6
6A:mu[6] * mu[2] * mu[1] * 1 * 3 * mark[1]=-3
6A:mu[6] * mu[3] * mu[1] * 1 * 2 * mark[1]=-2
6A:mu[6] * mu[3] * mu[2] * 1 * 1 * mark[2]=1

Ans=(216-27-8+0-1+1)+3*(-27-16-5+7-5+2+1)+6*(6-3-2+1)=64

gcd(a,b,c)=1 Case:

I have labelled 3 sections of the program with I:,3B:,6A: tags. For gcd(a,b,c)=1 problem I: tags are after which we stop. We don't need 3B: or 6A: tags. We neither need to construct the graph nor traverse it. And it indeed clocks less than a sec for n=1000000.

But for pairwise gcd=1 problem we need to exclude/include more scenarios.So lets proceed.

gcd(a,b)>1 or gcd(b,c)>1 or gcd(c,a)>1 Case:

For each of the items labelled 3B: he adds an edge from first mu node to first f node with weight equal to either of first f[i] value outside the first pathesis. enter image description here

in 3B:(mu[1] * f[2] + mu[2] * f[1]) * f[2] * f[2] (mu[1] * f[2] + mu[2] * f[1]) part is for selecting an item in a column which is not multiple of 2.wile the other two columns have multiple of 2.3 in 3B is C(3 1) for permuting this column either at start , middle or end.

Essentially for all pair of factors of i we draw edge from smaller factor to larger factor with weight f[i].If the edge already exist we skip that. For 6 all pair of factors are:

1,2  =>already edge exists with weight f[2] so will skip
1,3  =>already edge exists with weight f[3] so will skip
1,6  =>draw edge with weight f[6]
2,3  =>draw edge with weight f[6]
2,6  =>draw edge with weight f[6]
3,6  =>draw edge with weight f[6]

Tiangle counting:

Note Neither for I: labeled values nor for 3B: labeled values the graph is required. The graph is being constructed for 6A: values

mark[c] denotes wheather it has been traversed earlier as first distance. if yes it has value equal to weight of edge used to traverse to it. enter image description here

Before traversing node a, for each edge incident on a we mark the source node in by weight and clear it once traversing a case is complete. So mark[c] to have a valid value node x has to be node a. Hence a triangle.

enter image description here

w1 * w2 * mark[c] is equivalent to w1 * w2 * wc

The overall Time complexity = O(n+E +E^1.5)

=$O(n+nlogn+(nlogn)^{1.5})$

for n=1000000, its around = $O(n+10n+430n)$ = $4*10^8$

Since median of factors of a number is its square root we managed to get a sparse graph, so time complexity is order of $(nlogn)^{1.5}$ . If graph was dense ie $E=n^2$ then triangle finding is $O(n^3)$ when we arrive at complexity similar to @mike-earnest 's answer. Conversely if you substitute $n^{0.5}$ for $n$ in @mike-earnest 's answer you get complexity similar to this.

$\endgroup$
2
  • $\begingroup$ The formula on OEIS seems to be a little bit broken. Possibly the issue is that we're using the wrong upper bound on $a,b,c$ in the sum, but I don't see a choice for that upper bound that gives the right answer, and I don't think the sum converges without an upper bound. $\endgroup$ Commented Jun 17, 2022 at 14:46
  • 1
    $\begingroup$ @misha-lavrov Eric Towers tried to answer it as a part of little different question here . He is also talking about constructing graph. His graph is quite trivial which doesn't decrease time complexity unlike the one which Reyna & Heyman are talking about . If you have gone through the paper let me know if you understand how they are constructing the graph. $\endgroup$
    – user1068604
    Commented Jun 17, 2022 at 15:23

1 Answer 1

2
$\begingroup$

The OEIS formula is mistaken. To fix it, replace all instances of gcd with lcm. This code prints 13 when run.

from math import gcd

def lcm(a,b):
    return a * b // gcd(a, b)

mu = [0, 1, -1, -1]

N = 3    
accum = 0
for a in range(1, N + 1):
    for b in range(1, N + 1):
        for c in range(1, N + 1):
            accum += (
                  mu[a] 
                * mu[b] 
                * mu[c] 
                * (N // lcm(a, b))
                * (N // lcm(a, c))
                * (N // lcm(b, c))
                )
print('number of triples = ', accum)
$\endgroup$
9
  • $\begingroup$ Thanks mike . Did you get a chance to look at more efficent ways to compute ie Reyna & Heyman's paper linked there. I am here more for that. I can't make much of it no matter how hard I try. $\endgroup$
    – user1068604
    Commented Jun 17, 2022 at 19:32
  • $\begingroup$ @MasrukaJannat What precisely are you looking for in an answer? Code to compute the number of triples? I do not understand the paper, but I can tell the paper is not helpful for computing the number of triples. $\endgroup$ Commented Jun 17, 2022 at 19:47
  • $\begingroup$ I need to compute for n of order of 1 million . So this wont suffice. this is O(n^3*log(n)). probably its possible something like O(n^2*log(n)) or maybe better if we go by that paper. $\endgroup$
    – user1068604
    Commented Jun 17, 2022 at 19:51
  • 1
    $\begingroup$ I do not see how you are getting any sort of time estimate from the paper. You understand that in this problem, the graph would have three vertices, with three edges between every pair (because we require (a,b), (b,c), and (a,c) to all be coprime), right? Therefore, any graph you build will take constant time. Furthermore, it seems like the paper is computing nothing exactly; everything has an error term. $\endgroup$ Commented Jun 17, 2022 at 21:29
  • 1
    $\begingroup$ @MasrukaJannat In the discussion you linked, they are not claiming they can count pairwise coprime triples for $N=10^6$ in under a second. Anyway, I think I am not able to help you, but I hope you can find your algorithm. $\endgroup$ Commented Jun 18, 2022 at 0:55

You must log in to answer this question.