📌  相关文章
📜  与N互质的最多N的所有数字的乘积

📅  最后修改于: 2021-04-24 19:50:57             🧑  作者: Mango

给定一个整数N ,任务是找到从互斥素到给定数字N的范围[1,N]的所有数字的乘积。

例子:

方法:想法是迭代[1,N]范围,对于每个数字,检查其N的GCD是否等于1 。如果发现对于任何数字都是正确的,则在所得产品中包括该数字。
请按照以下步骤解决问题:

  1. 产品初始化为1
  2. 遍历范围[1,N],如果GCDn1,多层产品其中i。
  3. 上述步骤后,打印产品的价值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


输出
24

时间复杂度: O(N log N)
辅助空间: O(1)