Dado un entero n. Comprueba si el número n es un número superperfecto o no. Un número superperfecto es un entero positivo que satisface σ 2 (n) = σ(σ(n)) = 2n, donde σ es la función sumatoria del divisor .
Input: n = 16 Output: yes Explanation: 16 is a superperfect number as σ(16) = 1 + 2 + 4 + 8 + 16 = 31, and σ(31) = 1 + 31 = 32, thus σ(σ(16)) = 32 = 2 × 16. Input: n = 8 Output: no Explanation: σ(8) = 1 + 2 + 4 + 8 = 15 and σ(15) = 1 + 3 + 5 + 15 = 24 thus ( σ(σ(8)) = 24 ) ≠ (2 * 8 = 26)
La idea es sencillamente sencilla. Simplemente iteramos de 1 a sqrt (n) y encontramos la suma de todos los divisores de n, llamemos a esta suma como n1. Ahora nuevamente necesitamos iterar de 1 a sqrt(n1) y encontrar la suma de todos los divisores. Después de eso, solo tenemos que comprobar si la suma resultante es igual a 2*n o no.
C++
// C++ program to check whether number is // superperfect or not #include<bits/stdc++.h> using namespace std; // Function to calculate sum of all divisors int divSum(int num) { // Final result of summation of divisors int result = 0; // find all divisors which divides 'num' for (int i=1; i*i <= num; ++i) { // if 'i' is divisor of 'num' if (num%i == 0) { // if both divisors are same then add // it only once else add both if (i == (num/i)) result += i; else result += (i + num/i); } } return result; } // Returns true if n is Super Perfect else false. bool isSuperPerfect(int n) { // Find the sum of all divisors of number n int n1 = divSum(n); // Again find the sum of all divisors of n1 // and check if sum is equal to n1 return (2*n == divSum(n1)); } //Driver code int main() { int n = 16; cout << (isSuperPerfect(n) ? "Yes\n" : "No\n"); n = 6; cout << (isSuperPerfect(n) ? "Yes\n" : "No\n"); return 0; }
Java
// Java program to check whether number is // superperfect or not public class Divisors { // Function to calculate sum of all divisors static int divSum(int num) { // Final result of summation of divisors int result = 0; // find all divisors which divides 'num' for (int i=1; i*i <= num; ++i) { // if 'i' is divisor of 'num' if (num%i == 0) { // if both divisors are same then add // it only once else add both if (i == (num/i)) result += i; else result += (i + num/i); } } return result; } // Returns true if n is Super Perfect else false. static boolean isSuperPerfect(int n) { // Find the sum of all divisors of number n int n1 = divSum(n); // Again find the sum of all divisors of n1 // and check if sum is equal to n1 return (2*n == divSum(n1)); } public static void main (String[] args) { int n = 16; System.out.printf((isSuperPerfect(n) ? "Yes\n" : "No\n")); n = 6; System.out.printf((isSuperPerfect(n) ? "Yes\n" : "No\n")); } } // This code is contributed by Saket Kumar
Python3
# Python program to check whether number # is superperfect or not import math # Function to calculate sum of all divisors def divSum(num): # Final result of summation of divisors result = 0 # find all divisors which divides 'num' sq = int(math.sqrt(num)) for i in range(1, sq+1): # if 'i' is divisor of 'num' if num %i == 0: # if both divisors are same then add # it only once else add both if i == (num//i): result += i else: result += (i + num//i) return result # Returns true if n is superperfect else false def isSuperPerfect(n): # Find the sum of all divisors of number n n1 = divSum(n) # Again find the sum of all divisors of n1 return divSum(n1) == 2*n #Driver code n = 16 print ('Yes' if isSuperPerfect(n) else 'No') n = 6 print ('Yes' if isSuperPerfect(n) else 'No')
C#
// C# program to check whether number is // superperfect or not using System; class Divisors { // Function to calculate sum of all divisors static int divSum(int num) { // Final result of summation of divisors int result = 0; // find all divisors which divides 'num' for (int i = 1; i * i <= num; ++i) { // if 'i' is divisor of 'num' if (num % i == 0) { // if both divisors are same then add // it only once else add both if (i == (num / i)) result += i; else result += (i + num / i); } } return result; } // Returns true if n is Super Perfect else false. static bool isSuperPerfect(int n) { // Find the sum of all divisors of number n int n1 = divSum(n); // Again find the sum of all divisors of n1 // and check if sum is equal to n1 return (2 * n == divSum(n1)); } public static void Main () { int n = 16; Console.WriteLine((isSuperPerfect(n) ? "Yes" : "No")); n = 6; Console.WriteLine((isSuperPerfect(n) ? "Yes" : "No")); } } // This code is contributed by vt_m.
PHP
<?php // PHP program to check whether // number is superperfect or not // Function to calculate // sum of all divisors function divSum($num) { // Final result of // summation of divisors $result = 0; // find all divisors // which divides 'num' for ($i = 1; $i * $i <= $num; ++$i) { // if 'i' is divisor // of 'num' if ($num % $i == 0) { // if both divisors // are same then add // it only once else // add both if ($i == ($num / $i)) $result += $i; else $result += ($i + $num/$i); } } return $result; } // Returns true if n is // Super Perfect else false. function isSuperPerfect($n) { // Find the sum of all // divisors of number n $n1 = divSum($n); // Again find the sum // of all divisors of n1 // and check if sum is // equal to n1 return (2 * $n == divSum($n1)); } // Driver code $n = 16; $hh = (isSuperPerfect($n) ? "Yes\n" : "No\n"); echo($hh); $n = 6; $hh=(isSuperPerfect($n) ? "Yes\n" : "No\n"); echo($hh); // This code is contributed by AJit ?>
Javascript
<script> // JavaScript program for the above approach // Function to calculate sum of all divisors function divSum(num) { // Final result of summation of divisors let result = 0; // find all divisors which divides 'num' for (let i = 1; i * i <= num; ++i) { // if 'i' is divisor of 'num' if (num % i == 0) { // if both divisors are same then add // it only once else add both if (i == (num/i)) result += i; else result += (i + num/i); } } return result; } // Returns true if n is Super Perfect else false. function isSuperPerfect(n) { // Find the sum of all divisors of number n let n1 = divSum(n); // Again find the sum of all divisors of n1 // and check if sum is equal to n1 return (2*n == divSum(n1)); } // Driver Code let n = 16; document.write((isSuperPerfect(n) ? "Yes\n" : "No\n") + "<br />"); n = 6; document.write((isSuperPerfect(n) ? "Yes\n" : "No\n") + "<br />"); // This code is contributed by splevel62. </script>
Output: Yes No
Complejidad temporal: O(sqrt(n + n1)) donde n1 es la suma de los divisores de n.
Espacio auxiliar: O(1)
Datos sobre los supernúmeros:
- Si n es un número superperfecto par, entonces n debe ser una potencia de 2, es decir, 2 k tal que 2 k+1 – 1 es un primo de Mersenne .
- No se sabe si existen números superperfectos impares. Un número superperfecto impar n tendría que ser un número cuadrado tal que n o σ(n) sea divisible por al menos tres números primos distintos. No hay números superperfectos impares por debajo de 7×10 24
Referencia:
https://en.wikipedia.org/wiki/Superperfect_number
Este artículo es una contribución de Shubham Bansal . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA