给定一个正整数N,则任务是找到事实(N)是否是由总和整除(N),其中的事实(N)是阶乘的N和总和(N)= 1 2 + 2 2 + 3 2 + … + N 2 。
例子:
Input: N = 5
Output: No
fact(N) = 120, sum(N) = 55
And, 120 is not divisible by 55
Input: N = 7
Output: Yes
方法:
- 在这里重要的是要首先实现所有数字平方和的封闭公式。前N个自然数的平方和。
- 现在,由于n是N阶乘和求和的公共因子,我们可以将其删除。
- 现在,对于值(N +1)*(2N +1)中的每个素数P,说值中有X个P因子,然后找到因式(N – 1)中P的因子个数,说它们是Y。如果Y
- 要计算P因数(N)中的因数,我们可以简单地使用Lengendre公式。
- 在第4点中,将质数2、3的计数加1以在求和公式中占6。
- 单独检查“值”中的所有素数P,如果都满足条件3,则答案为“是”。
- 第2点将帮助我们将时间复杂度降低N倍。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
#define ll long long int
using namespace std;
// Function to count number of times
// prime P divide factorial N
bool checkfact(int N, int countprime, int prime)
{
int countfact = 0;
if (prime == 2 || prime == 3)
countfact++;
int divide = prime;
// Lengendre Formula
while (N / divide != 0) {
countfact += N / divide;
divide = divide * divide;
}
if (countfact >= countprime)
return true;
else
return false;
}
// Function to find count number of times
// all prime P divide summation
bool check(int N)
{
// Formula for summation of square after removing n
// and constant 6
int sumsquares = (N + 1) * (2 * N + 1);
int countprime = 0;
// Loop to traverse over all prime P which divide
// summation
for (int i = 2; i <= sqrt(sumsquares); i++) {
int flag = 0;
while (sumsquares % i == 0) {
flag = 1;
countprime++;
sumsquares /= i;
}
if (flag) {
if (!checkfact(N - 1, countprime, i))
return false;
countprime = 0;
}
}
// If Number itself is a Prime Number
if (sumsquares != 1)
if (!checkfact(N - 1, 1, sumsquares))
return false;
return true;
}
// Driver Code
int main()
{
int N = 5;
if (check(N))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation of the approach
class GfG
{
// Function to count number of times
// prime P divide factorial N
static boolean checkfact(int N, int countprime,
int prime)
{
int countfact = 0;
if (prime == 2 || prime == 3)
countfact++;
int divide = prime;
// Lengendre Formula
while (N / divide != 0)
{
countfact += N / divide;
divide = divide * divide;
}
if (countfact >= countprime)
return true;
else
return false;
}
// Function to find count number of times
// all prime P divide summation
static boolean check(int N)
{
// Formula for summation of square after removing n
// and constant 6
int sumsquares = (N + 1) * (2 * N + 1);
int countprime = 0;
// Loop to traverse over all prime P which divide
// summation
for (int i = 2; i <= Math.sqrt(sumsquares); i++)
{
int flag = 0;
while (sumsquares % i == 0)
{
flag = 1;
countprime++;
sumsquares /= i;
}
if (flag == 1)
{
if (!checkfact(N - 1, countprime, i))
return false;
countprime = 0;
}
}
// If Number itself is a Prime Number
if (sumsquares != 1)
if (!checkfact(N - 1, 1, sumsquares))
return false;
return true;
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
if (check(N))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Prerna Saini
Python3
# Python 3 implementation of the approach
from math import sqrt
# Function to count number of times
# prime P divide factorial N
def checkfact(N, countprime, prime):
countfact = 0
if (prime == 2 or prime == 3):
countfact += 1
divide = prime
# Lengendre Formula
while (int(N / divide ) != 0):
countfact += int(N / divide)
divide = divide * divide
if (countfact >= countprime):
return True
else:
return False
# Function to find count number of times
# all prime P divide summation
def check(N):
# Formula for summation of square after
# removing n and constant 6
sumsquares = (N + 1) * (2 * N + 1)
countprime = 0
# Loop to traverse over all prime P
# which divide summation
for i in range(2, int(sqrt(sumsquares)) + 1, 1):
flag = 0
while (sumsquares % i == 0):
flag = 1
countprime += 1
sumsquares /= i
if (flag):
if (checkfact(N - 1,
countprime, i) == False):
return False
countprime = 0
# If Number itself is a Prime Number
if (sumsquares != 1):
if (checkfact(N - 1, 1,
sumsquares) == False):
return False
return True
# Driver Code
if __name__ == '__main__':
N = 5
if(check(N)):
print("Yes")
else:
print("No")
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to count number of times
// prime P divide factorial N
static bool checkfact(int N, int countprime,
int prime)
{
int countfact = 0;
if (prime == 2 || prime == 3)
countfact++;
int divide = prime;
// Lengendre Formula
while (N / divide != 0)
{
countfact += N / divide;
divide = divide * divide;
}
if (countfact >= countprime)
return true;
else
return false;
}
// Function to find count number of times
// all prime P divide summation
static bool check(int N)
{
// Formula for summation of square
// after removing n and constant 6
int sumsquares = (N + 1) * (2 * N + 1);
int countprime = 0;
// Loop to traverse over all prime P
// which divide summation
for (int i = 2; i <= Math.Sqrt(sumsquares); i++)
{
int flag = 0;
while (sumsquares % i == 0)
{
flag = 1;
countprime++;
sumsquares /= i;
}
if (flag == 1)
{
if (!checkfact(N - 1, countprime, i))
return false;
countprime = 0;
}
}
// If Number itself is a Prime Number
if (sumsquares != 1)
if (!checkfact(N - 1, 1, sumsquares))
return false;
return true;
}
// Driver Code
public static void Main()
{
int N = 5;
if (check(N))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed
// by Akanksha Rai
PHP
= $countprime)
return true;
else
return false;
}
// Function to find count number of times
// all prime P divide summation
function check($N)
{
// Formula for summation of square
// after removing n and constant 6
$sumsquares = ($N + 1) * (2 * $N + 1);
$countprime = 0;
// Loop to traverse over all prime P
// which divide summation
for ($i = 2; $i <= sqrt($sumsquares); $i++)
{
$flag = 0;
while ($sumsquares % $i == 0)
{
$flag = 1;
$countprime++;
$sumsquares = (int)($sumsquares / $i);
}
if ($flag == 1)
{
if (checkfact($N - 1, $countprime, $i))
return false;
$countprime = 0;
}
}
// If Number itself is a Prime Number
if ($sumsquares != 1)
if (checkfact($N - 1, 1, $sumsquares))
return false;
return true;
}
// Driver Code
$N = 5;
if (check($N))
echo("Yes");
else
echo("No");
// This code is contributed by Code_Mech
?>
Javascript
输出:
No