Skip to content
Related Articles

Related Articles

Product of all numbers up to N that are co-prime with N
  • Last Updated : 17 Mar, 2021

Given an integer N, the task is to find the product of all the numbers from the range [1, N] that are co-prime to the given number N.

Examples:

Input: N = 5
Output: 24
Explanation:
Numbers which are co-prime with 5 are {1, 2, 3, 4}.
Therefore, the product is given by 1 * 2 * 3 * 4 = 24.

Input: N = 6
Output: 5
Explanation:
Numbers which are co-prime to 6 are {1, 5}.
Therefore, the required product is equal to 1 * 5 = 5

Approach: The idea is to iterate over the range [1, N] and for every number, check if its GCD with N is equal to 1 or not. If found to be true for any number, then include that number in the resultant product. 
Follow the steps below to solve the problem:



  1. Initialize the product as 1.
  2. Iterate over the range [1, N] and if GCD of i and N is 1, multiply product with i.
  3. After the above step, print the value of the product.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <iostream>
using namespace std;
 
// Function to return gcd of a and b
int gcd(int a, int b)
{
    // Base Case
    if (a == 0)
        return b;
 
    // Recursive GCD
    return gcd(b % a, a);
}
 
// Function to find the product of
// all the numbers till N that are
// relatively prime to N
int findProduct(unsigned int N)
{
    // Stores the resultant product
    unsigned int result = 1;
 
    // Iterate over [2, N]
    for (int i = 2; i < N; i++) {
 
        // If gcd is 1, then find the
        // product with result
        if (gcd(i, N) == 1) {
            result *= i;
        }
 
        
    }
   // Return the final product
        return result;
}
 
// Driver Code
int main()
{
    int N = 5;
 
    cout << findProduct(N);
    return 0;
}

Java




// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function to return
// gcd of a and b
static int gcd(int a, int b)
{
  // Base Case
  if (a == 0)
    return b;
 
  // Recursive GCD
  return gcd(b % a, a);
}
 
// Function to find the
// product of all the
// numbers till N that are
// relatively prime to N
static int findProduct(int N)
{
  // Stores the resultant
  // product
  int result = 1;
 
  // Iterate over [2, N]
  for (int i = 2; i < N; i++)
  {
    // If gcd is 1, then
    // find the product
    // with result
    if (gcd(i, N) == 1)
    {
      result *= i;
    }
  }
   
  // Return the final
  // product
  return result;
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 5;
  System.out.print(findProduct(N));
}
}
 
// This code is contributed by Rajput-Ji

Python3




# Python3 program for the
# above approach
 
# Function to return
# gcd of a and b
def gcd(a, b):
   
    # Base Case
    if (a == 0):
        return b;
 
    # Recursive GCD
    return gcd(b % a, a);
 
# Function to find the
# product of all the
# numbers till N that are
# relatively prime to N
def findProduct(N):
   
    # Stores the resultant
    # product
    result = 1;
 
    # Iterate over [2, N]
    for i in range(2, N):
       
        # If gcd is 1, then
        # find the product
        # with result
        if (gcd(i, N) == 1):
            result *= i;
 
    # Return the final
    # product
    return result;
 
# Driver Code
if __name__ == '__main__':
   
    N = 5;
    print(findProduct(N));
 
# This code is contributed by 29AjayKumar

C#




// C# program for the
// above approach
using System;
 
class GFG{
 
// Function to return
// gcd of a and b
static int gcd(int a, int b)
{
   
  // Base Case
  if (a == 0)
    return b;
 
  // Recursive GCD
  return gcd(b % a, a);
}
 
// Function to find the
// product of all the
// numbers till N that are
// relatively prime to N
static int findProduct(int N)
{
   
  // Stores the resultant
  // product
  int result = 1;
 
  // Iterate over [2, N]
  for(int i = 2; i < N; i++)
  {
     
    // If gcd is 1, then
    // find the product
    // with result
    if (gcd(i, N) == 1)
    {
      result *= i;
    }
  }
   
  // Return the readonly
  // product
  return result;
}
 
// Driver Code
public static void Main(String[] args)
{
  int N = 5;
   
  Console.Write(findProduct(N));
}
}
 
// This code is contributed by Amit Katiyar

Javascript




<script>
 
 
// Javascript program for the above approach
 
// Function to return gcd of a and b
function gcd(a, b)
{
    // Base Case
    if (a == 0)
        return b;
 
    // Recursive GCD
    return gcd(b % a, a);
}
 
// Function to find the product of
// all the numbers till N that are
// relatively prime to N
function findProduct( N)
{
    // Stores the resultant product
    var result = 1;
 
    // Iterate over [2, N]
    for (var i = 2; i < N; i++) {
 
        // If gcd is 1, then find the
        // product with result
        if (gcd(i, N) == 1) {
            result *= i;
        }
        
    }
   // Return the final product
        return result;
}
 
// Driver Code
var N = 5;
document.write(findProduct(N))
 
</script>
Output
24

Time Complexity: O(N log N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :