给定数字“ N”。检查“ N”的阶乘是否可被第一个“ N”个自然数的和整除?如果可以分开,则打印“是”,否则打印“否”。
例子:
Input: N = 3
Output: YES
As (1*2*3)%(1+2+3) = 0,
Hence divisibility is possible.
Input: N = 4
Output: NO
Here (1*2*3*4)%(1+2+3+4) != 0,
Hence divisibility doesn't occur.
方法:
- 第一个’n’个自然数的总和:s =(n)*(n + 1)/ 2。这可以表示为(n + 1)!/ 2 *(n-1)!
- 现在n!/ s = 2 *(n-1)!/(n + 1)。
- 从上面的公式中,观察结果推导为:
- 如果“ n + 1”是质数,则“ n!”不能被前n个自然数的和除。
- 如果“ n + 1”不是素数,则“ n!”可被第一个“ n”个自然数之和整除。
例如:
- 令n = 4。
- 因此,“ n!/ s” = 2 *(3!)/ 5。 = 1 * 2 * 3 * 2/5。
- 在这里n!为了被“ s”整除,我们需要分子中至少存在“ 5”的倍数,即在给定的示例中分子表示为3的乘积!和2,整个产品都可以被“ 5”整除
至少应有5的倍数,即5 * 1或5 * 2或5 * 3,依此类推。由于在阶乘项中,存在的最高数字为’n-1’,因此如果’n + 1’为质数,则分子将永远无法以’n + 1’项表示。因此,不可分割性是不可能的。 - 在任何其他情况下,无论“ n + 1”是偶数还是奇数,但不是“素数”,除数始终是可能的。
注意:在n = 1的情况下要格外小心。作为1!总是可以被1整除。
下面是上述方法的实现:
C++
#include
using namespace std;
// Function to check whether
// a number is prime or not.
bool is_prime(int num)
{
// Count variable to store
// the number of factors of 'num'
int count = 0;
// Counting the number of factors
for (int i = 1; i * i <= (num); i++) {
if ((num) % i == 0) {
if (i * i != (num))
count += 2;
else
count++;
}
}
// If number is prime return true
if (count == 2)
return true;
else
return false;
}
// Function to check for divisibility
string is_divisible(int n)
{
// if 'n' equals 1 then divisibility is possible
if (n == 1) {
return "YES";
}
// Else check whether 'n+1' is prime or not
else {
// If 'n+1' is prime then 'n!' is
// not divisible by 'n*(n+1)/2'
if (is_prime(n + 1))
return "NO";
// else divisibility occurs
else
return "YES";
}
}
// Driver Code
int main()
{
int n;
// Test for n=3
n = 3;
cout << is_divisible(n) << endl;
// Test for n=4
n = 4;
cout << is_divisible(n) << endl;
return 0;
}
Java
class GfG
{
// Function to check whether
// a number is prime or not.
static boolean is_prime(int num)
{
// Count variable to store
// the number of factors of 'num'
int count = 0;
// Counting the number of factors
for (int i = 1; i * i <= (num); i++)
{
if ((num) % i == 0)
{
if (i * i != (num))
count += 2;
else
count++;
}
}
// If number is prime return true
if (count == 2)
return true;
else
return false;
}
// Function to check for divisibility
static String is_divisible(int n)
{
// if 'n' equals 1 then divisibility is possible
if (n == 1)
{
return "YES";
}
// Else check whether 'n+1' is prime or not
else
{
// If 'n+1' is prime then 'n!' is
// not divisible by 'n*(n+1)/2'
if (is_prime(n + 1))
return "NO";
// else divisibility occurs
else
return "YES";
}
}
// Driver Code
public static void main(String[] args)
{
int n;
// Test for n=3
n = 3;
System.out.println(is_divisible(n));
// Test for n=4
n = 4;
System.out.println(is_divisible(n));
}
}
// This code is contributed by Prerna Saini
Python3
# Function to check whether
# a number is prime or not.
def is_prime(num):
# Count variable to store
# the number of factors of 'num'
count = 0
# Counting the number of factors
for i in range(1, num + 1):
if i * i > num:
break
if ((num) % i == 0):
if (i * i != (num)):
count += 2
else:
count += 1
# If number is prime return true
if (count == 2):
return True
else:
return False
# Function to check for divisibility
def is_divisible(n):
# if 'n' equals 1 then
# divisibility is possible
if (n == 1):
return "YES"
# Else check whether 'n+1' is prime or not
else:
# If 'n+1' is prime then 'n!' is
# not divisible by 'n*(n+1)/2'
if (is_prime(n + 1)):
return "NO"
# else divisibility occurs
else:
return "YES"
# Driver Code
# Test for n=3
n = 3
print(is_divisible(n))
# Test for n=4
n = 4
print(is_divisible(n))
# This code is contributed
# by mohit kumar
C#
// C# implement the approach
class GfG
{
// Function to check whether
// a number is prime or not.
static bool is_prime(int num)
{
// Count variable to store
// the number of factors of 'num'
int count = 0;
// Counting the number of factors
for (int i = 1; i * i <= (num); i++)
{
if ((num) % i == 0)
{
if (i * i != (num))
count += 2;
else
count++;
}
}
// If number is prime return true
if (count == 2)
return true;
else
return false;
}
// Function to check for divisibility
static string is_divisible(int n)
{
// if 'n' equals 1 then divisibility is possible
if (n == 1)
{
return "YES";
}
// Else check whether 'n+1' is prime or not
else
{
// If 'n+1' is prime then 'n!' is
// not divisible by 'n*(n+1)/2'
if (is_prime(n + 1))
return "NO";
// else divisibility occurs
else
return "YES";
}
}
// Driver Code
static void Main()
{
int n;
// Test for n=3
n = 3;
System.Console.WriteLine(is_divisible(n));
// Test for n=4
n = 4;
System.Console.WriteLine(is_divisible(n));
}
}
// This code is contributed by mits
PHP
Javascript
输出:
YES
NO