给定数字N ,任务是检查N是否为Giuga数。如果N是一个Giuga数,则打印“是”,否则打印“否” 。
A Giuga Number is a composite number N such that p divides (N/p – 1) for every prime factor p of N.
例子:
Input: N = 30
Output: Yes
Explanation:
30 is a composite number whose prime divisors are {2, 3, 5}, such that
2 divides 30/2 – 1 = 14,
3 divides 30/3 – 1 = 9, and
5 divides 30/5 – 1 = 5.
Input: N = 161
Output: No
方法:想法是检查N是否为整数。如果不是,则打印“否”。
如果N是一个复合数,则找到一个数的质因数,对于每个质数p,检查条件p除(n / p – 1)是否成立。如果上述条件成立,则打印“是”,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if n is
// a composite number
bool isComposite(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return false;
// This is checked to skip
// middle 5 numbers
if (n % 2 == 0 || n % 3 == 0)
return true;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return true;
return false;
}
// Function to check if N is a
// Giuga Number
bool isGiugaNum(int n)
{
// N should be composite to be
// a Giuga Number
if (!(isComposite(n)))
return false;
int N = n;
// Print the number of 2s
// that divide n
while (n % 2 == 0) {
if ((N / 2 - 1) % 2 != 0)
return false;
n = n / 2;
}
// N must be odd at this point.
// So we can skip one element
for (int i = 3; i <= sqrt(n);
i = i + 2) {
// While i divides n,
// print i and divide n
while (n % i == 0) {
if ((N / i - 1) % i != 0)
return false;
n = n / i;
}
}
// This condition is to handle
// the case when n
// is a prime number > 2
if (n > 2)
if ((N / n - 1) % n != 0)
return false;
return true;
}
// Driver Code
int main()
{
// Given Number N
int N = 30;
// Function Call
if (isGiugaNum(N))
cout << "Yes";
else
cout << "No";
}
Java
// Java program for the above approach
class GFG{
// Function to check if n is
// a composite number
static boolean isComposite(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return false;
// This is checked to skip
// middle 5 numbers
if (n % 2 == 0 || n % 3 == 0)
return true;
for(int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return true;
return false;
}
// Function to check if N is a
// Giuga Number
static boolean isGiugaNum(int n)
{
// N should be composite to be
// a Giuga Number
if (!(isComposite(n)))
return false;
int N = n;
// Print the number of 2s
// that divide n
while (n % 2 == 0)
{
if ((N / 2 - 1) % 2 != 0)
return false;
n = n / 2;
}
// N must be odd at this point.
// So we can skip one element
for(int i = 3; i <= Math.sqrt(n);
i = i + 2)
{
// While i divides n,
// print i and divide n
while (n % i == 0)
{
if ((N / i - 1) % i != 0)
return false;
n = n / i;
}
}
// This condition is to handle
// the case when n
// is a prime number > 2
if (n > 2)
if ((N / n - 1) % n != 0)
return false;
return true;
}
// Driver code
public static void main(String[] args)
{
// Given Number N
int n = 30;
// Function Call
if (isGiugaNum(n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Pratima Pandey
Python3
# Python program for the above approach
import math
# Function to check if n is
# a composite number
def isComposite(n):
# Corner cases
if(n <= 1):
return False
if(n <= 3):
return False
# This is checked to skip
# middle 5 numbers
if(n % 2 == 0 or n % 3 == 0):
return True
i = 5
while(i * i <= n):
if(n % i == 0 or n % (i + 2) == 0):
return True
i += 6
return False
# Function to check if N is a
# Giuga Number
def isGiugaNum(n):
# N should be composite to be
# a Giuga Number
if(not(isComposite(n))):
return False
N = n
# Print the number of 2s
# that divide n
while(n % 2 == 0):
if((int(N / 2) - 1) % 2 != 0):
return False
n = int(n / 2)
# N must be odd at this point.
# So we can skip one element
for i in range(3, int(math.sqrt(n)) + 1, 2):
# While i divides n,
# print i and divide n
while(n % i == 0):
if((int(N / i) - 1) % i != 0):
return False
n = int(n / i)
# This condition is to handle
# the case when n
# is a prime number > 2
if(n > 2):
if((int(N / n) - 1) % n != 0):
return False
return True
# Driver code
# Given Number N
n = 30
if(isGiugaNum(n)):
print("Yes")
else:
print("No")
# This code is contributed by avanitrachhadiya2155
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if n is
// a composite number
static bool isComposite(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return false;
// This is checked to skip
// middle 5 numbers
if (n % 2 == 0 || n % 3 == 0)
return true;
for(int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return true;
return false;
}
// Function to check if N is a
// Giuga Number
static bool isGiugaNum(int n)
{
// N should be composite to be
// a Giuga Number
if (!(isComposite(n)))
return false;
int N = n;
// Print the number of 2s
// that divide n
while (n % 2 == 0)
{
if ((N / 2 - 1) % 2 != 0)
return false;
n = n / 2;
}
// N must be odd at this point.
// So we can skip one element
for(int i = 3; i <= Math.Sqrt(n);
i = i + 2)
{
// While i divides n,
// print i and divide n
while (n % i == 0)
{
if ((N / i - 1) % i != 0)
return false;
n = n / i;
}
}
// This condition is to handle
// the case when n
// is a prime number > 2
if (n > 2)
if ((N / n - 1) % n != 0)
return false;
return true;
}
// Driver code
static void Main()
{
// Given Number N
int N = 30;
// Function Call
if (isGiugaNum(N))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by divyeshrabadiya07
输出:
Yes