Perfect Number in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Playing with numbers has been a part of our lives since we were little. The most basic arithmetic operations on numbers have useful results. We must focus more on the logic that develops in mathematics because that is the backbone behind programming.

The idea of a perfect number is centuries old. For over 2300 years, they've been researched.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

What is a Perfect Number?

When a number is equal to the sum of all of its positive divisors except itself, it is said to be a Perfect Number.

For example:

n = 6

Positive divisors of 6 except itself are 1, 2 and 3

Sum of divisors is 1 + 2 + 3 = 6

Hence 6 is a perfect number.

Steps Involved to Check Whether the Given Number Is the Perfect Number in Python

1. First, we need to request an integer from the user, which will be stored in a variable named 'input_number'.
2. Now declare a variable called 'sum_variable' in which the sum of the divisors of the provided input_number will be stored.
3. Using a for loop, we will check whether the given number divides the input number, i.e. gives zero reminders. Our divisors will be these numbers.
4. Now add all the divisors to the variable 'sum_variable'.
5. It is the last step where we will compare the number given by the user to the value of the sum_varialbe, use the decision statements, and if the values are equal then we will display the given number as a perfect number.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Python Implementation

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Function to Check Whether Given Number is Perfect Number in Python

Let's look at another program where we'll use a function to find whether a given number is a perfect number in python.

Time Complexity: O(n) because the loop runs from 1 to n.
Space Complexity: O(1) since we are not using any extra space.

Efficient Solution

We check whether the given number is the perfect number in python efficiently by running a while loop till the square root of n. If a number is dividing n, i.e. after the division remainder is zero, then add i and n/i to the sum_variable.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Python Implementation

Time Complexity: O(√n) because we are substituting i by i*i at each iteration.
Space Complexity: O(1) since we are not using any extra space.

Key Points

  1. Every even perfect number has the form 2p1(2p1)2^{p-1}(2^p-1), where 2p12^{p-1} and p is a prime number, and Euclid proved this.

    For example:
    for p=2:21(221)=23=6p = 2: 2^1(2^2-1) = 2*3 = 6
    for p=3:22(231)=47=28p = 3: 2^2(2^3-1) = 4*7 = 28

  2. No one has found any odd perfect number to date.

Conclusion

In this article, we learned about:

  • When a number is equal to the sum of all of its positive divisors except itself, it is said to be a Perfect Number
  • We looked at two approaches to check whether the given number is a perfect number in python.
  • We can find whether a number is perfect or not in O(√n) complexity and space complexity of O(1).
  • There is no odd perfect number found to date.

See Also: