• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Check For Perfect Number In Python

Check For Perfect Number In Python

Author: Aditya Raj
Last Updated: January 9, 2022

We have named numbers based on their specialties. One such number is a perfect number. In this article, we will discuss the properties of perfect numbers. We will also implement a program to check for a perfect number in python.

What Is A Perfect Number?

A number is called a perfect number if it is equal to the sum of all of its factors excluding the number itself. If we consider the number itself in the sum, we can say that the sum of all the factors of a perfect number is double the given number.

For example, consider the number 6. It has four factors i.e. 1,2,3, and 6. As we are excluding the number itself, the sum of other factors i.e. 1, 2, and 3 is 6. Hence, 6 is a perfect number.

Alternatively, the sum of all the factors of 6 is 1+2+3+6 i.e. 12, which is double the number itself. Hence, 6 is a perfect number.

Let us take another number 10. The factors of 10 are 1,2,5, and 10. The sum of all the factors of 10 equals 18, which is not double the given number. Hence, 10 is not a perfect number. 

Check For Perfect Number In Python

To check for a perfect number, we will first find its factors. After that, we will check if the sum of all the factors is double the given number or not.

To find the factors of the given number N, we will divide the number by all the numbers starting from 1 to N. The numbers that completely divide the given number will be declared as factors of N. We will store these factors in a list as follows.

dedef calculate_factors(N):
    factors = []
    for i in range(1, N + 1):
        if N % i == 0:
            factors.append(i)
    return factors


input_number = 10
output = calculate_factors(input_number)
print("factors of {} are {}".format(input_number, output))

Output:

factors of 10 are [1, 2, 5, 10]

After finding the factors of the given number, we will find the sum of the factors using the sum() function. After finding the sum, we will check if the sum is double the given number or not. If yes, we will say that the given number is a perfect number. Otherwise not. 

We can implement this logic to check for a Perfect number in python as follows.

def calculate_factors(N):
    factors = []
    for i in range(1, N + 1):
        if N % i == 0:
            factors.append(i)
    return factors


def check_perfect_number(N):
    factors = calculate_factors(N)
    sumOfFactors = sum(factors)
    if sumOfFactors // 2 == N:
        return True
    else:
        return False


input_number = 10
output = check_perfect_number(input_number)
print("{} is a perfect number: {}".format(input_number, output))
input_number = 6
output = check_perfect_number(input_number)
print("{} is a perfect number: {}".format(input_number, output))

Output:

10 is a perfect number: False
6 is a perfect number: True

Conclusion

In this article, we have discussed what a perfect number is. We have also implemented a program to check whether a given number is a perfect number or not. To know more about numbers in python, you can read this article on decimal numbers in python. You might also like this article on complex numbers in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us