Duffinian Numbers
Given a number N, the task is to check if N is an Duffinian Number or not. If N is an Duffinian Number then print “Yes” else print “No”.
Duffinian Number is a composite numbers N such that it is relatively prime to the sum of divisors of N.
Examples:
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.
Approach: The idea is to find the sum of factors of the number N. If the gcd of N and the sum of factors of N are relatively prime then the given number N is a Duffinian Number, else N is not a Duffinian Number.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to calculate the sum of all// divisors of a given numberint 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 numberbool 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 Codeint main(){ // Given Number int n = 36; // Function Call if (isDuffinian(n)) cout << "Yes"; else cout << "No"; return 0;} |
Java
// Java program for the above approachclass GFG{// Recursive function to return// gcd of a and bstatic 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 numberstatic 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 numberstatic 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 codepublic 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 approachimport math# Function to calculate the sum of all# divisors of a given numberdef 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 numberdef 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 numbern = 36# Function callif (isDuffinian(n)): print("Yes")else: print("No")# This code is contributed by sanjoy_62 |
C#
// C# program for the above approachusing System;class GFG{// Recursive function to return// gcd of a and bstatic 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 numberstatic 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 numberstatic 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 codepublic 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
<script>// Javascript program for the above approach // Recursive function to return // gcd of a and b function gcd( a , b) { if (b == 0) return a; return gcd(b, a % b); } // Function to calculate the sum of // all divisors of a given number function divSum( n) { // Sum of divisors let result = 0; // Find all divisors of num for (let 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 function isDuffinian( n) { // Calculate the sum of divisors let 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 let hcf = gcd(n, sumDivisors); // Returns true if N and sumDivisors // are relatively prime return hcf == 1; } // Driver code // Given Number let n = 36; // Function Call if (isDuffinian(n)) { document.write("Yes"); } else { document.write("No"); }// This code contributed by Rajput-Ji</script> |
Yes
Time Complexity: O(1)


