• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Perfect Numbers in Python

Perfect Numbers in Python

May 6, 2022 Leave a Comment

We can check if a number is a perfect number in Python easily with a simple function. A number is perfect is the divisors of a number (excluding the number itself) sum to the number.

def checkPerfectNumber(n):
    sum_div = 0
    for i in range(1, n // 2 + 1):
        if (n % i == 0):
            sum_div = sum_div + i
    if (sum_div == n):
        return True
    else:
        return False

print(checkPerfectNumber(6))
print(checkPerfectNumber(13))
print(checkPerfectNumber(28))

#Output:
True
False
True

There are many interesting definitions of different kinds of numbers in mathematics. One such type of number which has been studied throughout history is the perfect number.

A perfect number is a positive integer which is equal to the sum of its factors, excluding the number itself.

With Python, we can easily define a function which will check to see if a number is a perfect number.

To check if a number is a perfect number, we just need to loop over the numbers between 1 and the number divided by 2 (because no integer divisor can be greater than the number divided by 2).

Then, at each step, we will check if the number is divisible by the loop iteration number and if it is, we will add it to a running total of the sum of the other divisors.

After the loop has completed, we will check if the sum of the divisors equals the number.

Below is a function which will check if a number is a perfect number in Python.

def checkPerfectNumber(n):
    sum_div = 0
    for i in range(1, n // 2 + 1):
        if (n % i == 0):
            sum_div = sum_div + i
    if (sum_div == n):
        return True
    else:
        return False

print(checkPerfectNumber(6))
print(checkPerfectNumber(13))
print(checkPerfectNumber(28))

#Output:
True
False
True

Hopefully this article has been useful for you to learn about perfect numbers and how you can check if a number is a perfect number in Python.

Other Articles You'll Also Like:

  • 1.  Using Python To Split String by Comma into List
  • 2.  pandas nsmallest – Find Smallest Values in Series or Dataframe
  • 3.  Python if else do nothing – Using pass Statement to Do Nothing in Python
  • 4.  Using if in Python Lambda Expression
  • 5.  Replace Values in Dictionary in Python
  • 6.  Convert First Letter of String to Lowercase in Python
  • 7.  How to Repeat a Function in Python
  • 8.  How to Measure Execution Time of Program in Python
  • 9.  Print Time Elapsed in Python
  • 10.  Using Python to Get Queue Size

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy