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++
// 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"; } |
chevron_right
filter_none
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. |
chevron_right
filter_none
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 = 16if (isAlmostperfect(n)): print ("Yes") else: print ("No") # This code is contributed by # Manish Shaw (manishshaw1) |
chevron_right
filter_none
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. |
chevron_right
filter_none
PHP
<?php // PHP program to check if a // number is almost perfect. // function to check // almost perfect function 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. ?> |
chevron_right
filter_none
Output:
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.
Recommended Posts:
- Previous perfect square and cube number smaller than number N
- Even Perfect Number
- Multiply perfect number
- Perfect totient number
- Anti-perfect Number
- Perfect Digital Invariants number
- Find the Next perfect square greater than a given number
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Least number to be added to or subtracted from N to make it a Perfect Square
- Largest Divisor of a Number not divisible by a perfect square
- Smallest N digit number which is a perfect fourth power
- Check whether the number can be made perfect square after adding K
- Check whether the number formed by concatenating two numbers is a perfect square or not
- Check if number formed by joining two Numbers is Perfect Cube
- Probability of getting a perfect square when a random number is chosen in a given range
- Perfect Square String
- Sum of all perfect numbers present in an array
- Print N numbers such that their sum is a Perfect Cube
- Check if the sum of perfect squares in an array is divisible by x
- Print N numbers such that their product is a Perfect Cube
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

