Duffinian Numbers | Recursive function to return gcd of a and b ; Function to calculate the sum of all divisors of a given number ; Sum of divisors ; Find all divisors of num ; if ' i ' is divisor of ' n ' ; If both divisors are same then add it once ; Add 1 and n to result as above loop considers proper divisors greater than 1. ; Function to check if n is an Duffinian number ; Calculate the sum of divisors ; If number is prime return false ; Find the gcd of n and sum of divisors of n ; Returns true if N and sumDivisors are relatively prime ; Given Number ; Function Call
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 ( " " ) ; } else { document . write ( " " ) ; }