Queries to count the number of unordered co-prime pairs from 1 to N
Last Updated :
11 Jul, 2025
Given a number N. The task is to find the number of unordered coprime pairs of integers from 1 to N. There can be multiple queries.
Examples:
Input: 3
Output: 4
(1, 1), (1, 2), (1, 3), (2, 3)
Input: 4
Output: 6
(1, 1), (1, 2), (1, 3), (1, 4), (2, 3), (3, 4)
Approach: Here Euler's Totient Function will be helpful. Euler's totient function denoted as phi(N), is an arithmetic function that counts the positive integers less than or equal to N that are relatively prime to N.
The idea is to use the following properties of Euler Totient function i.e.
- The formula basically says that the value of ?(n) is equal to n multiplied by product of (1 – 1/p) for all prime factors p of n. For example value of ?(6) = 6 * (1-1/2) * (1 – 1/3) = 2.
- For a prime number p, ?(p) is p-1. For example ?(5) is 4, ?(7) is 6 and ?(13) is 12. This is obvious, gcd of all numbers from 1 to p-1 will be 1 because p is a prime.
Now, find the sum of all phi(x) for all i between 1 to N using prefix sum method. Using this, one can answer in o(1) time.
Below is the implementation of above approach.
C++
// C++ program to find number of unordered
// coprime pairs of integers from 1 to N
#include <bits/stdc++.h>
using namespace std;
#define N 100005
// to store euler's totient function
int phi[N];
// to store required answer
int S[N];
// Computes and prints totient of all numbers
// smaller than or equal to N.
void computeTotient()
{
// Initialise the phi[] with 1
for (int i = 1; i < N; i++)
phi[i] = i;
// Compute other Phi values
for (int p = 2; p < N; p++) {
// If phi[p] is not computed already,
// then number p is prime
if (phi[p] == p) {
// Phi of a prime number p is
// always equal to p-1.
phi[p] = p - 1;
// Update phi values of all
// multiples of p
for (int i = 2 * p; i < N; i += p) {
// Add contribution of p to its
// multiple i by multiplying with
// (1 - 1/p)
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
}
// function to compute number coprime pairs
void CoPrimes()
{
// function call to compute
// euler totient function
computeTotient();
// prefix sum of all euler totient function values
for (int i = 1; i < N; i++)
S[i] = S[i - 1] + phi[i];
}
// Driver code
int main()
{
// function call
CoPrimes();
int q[] = { 3, 4 };
int n = sizeof(q) / sizeof(q[0]);
for (int i = 0; i < n; i++)
cout << "Number of unordered coprime\n"
<< "pairs of integers from 1 to "
<< q[i] << " are " << S[q[i]] << endl;
return 0;
}
C
// C program to find number of unordered
// coprime pairs of integers from 1 to N
#include <stdio.h>
#define N 100005
// to store euler's totient function
int phi[N];
// to store required answer
int S[N];
// Computes and prints totient of all numbers
// smaller than or equal to N.
void computeTotient()
{
// Initialise the phi[] with 1
for (int i = 1; i < N; i++)
phi[i] = i;
// Compute other Phi values
for (int p = 2; p < N; p++) {
// If phi[p] is not computed already,
// then number p is prime
if (phi[p] == p) {
// Phi of a prime number p is
// always equal to p-1.
phi[p] = p - 1;
// Update phi values of all
// multiples of p
for (int i = 2 * p; i < N; i += p) {
// Add contribution of p to its
// multiple i by multiplying with
// (1 - 1/p)
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
}
// function to compute number coprime pairs
void CoPrimes()
{
// function call to compute
// euler totient function
computeTotient();
// prefix sum of all euler totient function values
for (int i = 1; i < N; i++)
S[i] = S[i - 1] + phi[i];
}
// Driver code
int main()
{
// function call
CoPrimes();
int q[] = { 3, 4 };
int n = sizeof(q) / sizeof(q[0]);
for (int i = 0; i < n; i++)
printf("Number of unordered coprime\npairs of integers from 1 to %d are %d\n",q[i],S[q[i]]);
return 0;
}
// This code is contributed by kothavvsaakash.
Java
// Java program to find number of unordered
// coprime pairs of integers from 1 to N
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static final int N = 100005;
// to store euler's
// totient function
static int[] phi;
// to store required answer
static int[] S ;
// Computes and prints totient
// of all numbers smaller than
// or equal to N.
static void computeTotient()
{
// Initialise the phi[] with 1
for (int i = 1; i < N; i++)
phi[i] = i;
// Compute other Phi values
for (int p = 2; p < N; p++)
{
// If phi[p] is not computed
// already, then number p is prime
if (phi[p] == p)
{
// Phi of a prime number p
// is always equal to p-1.
phi[p] = p - 1;
// Update phi values of
// all multiples of p
for (int i = 2 * p; i < N; i += p)
{
// Add contribution of p to
// its multiple i by multiplying
// with (1 - 1/p)
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
}
// function to compute
// number coprime pairs
static void CoPrimes()
{
// function call to compute
// euler totient function
computeTotient();
// prefix sum of all euler
// totient function values
for (int i = 1; i < N; i++)
S[i] = S[i - 1] + phi[i];
}
// Driver code
public static void main(String args[])
{
phi = new int[N];
S = new int[N];
// function call
CoPrimes();
int q[] = { 3, 4 };
int n = q.length;
for (int i = 0; i < n; i++)
System.out.println("Number of unordered coprime\n" +
"pairs of integers from 1 to " +
q[i] + " are " + S[q[i]] );
}
}
// This code is contributed
// by Subhadeep
Python 3
# Python3 program to find number
# of unordered coprime pairs of
# integers from 1 to N
N = 100005
# to store euler's totient function
phi = [0] * N
# to store required answer
S = [0] * N
# Computes and prints totient of all
# numbers smaller than or equal to N.
def computeTotient():
# Initialise the phi[] with 1
for i in range(1, N):
phi[i] = i
# Compute other Phi values
for p in range(2, N) :
# If phi[p] is not computed already,
# then number p is prime
if (phi[p] == p) :
# Phi of a prime number p
# is always equal to p-1.
phi[p] = p - 1
# Update phi values of all
# multiples of p
for i in range(2 * p, N, p) :
# Add contribution of p to its
# multiple i by multiplying with
# (1 - 1/p)
phi[i] = (phi[i] // p) * (p - 1)
# function to compute number
# coprime pairs
def CoPrimes():
# function call to compute
# euler totient function
computeTotient()
# prefix sum of all euler
# totient function values
for i in range(1, N):
S[i] = S[i - 1] + phi[i]
# Driver code
if __name__ == "__main__":
# function call
CoPrimes()
q = [ 3, 4 ]
n = len(q)
for i in range(n):
print("Number of unordered coprime\n" +
"pairs of integers from 1 to ",
q[i], " are " , S[q[i]])
# This code is contributed
# by ChitraNayal
C#
// C# program to find number
// of unordered coprime pairs
// of integers from 1 to N
using System;
class GFG
{
static int N = 100005;
// to store euler's
// totient function
static int[] phi;
// to store required answer
static int[] S ;
// Computes and prints totient
// of all numbers smaller than
// or equal to N.
static void computeTotient()
{
// Initialise the phi[] with 1
for (int i = 1; i < N; i++)
phi[i] = i;
// Compute other Phi values
for (int p = 2; p < N; p++)
{
// If phi[p] is not computed
// already, then number p is prime
if (phi[p] == p)
{
// Phi of a prime number p
// is always equal to p-1.
phi[p] = p - 1;
// Update phi values of
// all multiples of p
for (int i = 2 * p;
i < N; i += p)
{
// Add contribution of
// p to its multiple i
// by multiplying
// with (1 - 1/p)
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
}
// function to compute
// number coprime pairs
static void CoPrimes()
{
// function call to compute
// euler totient function
computeTotient();
// prefix sum of all euler
// totient function values
for (int i = 1; i < N; i++)
S[i] = S[i - 1] + phi[i];
}
// Driver code
public static void Main()
{
phi = new int[N];
S = new int[N];
// function call
CoPrimes();
int[] q = { 3, 4 };
int n = q.Length;
for (int i = 0; i < n; i++)
Console.WriteLine("Number of unordered coprime\n" +
"pairs of integers from 1 to " +
q[i] + " are " + S[q[i]] );
}
}
// This code is contributed
// by mits
PHP
<?php
// PHP program to find number
// of unordered coprime pairs
// of integers from 1 to N
$N = 100005;
// to store euler's totient function
$phi = array_fill(0, $N, 0);
// to store required answer
$S = array_fill(0, $N, 0);
// Computes and prints totient
// of all numbers smaller than
// or equal to N.
function computeTotient()
{
global $N, $phi, $S;
// Initialise the phi[] with 1
for ($i = 1; $i < $N; $i++)
$phi[$i] = $i;
// Compute other Phi values
for ($p = 2; $p < $N; $p++)
{
// If phi[p] is not computed
// already, then number p
// is prime
if ($phi[$p] == $p)
{
// Phi of a prime number p
// is always equal to p-1.
$phi[$p] = $p - 1;
// Update phi values of
// all multiples of p
for ($i = 2 * $p;
$i < $N; $i += $p)
{
// Add contribution of p
// to its multiple i by
// multiplying with (1 - 1/p)
$phi[$i] = (int)(($phi[$i] /
$p) * ($p - 1));
}
}
}
}
// function to compute
// number coprime pairs
function CoPrimes()
{
global $N, $phi, $S;
// function call to compute
// euler totient function
computeTotient();
// prefix sum of all euler
// totient function values
for ($i = 1; $i < $N; $i++)
$S[$i] = $S[$i - 1] + $phi[$i];
}
// Driver code
// function call
CoPrimes();
$q = array( 3, 4 );
$n = sizeof($q);
for ($i = 0; $i < $n; $i++)
echo "Number of unordered coprime\n" .
"pairs of integers from 1 to " .
$q[$i] . " are ".$S[$q[$i]]."\n";
// This code is contributed
// by mits
?>
JavaScript
<script>
// Javascript program to find number of unordered
// coprime pairs of integers from 1 to N
let N = 100005;
// to store euler's
// totient function
let phi = new Array(N);
// to store required answer
let S = new Array(N);
for(let i = 0; i < N; i++)
{
phi[i] = 0;
S[i] = 0;
}
// Computes and prints totient
// of all numbers smaller than
// or equal to N.
function computeTotient()
{
// Initialise the phi[] with 1
for (let i = 1; i < N; i++)
phi[i] = i;
// Compute other Phi values
for (let p = 2; p < N; p++)
{
// If phi[p] is not computed
// already, then number p is prime
if (phi[p] == p)
{
// Phi of a prime number p
// is always equal to p-1.
phi[p] = p - 1;
// Update phi values of
// all multiples of p
for (let i = 2 * p; i < N; i += p)
{
// Add contribution of p to
// its multiple i by multiplying
// with (1 - 1/p)
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
}
// function to compute
// number coprime pairs
function CoPrimes()
{
// function call to compute
// euler totient function
computeTotient();
// prefix sum of all euler
// totient function values
for (let i = 1; i < N; i++)
S[i] = S[i - 1] + phi[i];
}
// Driver code
// function call
CoPrimes();
let q = [ 3, 4 ];
let n = q.length;
for (let i = 0; i < n; i++)
document.write("Number of unordered coprime<br>" +
"pairs of integers from 1 to " +
q[i] + " are " + S[q[i]] +"<br>" );
// This code is contributed by avanitrachhadiya2155
</script>
Output: Number of unordered coprime
pairs of integers from 1 to 3 are 4
Number of unordered coprime
pairs of integers from 1 to 4 are 6
Time Complexity: O(n + 1000053/2)
Auxiliary Space: O(100005)
Similar Reads
Euler Totient for Competitive Programming What is Euler Totient function(ETF)?Euler Totient Function or Phi-function for 'n', gives the count of integers in range '1' to 'n' that are co-prime to 'n'. It is denoted by \phi(n) .For example the below table shows the ETF value of first 15 positive integers: 3 Important Properties of Euler Totie
8 min read
Euler's Totient Function Given an integer n, find the value of Euler's Totient Function, denoted as Φ(n). The function Φ(n) represents the count of positive integers less than or equal to n that are relatively prime to n. Euler's Totient function Φ(n) for an input n is the count of numbers in {1, 2, 3, ..., n-1} that are re
10 min read
Count of non co-prime pairs from the range [1, arr[i]] for every array element Given an array arr[] consisting of N integers, the task for every ith element of the array is to find the number of non co-prime pairs from the range [1, arr[i]]. Examples: Input: N = 2, arr[] = {3, 4}Output: 2 4Explanation: All non-co-prime pairs from the range [1, 3] are (2, 2) and (3, 3).All non-
13 min read
Generate an array having sum of Euler Totient Function of all elements equal to N Given a positive integer N, the task is to generate an array such that the sum of the Euler Totient Function of each element is equal to N. Examples: Input: N = 6Output: 1 6 2 3 Input: N = 12Output: 1 12 2 6 3 4 Approach: The given problem can be solved based on the divisor sum property of the Euler
5 min read
Count all possible values of K less than Y such that GCD(X, Y) = GCD(X+K, Y) Given two integers X and Y, the task is to find the number of integers, K, such that gcd(X, Y) is equal to gcd(X+K, Y), where 0 < K <Y. Examples: Input: X = 3, Y = 15Output: 4Explanation: All possible values of K are {0, 3, 6, 9} for which GCD(X, Y) = GCD(X + K, Y). Input: X = 2, Y = 12Output:
8 min read
Count of integers up to N which are non divisors and non coprime with N Given an integer N, the task is to find the count of all possible integers less than N satisfying the following properties: The number is not coprime with N i.e their GCD is greater than 1.The number is not a divisor of N. Examples: Input: N = 10 Output: 3 Explanation: All possible integers which ar
5 min read
Find the number of primitive roots modulo prime Given a prime p . The task is to count all the primitive roots of p .A primitive root is an integer x (1 <= x < p) such that none of the integers x - 1, x2 - 1, ...., xp - 2 - 1 are divisible by p but xp - 1 - 1 is divisible by p . Examples: Input: P = 3 Output: 1 The only primitive root modul
5 min read
Compute power of power k times % m Given x, k and m. Compute (xxxx...k)%m, x is in power k times. Given x is always prime and m is greater than x. Examples: Input : 2 3 3 Output : 1 Explanation : ((2 ^ 2) ^ 2) % 3 = (4 ^ 2) % 3 = 1 Input : 3 2 3 Output : 0 Explanation : (3^3)%3 = 0 A naive approach is to compute the power of x k time
15+ min read
Primitive root of a prime number n modulo n Given a prime number n, the task is to find its primitive root under modulo n. The primitive root of a prime number n is an integer r between[1, n-1] such that the values of r^x(mod n) where x is in the range[0, n-2] are different. Return -1 if n is a non-prime number. Examples: Input : 7 Output : S
15 min read
Euler's Totient function for all numbers smaller than or equal to n Euler's Totient function ?(n) for an input n is the count of numbers in {1, 2, 3, ..., n} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. For example, ?(4) = 2, ?(3) = 2 and ?(5) = 4. There are 2 numbers smaller or equal to 4 that are relatively pri
13 min read