Duffinian Numbers
Last Updated :
23 Mar, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int divSum( int n)
{
int result = 0;
for ( int i = 2; i <= sqrt (n); i++) {
if (n % i == 0) {
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
return (result + n + 1);
}
bool isDuffinian( int n)
{
int sumDivisors = divSum(n);
if (sumDivisors == n + 1)
return false ;
int hcf = __gcd(n, sumDivisors);
return hcf == 1;
}
int main()
{
int n = 36;
if (isDuffinian(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int divSum( int n)
{
int result = 0 ;
for ( int i = 2 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
return (result + n + 1 );
}
static boolean isDuffinian( int n)
{
int sumDivisors = divSum(n);
if (sumDivisors == n + 1 )
return false ;
int hcf = gcd(n, sumDivisors);
return hcf == 1 ;
}
public static void main(String[] args)
{
int n = 36 ;
if (isDuffinian(n))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import math
def divSum(n):
result = 0
for i in range ( 2 , int (math.sqrt(n)) + 1 ):
if (n % i = = 0 ):
if (i = = (n / / i)):
result + = i
else :
result + = (i + n / i)
return (result + n + 1 )
def isDuffinian(n):
sumDivisors = int (divSum(n))
if (sumDivisors = = n + 1 ):
return False
hcf = math.gcd(n, sumDivisors)
return hcf = = 1
n = 36
if (isDuffinian(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int divSum( int n)
{
int result = 0;
for ( int i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
return (result + n + 1);
}
static bool isDuffinian( int n)
{
int sumDivisors = divSum(n);
if (sumDivisors == n + 1)
return false ;
int hcf = gcd(n, sumDivisors);
return hcf == 1;
}
public static void Main( string [] args)
{
int n = 36;
if (isDuffinian(n))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function gcd( a , b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
function divSum( n) {
let result = 0;
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
return (result + n + 1);
}
function isDuffinian( n) {
let sumDivisors = divSum(n);
if (sumDivisors == n + 1)
return false ;
let hcf = gcd(n, sumDivisors);
return hcf == 1;
}
let n = 36;
if (isDuffinian(n)) {
document.write( "Yes" );
} else {
document.write( "No" );
}
</script>
|
Time Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...