📜  检查给定整数是否为K个连续整数的乘积

📅  最后修改于: 2021-04-17 18:42:02             🧑  作者: Mango

给定两个正整数NK ,任务是检查给定整数N是否可以表示为K个连续整数的乘积。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

方法:可以通过使用滑动窗口技术解决给定的问题。请按照以下步骤解决问题:

  • 初始化两个整数,例如Kthrootproduct ,以分别存储整数N的K根和K个连续整数的乘积。
  • 将范围为[1,K]的整数乘积存储在变量积中
  • 否则,请在[2,Kthroot]范围内进行迭代并执行以下步骤:
    • 如果乘积的值等于N ,则打印“是”并跳出循环。
    • 将product的值更新为(product *(i + K – 1))/(i – 1)
  • 完成上述步骤后,如果以上情况均不满足,则打印“否”,因为N不能表示为K个连续整数的乘积。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if N can be expressed
// as the product of K consecutive integers
string checkPro(int n, int k)
{
    double exp = 1.0 / k;
     
    // Stores the K-th root of N
    int KthRoot = (int)pow(n, exp);
     
    // Stores the product of K
    // consecutive integers
    int product = 1;
     
    // Traverse over the range [1, K]
    for(int i = 1; i < k + 1; i++)
    {
         
        // Update the product
        product = product * i;
    }
     
    // If product is N, then return "Yes"
    if (product == n)
        return "Yes";
     
    else
    {
         
        // Otherwise, traverse over
        // the range [2, Kthroot]
        for(int j = 2; j < KthRoot + 1; j++)
        {
             
            // Update the value of product
            product = product * (j + k - 1);
            product = product / (j - 1);
             
            // If product is equal to N
            if (product == n)
                return "Yes";
        } 
    }
     
    // Otherwise, return "No"
    return "No";
}
 
// Driver code
int main()
{
    int N = 210;
    int K = 3;
 
    cout << checkPro(N, K);
 
    return 0;
}
 
// This code is contributed by avijitmondal1998


Java
// Java program for the above approach
public class GFG {
 
  // Function to check if N can be expressed
  // as the product of K consecutive integers
  static String checkPro(int n, int k){
 
    double exp = 1.0 / k ;
 
    // Stores the K-th root of N
    int KthRoot = (int)Math.pow(n, exp);
 
    // Stores the product of K
    // consecutive integers
    int product = 1 ;
 
    // Traverse over the range [1, K]
    for (int i = 1; i < k + 1; i++){
      // Update the product
      product = product * i;
    }
 
    // If product is N, then return "Yes"
    if(product == n)
      return "Yes";
 
    else {
      // Otherwise, traverse over
      // the range [2, Kthroot]
      for (int j = 2; j < KthRoot + 1; j++) {
 
        // Update the value of product
        product = product * (j + k - 1) ;
        product = product / (j - 1) ;
 
        // If product is equal to N
        if(product == n)
          return "Yes" ;
      } 
    }
 
    // Otherwise, return "No"
    return "No" ;
  }
 
  // Driver Code
  public static void main (String[] args) {
 
    int N = 210;
    int K = 3;
 
    System.out.println(checkPro(N, K));
  }
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to check if N can be expressed
# as the product of K consecutive integers
def checkPro(n, k):
 
    # Stores the K-th root of N
    KthRoot = int(n**(1 / k))
 
    # Stores the product of K
    # consecutive integers
    product = 1
     
    # Traverse over the range [1, K]
    for i in range(1, k + 1):
       
        # Update the product
        product = product * i
         
    print(product)
    # If product is N, then return "Yes"
    if(product == N):
        return ("Yes")
       
    # Otherwise, traverse over
    # the range [2, Kthroot]
    for i in range(2, KthRoot + 1):
       
        # Update the value of product
        product = product*(i + k-1)
        product = product/(i - 1)
        print(product)
        # If product is equal to N
        if(product == N):
            return ("Yes")
           
    # Otherwise, return "No"
    return ("No")
 
# Driver Code
N = 210
K = 3
 
# Function Call
print(checkPro(N, K))


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if N can be expressed
// as the product of K consecutive integers
static string checkPro(int n, int k)
{
    double exp = 1.0 / k ;
     
    // Stores the K-th root of N
    int KthRoot = (int)Math.Pow(n, exp);
     
    // Stores the product of K
    // consecutive integers
    int product = 1 ;
     
    // Traverse over the range [1, K]
    for(int i = 1; i < k + 1; i++)
    {
         
        // Update the product
        product = product * i;
    }
     
    // If product is N, then return "Yes"
    if (product == n)
        return "Yes";
         
    else
    {
         
        // Otherwise, traverse over
        // the range [2, Kthroot]
        for(int j = 2; j < KthRoot + 1; j++)
        {
             
            // Update the value of product
            product = product * (j + k - 1);
            product = product / (j - 1);
             
            // If product is equal to N
            if (product == n)
                return "Yes";
        } 
    }
     
    // Otherwise, return "No"
    return "No";
}
 
// Driver Code
static public void Main()
{
    int N = 210;
    int K = 3;
 
    Console.WriteLine(checkPro(N, K));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
Yes

时间复杂度: O(K + N (1 / K) )
辅助空间: O(1)