Almost Perfect Number - GeeksforGeeks

29 Apr, 2021
Given a number n, check it is the Almost Perfect number or not. Almost perfect number is a natural number whose sum of all divisors including 1 and the number itself is equal to 2n – 1.Example :  
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++
Java
Python3
C#
PHP
Javascript
// CPP program to check if a number// is almost perfect.#include <bits/stdc++.h>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 program to check if a// number is almost perfect. class GFG {     // Function to check number is// almost perfect or notstatic 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 Codepublic 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.
# 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 coden = 16if (isAlmostperfect(n)):    print ("Yes")else:    print ("No") # This code is contributed by# Manish Shaw (manishshaw1)
// 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// PHP program to check if a// number is almost perfect. // function to check// almost perfectfunction isAlmostperfect($n){    $divisors = 0;     for ($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$n = 16;if (isAlmostperfect($n))    echo("Yes");else    echo("No"); // This code is contributed by Ajit.?>
<script> // Javascript program to check if a number// is almost perfect. function isAlmostperfect( n){    let divisors = 0;     for (let 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    let n = 16;    if (isAlmostperfect(n))        document.write("Yes");    else        document.write("No");        </script>
Yes
 
The almost perfect numbers are found to be of the form 2^k(k = 0, 1, 2, 3, 4, ..). However it is not proved. 
Smitha Dinesh Semwal
jit_t
manishshaw1
jana_sayantan
divisibility
series
School Programming
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Constructors in Java
Exceptions in Java
Ternary Operator in Python
Inline Functions in C++
Difference between Abstract Class and Interface in Java
Destructors in C++
Pure Virtual Functions and Abstract Classes in C++
Python Exception Handling
Exception Handling in C++
Taking input from console in Python