Discovering Perfect Numbers: Python Program to Find Four perfect Numbers Less Than 10,000

In number theory, a positive integer is called a perfect number if it is equal to the sum of all of its positive divisors, excluding itself. For example, 6 is the first perfect number, because 6 = 3 + 2 + 1, and the next is 28 = 14 + 7 + 4 + 2 + 1. There are only four perfect numbers that are less than 10,000. In this blog post, we will write a Python program to find these four numbers.

Python Program to Find Four perfect Numbers

Table of Contents [ Python Program Perfect Numbers ]

  • What are perfect numbers?
  • How to find perfect numbers?
  • Writing a program to find perfect numbers.

What are perfect numbers?

A perfect number is a positive integer that is equal to the sum of all of its positive divisors, excluding itself. For example, 6 is a perfect number because its divisors are 1, 2, and 3, and 1 + 2 + 3 = 6.

How to find perfect numbers?

To find perfect numbers, we need to check each positive integer to see if it is equal to the sum of its positive divisors. This can be done by iterating over all the integers from 1 to n and checking if each integer is a divisor of n. If it is, we add it to a running total. If the running total equals n at the end of the loop, then n is a perfect number.

Writing a program to find perfect numbers

Here's a Python program that finds the four perfect numbers less than 10,000:

def is_perfect(n):

    total = 0

    for i in range(1, n):

        if n % i == 0:

            total += i

    return total == n


perfect_numbers = []

for i in range(1, 10000):

    if is_perfect(i):

        perfect_numbers.append(i)

        if len(perfect_numbers) == 4:

            break


print(perfect_numbers)

Output:

[6, 28, 496, 8128]

Explanation: 

def is_perfect(n):: This line defines a function called is_perfect that takes an integer n as input. This function will determine if n is a perfect number or not.

total = 0: Initializes a variable total to store the sum of the divisors.

for i in range(1, n):: This for loop iterates from 1 to n-1, checking each number i as a potential divisor of n.

if n % i == 0:

Checks if n is divisible by i (i.e., i is a divisor of n) by checking if the remainder of n divided by i is 0.
If it is divisible, it means i is a proper divisor of n.
Executes the following line only when the condition is true.
total += i: If i is a proper divisor of n, it adds i to the total sum.

return total == n: After the loop finishes, it checks if the sum of proper divisors (total) is equal to n. If they are equal, it returns True, indicating that n is a perfect number. Otherwise, it returns False.

perfect_numbers = []: Initializes an empty list to store the perfect numbers found.

for i in range(1, 10000):: This for loop iterates from 1 to 10,000 (exclusive), checking each number i as a candidate for a perfect number.

if is_perfect(i):: Calls the is_perfect function to check if i is a perfect number.

If the function returns True, it means i is a perfect number.
Executes the following lines only when the condition is true.
perfect_numbers.append(i): Appends the perfect number i to the perfect_numbers list.

if len(perfect_numbers) == 4:

Checks if the length of the perfect_numbers list is equal to 4.

If it is, it means four perfect numbers have been found.

Executes the following line only when the condition is true.

break: Breaks out of the loop to stop searching for more perfect numbers.

print(perfect_numbers): Prints the list of perfect numbers found, which will contain the four perfect numbers less than 10,000.

| Practice Python MCQ with "Python MCQ Programs Interview  " Android App. 

The is_perfect function takes an integer n and returns True if n is a perfect number and False otherwise. It does this by iterating over all the integers from 1 to n - 1 and adding up the ones that divide n evenly. If the total equals n, then n is a perfect number.

The main part of the program iterates over all integers from 1 to 10,000 and checks if each one is a perfect number using the is_perfect function. If it is, it adds it to a list of perfect numbers. Once there are four perfect numbers in the list, the loop breaks, and the list is printed.

In conclusion, a perfect number is a positive integer that is equal to the sum of all of its positive divisors, excluding itself. There are only four perfect numbers that are less than 10,000. We can find these numbers by iterating over all integers from 1 to 10,000 and checking if each one is a perfect number using a function that adds up its divisors. The resulting list of four perfect numbers can then be printed.

FAQ: Python Program perfect Numbers ]

How can we determine if a number is perfect or not?

Determining if a number is perfect or not is a simple process that involves finding the sum of all the factors of the number, excluding the number itself, and checking if the sum is equal to the number. Here are some ways to determine if a number is perfect or not:

Write down all the factors of the number, excluding the number itself, and add them up. If the sum is equal to the number, then it is a perfect number.

Use a function that takes an integer as input and returns True if it is a perfect number and False otherwise. The function can iterate over all integers from 1 to n-1 and add up the ones that divide n evenly. If the total equals n, then n is a perfect number.

Use an online tool or calculator that can determine if a given number is perfect or not.

In conclusion, determining if a number is perfect or not involves finding the sum of all its factors, excluding itself, and checking if it is equal to the number. This can be done manually by writing down all the factors and adding them up, or by using a function or an online tool.

What is the largest known perfect number?

The largest known perfect number is 2^82,589,932 × (2^82,589,933 − 1), which has 49,724,095 digits. This number corresponds to the Mersenne prime 2^82,589,933 − 1, which is also the largest known prime number as of May 2023. The discovery of this perfect number was announced in December 2018 and it is the 51st known Mersenne prime.

Are there any odd perfect numbers known to exist?

The existence of odd perfect numbers is an open question in mathematics, and it is currently unknown whether they exist or not. Many mathematicians have attempted to prove or disprove the existence of odd perfect numbers, but no one has been successful so far. In fact, it has been proven that if an odd perfect number exists, it must be greater than 10^1500. Some necessary conditions for odd perfect numbers have been established, but they are insufficient to prove their existence. For example, it has been shown that if an odd perfect number exists, it must be divisible by at least the first 8 prime numbers. However, this condition is not enough to prove the existence of odd perfect numbers. In conclusion, whether odd perfect numbers exist or not remains one of the most intriguing and unsolved problems in mathematics.

| Practical List - Python [ 4330701 ] [ PRACTICAL EXERCISES ] 

Post a Comment

0 Comments