Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
Almost Perfect Number
Next article icon

Almost Perfect Number

Last Updated : 25 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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"; } 
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. 
JavaScript
<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> 
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. ?> 

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.
Time Complexity: O(n)
Auxiliary Space: O(1)


Next Article
Almost Perfect Number

J

jaideeppyne1997
Improve
Article Tags :
  • Misc
  • DSA
  • Basic Coding Problems
  • divisibility
  • series
Practice Tags :
  • Misc
  • series

Similar Reads

    Special two digit number
    A special two-digit number is a number such that when the sum of the digits of the number is added to the product of its digits, the result is equal to the original two-digit number. Examples : input : 59. output : 59 is a Special Two-Digit Number Explanation: Sum of digits = 5 + 9 = 14 Product of i
    6 min read
    Program to Count numbers on fingers
    Count the given numbers on your fingers and find the correct finger on which the number ends. Examples: Input : 17 Output :1 Input :27 Output :3 Recommended PracticeFinger GameTry It! Approach: The first number starts from the thumb, second on the index finger, third on the middle finger, fourth on
    7 min read
    Odious number
    Odious number is a nonnegative number that has an odd number of 1s in its binary expansion. The first few odious numbers are therefore 1, 2, 4, 7, 8, 11, 13, 14, 16, 19... Given a number check if its a odious number or not. Examples : Input : 16 Output : Odious Number Explanation: Binary expansion o
    4 min read
    Mathematical Algorithms - Number Digits
    "Mathematical Algorithms | Number Digits" is a guide that explores problems and solutions involving digits. It covers common digit-related issues, such as digit properties, manipulation, and counting under constraints. The article discusses algorithmic approaches like modular arithmetic, string hand
    9 min read
    POTD Solutions | 28 Oct’ 23 | Bleak Numbers
    Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Number Theory and Bit manipulation but will also help you build up problem-solv
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences