给定一个整数n.检查数字 N 是否为超完美数。超完美数是满足σ的正整数 2. (n) =σ(σ(n))=2n,其中σ为 除数求和 作用
null
Input: n = 16Output: yesExplanation: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 = 8Output: no Explanation:σ(8) = 1 + 2 + 4 + 8 = 15and σ(15) = 1 + 3 + 5 + 15 = 24thus ( σ(σ(8)) = 24 ) ≠ (2 * 8 = 26)
我们强烈建议您在继续解决方案之前单击此处并进行练习。
这个想法很简单。我们只是从1迭代到sqrt(n),然后找到n的所有因子之和,我们把这个和称为n1。现在我们再次需要从1到sqrt(n1)进行迭代,找到所有除数的和。然后我们只需要检查结果的和是否等于2*n。
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" : "No" ); n = 6; cout << (isSuperPerfect(n) ? "Yes" : "No" ); 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" : "No" )); n = 6 ; System.out.printf((isSuperPerfect(n) ? "Yes" : "No" )); } } // 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" : "No" ); echo ( $hh ); $n = 6; $hh =(isSuperPerfect( $n ) ? "Yes" : "No" ); 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" : "No" ) + "<br />" ); n = 6; document.write((isSuperPerfect(n) ? "Yes" : "No" ) + "<br />" ); // This code is contributed by splevel62. </script> |
Output:YesNo
时间复杂性: O(sqrt(n+n1)),其中n1是n的除数之和。 辅助空间: O(1) 关于超数的事实:
- 如果n是偶数超完美数,那么n必须是2的幂,即2 K 这样2 k+1 –1是一个 梅森素数 .
- 目前还不知道是否存在奇数超完美数。奇数超完美数n必须是一个平方数,使得n或σ(n)至少可被三个不同的素数整除。在7×10以下没有奇数超完美数 24
参考: https://en.wikipedia.org/wiki/Superperfect_number 本文由 Shubham Bansal .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END