📌  相关文章
📜  检查前N个自然数的乘积是否可被其和整除

📅  最后修改于: 2021-04-27 17:57:48             🧑  作者: Mango

给定整数N ,任务是检查前N个自然数的乘积是否可被前N个自然数的总和整除。
例子:

天真的方法:找到前N个自然数的和与乘积,并检查乘积是否可除。
高效方法:我们知道,前N个自然的和与乘积为sum =(N *(N + 1))/ 2乘积= N!分别。现在要检查乘积是否可被总和整除,我们需要检查以下方程式的余数是否为0。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true if n is prime
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
 
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
 
    return true;
}
 
// Function that return true if the product
// of the first n natural numbers is divisible
// by the sum of first n natural numbers
bool isDivisible(int n)
{
    if (isPrime(n + 1))
        return false;
    return true;
}
 
// Driver code
int main()
{
    int n = 6;
    if (isDivisible(n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
// Function that returns true if n is prime
static boolean isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
 
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
 
    return true;
}
 
// Function that return true if the product
// of the first n natural numbers is divisible
// by the sum of first n natural numbers
static boolean isDivisible(int n)
{
    if (isPrime(n + 1))
        return false;
    return true;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 6;
    if (isDivisible(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by Code_Mech.


Python3
# Python 3 implementation of the approach
from math import sqrt
 
# Function that returns true if n is prime
def isPrime(n):
     
    # Corner cases
    if (n <= 1):
        return False
    if (n <= 3):
        return True
 
    # This is checked so that we can skip
    # middle five numbers in below loop
    if (n % 2 == 0 and n % 3 == 0):
        return False
 
    for i in range(5, int(sqrt(n)) + 1, 6):
        if (n % i == 0 and n % (i + 2) == 0):
            return False
 
    return True
 
# Function that return true if the product
# of the first n natural numbers is divisible
# by the sum of first n natural numbers
def isDivisible(n):
    if (isPrime(n + 1)):
        return False
    return True
 
# Driver code
if __name__ == '__main__':
    n = 6
    if (isDivisible(n)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function that returns true if n is prime
static bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
 
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
 
    return true;
}
 
// Function that return true if the product
// of the first n natural numbers is divisible
// by the sum of first n natural numbers
static bool isDivisible(int n)
{
    if (isPrime(n + 1))
        return false;
    return true;
}
 
// Driver code
static void Main()
{
    int n = 6;
    if (isDivisible(n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
 
}
}
 
// This code is contributed by mits


PHP


Javascript


输出:
No