📜  几乎完美的数字

📅  最后修改于: 2022-05-13 01:57:59.252000             🧑  作者: Mango

几乎完美的数字

给定一个数字 n,检查它是否是几乎完美的数字。几乎完美数是一个自然数,其所有除数(包括 1 和数字本身)之和等于2n – 1
例子 :

Input: n = 16
Output: Yes
Explanation: sum of divisors = 1 + 2 + 4 + 8 + 16 = 31 = 2n - 1

Input: n = 9
Output: No
Explanation: sum of divisors = 1 + 3 + 9 ≠ 2n - 1

C++
// CPP program to check if a number
// is almost perfect.
#include 
using namespace std;
 
bool isAlmostperfect(int n)
{
    int divisors = 0;
 
    for (int i = 1; i <= n; i++) {
 
        // store sum of divisors of n
        if (n % i == 0)
            divisors += i;
    }
 
    // sum of divisors = 2*n - 1
    if (divisors == 2 * n - 1)
        return true;
 
    return false;
}
 
int main()
{
    int n = 16;
    if (isAlmostperfect(n))
        cout << "Yes";
    else
        cout << "No";
}


Java
// Java program to check if a
// number is almost perfect.
 
class GFG {
     
// Function to check number is
// almost perfect or not
static boolean isAlmostperfect(int n)
{
    int divisors = 0;
 
    for (int i = 1; i <= n; i++)
    {
 
        // store sum of divisors of n
        if (n % i == 0)
            divisors += i;
    }
 
    // sum of divisors = 2*n - 1
    if (divisors == 2 * n - 1)
        return true;
 
    return false;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 16;
    if (isAlmostperfect(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by
// Smitha Dinesh Semwal.


Python3
# Python program to check if a number
# is almost perfect.
def isAlmostperfect(n):
 
    divisors = 0
    for i in range(1, n+1):
 
        # store sum of divisors of n
        if (n % i == 0):
            divisors = divisors + i
 
    # sum of divisors = 2*n - 1
    if (divisors == 2 * n - 1):
        return True
    else:
        return False
 
# Driver code
n = 16
if (isAlmostperfect(n)):
    print ("Yes")
else:
    print ("No")
 
# This code is contributed by
# Manish Shaw (manishshaw1)


C#
// C# program to check if a
// number is almost perfect.
using System;
 
class GFG
{
     
    // Function to check number is
    // almost perfect or not
    static bool isAlmostperfect(int n)
    {
        int divisors = 0;
     
        for (int i = 1; i <= n; i++)
        {
     
            // store sum of divisors of n
            if (n % i == 0)
                divisors += i;
        }
     
        // sum of divisors = 2 * n - 1
        if (divisors == 2 * n - 1)
            return true;
     
        return false;
    }
 
    // Driver Code
    static public void Main ()
    {
        int n = 16;
         
        if (isAlmostperfect(n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by Ajit.


PHP


Javascript


输出:
Yes

发现几乎完美的数具有 2^k(k = 0, 1, 2, 3, 4, ..) 的形式。然而,它没有被证明。