Open In App

Duffinian Numbers

Last Updated : 23 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 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




<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>


Output: 

Yes

Time Complexity: O(1)
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads