给定数字N ,任务是检查N是否为Droll Number 。如果N是纸卷号码,则打印“是”,否则打印“否” 。
A droll Number is a number such that the sum of even prime divisors of N is equaled to the sum of odd prime divisors of N.
例子:
Input: N = 6272
Output: Yes
Explanation:
6272 = 2*2*2*2*2*2*2*7*7 is droll
since 2+2+2+2+2+2+2 = 14 = 7+7.
Input: N = 10
Output: No
方法:这个想法是找到偶数素数和和奇数素数和,并检查它们是否相等。如果它们相等,则N是纸卷号码,并为其打印“是” ,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check droll numbers
bool isDroll(int n)
{
if (n == 1)
return false;
// To store sum of even prime factors
int sum_even = 0;
// To store sum of odd prime factors
int sum_odd = 0;
// Add the number of 2s
// that divide n in sum_even
while (n % 2 == 0) {
sum_even += 2;
n = n / 2;
}
// N must be odd at this point.
// So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i + 2) {
// While i divides n,
// print i and divide n
while (n % i == 0) {
sum_odd += i;
n = n / i;
}
}
// This condition is to handle
// the case when n is a prime
// number greater than 2
if (n > 2)
sum_odd += n;
// Condition to check droll number
return sum_even == sum_odd;
}
Driver Code int main()
{
// Given Number N
int N = 72;
// Function Call
if (isDroll(N))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to check droll numbers
static boolean isDroll(int n)
{
if (n == 1)
return false;
// To store sum of even prime factors
int sum_even = 0;
// To store sum of odd prime factors
int sum_odd = 0;
// Add the number of 2s
// that divide n in sum_even
while (n % 2 == 0)
{
sum_even += 2;
n = n / 2;
}
// N must be odd at this point.
// So we can skip
// one element (Note i = i +2)
for(int i = 3; i <= Math.sqrt(n);
i = i + 2)
{
// While i divides n,
// print i and divide n
while (n % i == 0)
{
sum_odd += i;
n = n / i;
}
}
// This condition is to handle
// the case when n is a prime
// number greater than 2
if (n > 2)
sum_odd += n;
// Condition to check droll number
return sum_even == sum_odd;
}
// Driver code
public static void main(String[] args)
{
// Given Number N
int n = 72;
// Function Call
if (isDroll(n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Pratima Pandey
Python3
# Python3 program for the above approach
import math;
# Function to check droll numbers
def isDroll(n):
if (n == 1):
return False;
# To store sum of even prime factors
sum_even = 0;
# To store sum of odd prime factors
sum_odd = 0;
# Add the number of 2s
# that divide n in sum_even
while (n % 2 == 0):
sum_even += 2;
n = n // 2;
# N must be odd at this point.
# So we can skip
# one element (Note i = i +2)
for i in range(3, int(math.sqrt(n)) + 1, 2):
# While i divides n,
# print i and divide n
while (n % i == 0):
sum_odd += i;
n = n // i;
# This condition is to handle
# the case when n is a prime
# number greater than 2
if (n > 2):
sum_odd += n;
# Condition to check droll number
return sum_even == sum_odd;
# Driver Code
# Given Number N
N = 72;
# Function Call
if (isDroll(N)):
print("Yes");
else:
print("No");
# This code is contributed by Code_Mech
C#
// C# program for the above approach
using System;
class GFG{
// Function to check droll numbers
static bool isDroll(int n)
{
if (n == 1)
return false;
// To store sum of even prime factors
int sum_even = 0;
// To store sum of odd prime factors
int sum_odd = 0;
// Add the number of 2s
// that divide n in sum_even
while (n % 2 == 0)
{
sum_even += 2;
n = n / 2;
}
// N must be odd at this point.
// So we can skip
// one element (Note i = i +2)
for(int i = 3; i <= Math.Sqrt(n);
i = i + 2)
{
// While i divides n,
// print i and divide n
while (n % i == 0)
{
sum_odd += i;
n = n / i;
}
}
// This condition is to handle
// the case when n is a prime
// number greater than 2
if (n > 2)
sum_odd += n;
// Condition to check droll number
return sum_even == sum_odd;
}
// Driver code
public static void Main()
{
// Given Number N
int n = 72;
// Function Call
if (isDroll(n))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Code_Mech
输出:
Yes
时间复杂度: O(sqrt(N))