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
202213
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
202213
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.

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.

Python Implementation

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.

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: