Dado un número N , la tarea es verificar si N es un Número Duffiniano o no. Si N es un número de Duffinian, escriba «Sí» , de lo contrario, escriba «No» .
Número de Duffinian es un número compuesto N tal que es relativamente primo a la suma de los divisores de N.
Ejemplos:
Entrada: N = 35
Salida: Sí
Explicación:
Suma de divisores de 35 = 1 + 5 + 7 + 35 = 48,
y 48 es primo relativo a 35.
Entrada: N = 28
Salida: No
Explicación:
Suma de divisores de 28 = 1 + 2 + 4 + 7 + 14 + 28 = 56,
y 56 no es primo relativo a 28.
Planteamiento: La idea es encontrar la suma de factores del número N . Si el gcd de N y la suma de los factores de N son primos relativos, entonces el número dado N es un número de Duffin , de lo contrario, N no es un número de Duffin .
A continuación se muestra la implementación del enfoque anterior:
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>
Yes
Complejidad de tiempo: O(1)