Queries to count the number of unordered co-prime pairs from 1 to N
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++
#include <bits/stdc++.h>
using namespace std;
#define N 100005
int phi[N];
int S[N];
void computeTotient()
{
for ( int i = 1; i < N; i++)
phi[i] = i;
for ( int p = 2; p < N; p++) {
if (phi[p] == p) {
phi[p] = p - 1;
for ( int i = 2 * p; i < N; i += p) {
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
}
void CoPrimes()
{
computeTotient();
for ( int i = 1; i < N; i++)
S[i] = S[i - 1] + phi[i];
}
int main()
{
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
#include <stdio.h>
#define N 100005
int phi[N];
int S[N];
void computeTotient()
{
for ( int i = 1; i < N; i++)
phi[i] = i;
for ( int p = 2; p < N; p++) {
if (phi[p] == p) {
phi[p] = p - 1;
for ( int i = 2 * p; i < N; i += p) {
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
}
void CoPrimes()
{
computeTotient();
for ( int i = 1; i < N; i++)
S[i] = S[i - 1] + phi[i];
}
int main()
{
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;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static final int N = 100005 ;
static int [] phi;
static int [] S ;
static void computeTotient()
{
for ( int i = 1 ; i < N; i++)
phi[i] = i;
for ( int p = 2 ; p < N; p++)
{
if (phi[p] == p)
{
phi[p] = p - 1 ;
for ( int i = 2 * p; i < N; i += p)
{
phi[i] = (phi[i] / p) * (p - 1 );
}
}
}
}
static void CoPrimes()
{
computeTotient();
for ( int i = 1 ; i < N; i++)
S[i] = S[i - 1 ] + phi[i];
}
public static void main(String args[])
{
phi = new int [N];
S = new int [N];
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]] );
}
}
|
Python 3
N = 100005
phi = [ 0 ] * N
S = [ 0 ] * N
def computeTotient():
for i in range ( 1 , N):
phi[i] = i
for p in range ( 2 , N) :
if (phi[p] = = p) :
phi[p] = p - 1
for i in range ( 2 * p, N, p) :
phi[i] = (phi[i] / / p) * (p - 1 )
def CoPrimes():
computeTotient()
for i in range ( 1 , N):
S[i] = S[i - 1 ] + phi[i]
if __name__ = = "__main__" :
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]])
|
C#
using System;
class GFG
{
static int N = 100005;
static int [] phi;
static int [] S ;
static void computeTotient()
{
for ( int i = 1; i < N; i++)
phi[i] = i;
for ( int p = 2; p < N; p++)
{
if (phi[p] == p)
{
phi[p] = p - 1;
for ( int i = 2 * p;
i < N; i += p)
{
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
}
static void CoPrimes()
{
computeTotient();
for ( int i = 1; i < N; i++)
S[i] = S[i - 1] + phi[i];
}
public static void Main()
{
phi = new int [N];
S = new int [N];
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]] );
}
}
|
PHP
<?php
$N = 100005;
$phi = array_fill (0, $N , 0);
$S = array_fill (0, $N , 0);
function computeTotient()
{
global $N , $phi , $S ;
for ( $i = 1; $i < $N ; $i ++)
$phi [ $i ] = $i ;
for ( $p = 2; $p < $N ; $p ++)
{
if ( $phi [ $p ] == $p )
{
$phi [ $p ] = $p - 1;
for ( $i = 2 * $p ;
$i < $N ; $i += $p )
{
$phi [ $i ] = (int)(( $phi [ $i ] /
$p ) * ( $p - 1));
}
}
}
}
function CoPrimes()
{
global $N , $phi , $S ;
computeTotient();
for ( $i = 1; $i < $N ; $i ++)
$S [ $i ] = $S [ $i - 1] + $phi [ $i ];
}
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" ;
?>
|
Javascript
<script>
let N = 100005;
let phi = new Array(N);
let S = new Array(N);
for (let i = 0; i < N; i++)
{
phi[i] = 0;
S[i] = 0;
}
function computeTotient()
{
for (let i = 1; i < N; i++)
phi[i] = i;
for (let p = 2; p < N; p++)
{
if (phi[p] == p)
{
phi[p] = p - 1;
for (let i = 2 * p; i < N; i += p)
{
phi[i] = (phi[i] / p) * (p - 1);
}
}
}
}
function CoPrimes()
{
computeTotient();
for (let i = 1; i < N; i++)
S[i] = S[i - 1] + phi[i];
}
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>" );
</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)
Last Updated :
01 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...