Product of all numbers up to N that are co-prime with N
Last Updated :
04 Dec, 2023
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:
- Initialize the product as 1.
- Iterate over the range [1, N] and if GCD of i and N is 1, multiply product with i.
- After the above step, print the value of the product.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int findProduct(unsigned int N)
{
unsigned int result = 1;
for ( int i = 2; i < N; i++) {
if (gcd(i, N) == 1) {
result *= i;
}
}
return result;
}
int main()
{
int N = 5;
cout << findProduct(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static int findProduct( int N)
{
int result = 1 ;
for ( int i = 2 ; i < N; i++)
{
if (gcd(i, N) == 1 )
{
result *= i;
}
}
return result;
}
public static void main(String[] args)
{
int N = 5 ;
System.out.print(findProduct(N));
}
}
|
Python3
def gcd(a, b):
if (a = = 0 ):
return b;
return gcd(b % a, a);
def findProduct(N):
result = 1 ;
for i in range ( 2 , N):
if (gcd(i, N) = = 1 ):
result * = i;
return result;
if __name__ = = '__main__' :
N = 5 ;
print (findProduct(N));
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int findProduct( int N)
{
int result = 1;
for ( int i = 2; i < N; i++)
{
if (gcd(i, N) == 1)
{
result *= i;
}
}
return result;
}
public static void Main(String[] args)
{
int N = 5;
Console.Write(findProduct(N));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function findProduct( N)
{
var result = 1;
for ( var i = 2; i < N; i++) {
if (gcd(i, N) == 1) {
result *= i;
}
}
return result;
}
var N = 5;
document.write(findProduct(N))
</script>
|
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Approach#2: Using nested while
This approach calculates the product of all numbers up to N that are co-prime with N, using Euler’s totient function. It first factorizes N and then applies the formula for Euler’s totient function to compute the product.
Algorithm
1. Find the Euler totient function value of N, which gives the count of numbers that are co-prime to N.
2. Calculate the product of all integers up to N-1.
C++
#include <iostream>
using namespace std;
int main() {
cout << "GFG!" ;
return 0;
}
|
C++
#include <iostream>
#include <cmath>
int gcd( int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int eulerTotient( int n) {
int phi = n;
int i = 2;
while (i * i <= n) {
if (n % i == 0) {
while (n % i == 0) {
n /= i;
}
phi -= phi / i;
}
i++;
}
if (n > 1) {
phi -= phi / n;
}
return phi;
}
int coPrimeProduct( int N) {
int phi_N = eulerTotient(N);
int product = 1;
for ( int i = 1; i < N; i++) {
if (gcd(i, N) == 1) {
product *= i;
}
}
return product;
}
int main() {
int N = 5;
int result = coPrimeProduct(N);
std::cout << result << std::endl;
return 0;
}
|
Java
public class Main {
static int gcd( int a, int b) {
if (b == 0 ) {
return a;
}
return gcd(b, a % b);
}
static int eulerTotient( int n) {
int phi = n;
int i = 2 ;
while (i * i <= n) {
if (n % i == 0 ) {
while (n % i == 0 ) {
n /= i;
}
phi -= phi / i;
}
i++;
}
if (n > 1 ) {
phi -= phi / n;
}
return phi;
}
static int coPrimeProduct( int N) {
int phi_N = eulerTotient(N);
int product = 1 ;
for ( int i = 1 ; i < N; i++) {
if (gcd(i, N) == 1 ) {
product *= i;
}
}
return product;
}
public static void main(String[] args) {
int N = 5 ;
int result = coPrimeProduct(N);
System.out.println(result);
}
}
|
Python3
from math import gcd
def co_prime_product_3(N):
def euler_totient(n):
phi = n
i = 2
while i * i < = n:
if n % i = = 0 :
while n % i = = 0 :
n / / = i
phi - = phi / / i
i + = 1
if n > 1 :
phi - = phi / / n
return phi
phi_N = euler_totient(N)
product = 1
for i in range ( 1 , N):
if gcd(i, N) = = 1 :
product * = i
return product
N = 5
print (co_prime_product_3(N))
|
C#
using System;
class MainClass {
static int Gcd( int a, int b)
{
if (b == 0) {
return a;
}
return Gcd(b, a % b);
}
static int EulerTotient( int n)
{
int phi = n;
int i = 2;
while (i * i <= n) {
if (n % i == 0) {
while (n % i == 0) {
n /= i;
}
phi -= phi / i;
}
i++;
}
if (n > 1) {
phi -= phi / n;
}
return phi;
}
static int CoPrimeProduct( int N)
{
int product = 1;
for ( int i = 1; i < N; i++) {
if (Gcd(i, N) == 1) {
product *= i;
}
}
return product;
}
public static void Main( string [] args)
{
int N = 5;
int result = CoPrimeProduct(
N);
Console.WriteLine( "Co-Prime Product for N = " + N
+ ": "
+ result);
}
}
|
Javascript
function gcd(a, b) {
if (b === 0) {
return a;
}
return gcd(b, a % b);
}
function eulerTotient(n) {
let phi = n;
let i = 2;
while (i * i <= n) {
if (n % i === 0) {
while (n % i === 0) {
n /= i;
}
phi -= Math.floor(phi / i);
}
i++;
}
if (n > 1) {
phi -= Math.floor(phi / n);
}
return phi;
}
function coPrimeProduct(N) {
let phi_N = eulerTotient(N);
let product = 1;
for (let i = 1; i < N; i++) {
if (gcd(i, N) === 1) {
product *= i;
}
}
return product;
}
function main() {
let N = 5;
let result = coPrimeProduct(N);
console.log(result);
}
main();
|
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...