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.

  1. 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.
  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;
}

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

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
?>

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


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.





Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.






User Actions