给定数字N ,任务是检查N是否为达芬尼数。如果N是达芬尼数字,则打印“是”,否则打印“否” 。
Duffinian Number is a composite numbers N such that it is relatively prime to the sum of divisors of N.
例子:
Input: N = 35
Output: Yes
Explanation:
Sum of divisors of 35 = 1 + 5 + 7 + 35 = 48,
and 48 is relatively prime to 35.
Input: N = 28
Output: No
Explanation:
Sum of divisors of 28 = 1 + 2 + 4 + 7 + 14 + 28 = 56,
and 56 is not relatively prime to 28.
方法:这个想法是找到数量为N的因子之和。如果N的最大公约数和N个因素的总和相对素数,那么在给定数量N是Duffinian号码,否则N是不是Duffinian号码。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate the sum of all
// divisors of a given number
int divSum(int n)
{
// Sum of divisors
int result = 0;
// Find all divisors of num
for (int i = 2; i <= sqrt(n); i++) {
// if 'i' is divisor of 'n'
if (n % i == 0) {
// If both divisors are same
// then add it once
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
// Add 1 and n to result as above
// loop considers proper divisors
// greater than 1.
return (result + n + 1);
}
// Function to check if n is an
// Duffinian number
bool isDuffinian(int n)
{
// Calculate the sum of divisors
int sumDivisors = divSum(n);
// If number is prime return false
if (sumDivisors == n + 1)
return false;
// Find the gcd of n and sum of
// divisors of n
int hcf = __gcd(n, sumDivisors);
// Returns true if N and sumDivisors
// are relatively prime
return hcf == 1;
}
// Driver Code
int main()
{
// Given Number
int n = 36;
// Function Call
if (isDuffinian(n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for the above approach
class GFG{
// Recursive function to return
// gcd of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to calculate the sum of
// all divisors of a given number
static int divSum(int n)
{
// Sum of divisors
int result = 0;
// Find all divisors of num
for(int i = 2; i <= Math.sqrt(n); i++)
{
// if 'i' is divisor of 'n'
if (n % i == 0)
{
// If both divisors are same
// then add it once
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
// Add 1 and n to result as above
// loop considers proper divisors
// greater than 1.
return (result + n + 1);
}
// Function to check if n is an
// Duffinian number
static boolean isDuffinian(int n)
{
// Calculate the sum of divisors
int sumDivisors = divSum(n);
// If number is prime return false
if (sumDivisors == n + 1)
return false;
// Find the gcd of n and sum of
// divisors of n
int hcf = gcd(n, sumDivisors);
// Returns true if N and sumDivisors
// are relatively prime
return hcf == 1;
}
// Driver code
public static void main(String[] args)
{
// Given Number
int n = 36;
// Function Call
if (isDuffinian(n))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
// This code is contributed by shubham
Python3
# Python3 program for the above approach
import math
# Function to calculate the sum of all
# divisors of a given number
def divSum(n):
# Sum of divisors
result = 0
# Find all divisors of num
for i in range(2, int(math.sqrt(n)) + 1):
# If 'i' is divisor of 'n'
if (n % i == 0):
# If both divisors are same
# then add it once
if (i == (n // i)):
result += i
else:
result += (i + n / i)
# Add 1 and n to result as above
# loop considers proper divisors
# greater than 1.
return (result + n + 1)
# Function to check if n is an
# Duffinian number
def isDuffinian(n):
# Calculate the sum of divisors
sumDivisors = int(divSum(n))
# If number is prime return false
if (sumDivisors == n + 1):
return False
# Find the gcd of n and sum of
# divisors of n
hcf = math.gcd(n, sumDivisors)
# Returns true if N and sumDivisors
# are relatively prime
return hcf == 1
# Driver Code
# Given number
n = 36
# Function call
if (isDuffinian(n)):
print("Yes")
else:
print("No")
# This code is contributed by sanjoy_62
C#
// C# program for the above approach
using System;
class GFG{
// Recursive function to return
// gcd of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to calculate the sum of
// all divisors of a given number
static int divSum(int n)
{
// Sum of divisors
int result = 0;
// Find all divisors of num
for(int i = 2; i <= Math.Sqrt(n); i++)
{
// If 'i' is divisor of 'n'
if (n % i == 0)
{
// If both divisors are same
// then add it once
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
// Add 1 and n to result as above
// loop considers proper divisors
// greater than 1.
return (result + n + 1);
}
// Function to check if n is an
// Duffinian number
static bool isDuffinian(int n)
{
// Calculate the sum of divisors
int sumDivisors = divSum(n);
// If number is prime return false
if (sumDivisors == n + 1)
return false;
// Find the gcd of n and sum of
// divisors of n
int hcf = gcd(n, sumDivisors);
// Returns true if N and sumDivisors
// are relatively prime
return hcf == 1;
}
// Driver code
public static void Main(string[] args)
{
// Given number
int n = 36;
// Function call
if (isDuffinian(n))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
}
// This code is contributed by rock_cool
Javascript
输出:
Yes
时间复杂度: O(1)