Python Program to Find Perfect Number codigence

Python Program to Find Perfect Number codigence

Python Program to Find Perfect Number codigence

  • Python Program to find Perfect Number using While Loop.
  • Perfect Number in Python using For Loop.

Overview of Perfect Number in Python

A Perfect Number N is defined as any positive integer where the sum of its divisors minus the number itself equals the number. The first few of these, already known to the ancient Greeks, are 6, 28, 496, and 8128.Euclid, over two thousand years ago, showed that all even perfect numbers can be represented by,N = 2p-1(2p -1) where p is a prime for which 2p -1 is a Mersenne prime.That is, we have an even Perfect Number of the form N whenever the Mersenne Number 2p -1 is a prime number. Undoubtedly Mersenne was familiar with Euclid’s book in coming up with his primes.

Python Program to find Perfect Number using While Loop

  • Define a function to check Perfect No.
  • Let's have a function named 'is_perfect' and take the argument 'no' for number to check.
  • Now Inside the Function,
  • copy the value of no into a temporary variable,say dup_no.
  • Now initialize a variable,say 'factor_sum' to zero.This will store the sum of factors of number.
  • Using While loop calculate the sum.
  • Use if statement to check if number is equal to sum or not.If equal then return True else return False.
  • Print the Final result.
  • Call the function to check ,For Example is_perfect(6).
  
#Python Program to check perfect no
#using for loop
def is_perfect(no):
    factor_sum = 0
    for i in range(1,no):
        if(no % i == 0):
            factor_sum+=i
    if(factor_sum == no):
        print("%d is a perfect no."%(no))
    else:
        print("%d is a not a perfect no."%(no))

is_perfect(6)
is_perfect(19)
is_perfect(28)
  
  

Output


6 is a perfect no.
19 is a not a perfect no.
28 is a perfect no.

Python Program to find Perfect Number using For Loop

  • Define a function to check Perfect No.
  • Let's have a function named 'is_perfect' and take the argument 'no' for number to check.
  • Now Inside the Function,
  • copy the value of no into a temporary variable,say dup_no.
  • Now initialize a variable,say 'factor_sum' to zero.This will store the sum of factors of number.
  • Using For loop calculate the sum.
  • Use if statement to check if number is equal to sum or not.If equal then return True else return False.
  • Print the Final result.
  • Call the function to check ,For Example is_perfect(6).
  
#using while loop
def is_perfect(no):
    factor_sum = 0
    dup_no = no #duplicating no
    while (dup_no > 1):
        if(no % dup_no == 0):
            factor_sum+=dup_no
        dup_no-=1

    if(factor_sum == no):
        print("%d is a perfect no."%(no))
    else:
        print("%d is a not a perfect no."%(no))

is_perfect(6)
is_perfect(19)
is_perfect(28)
  
  

Output


6 is a perfect no.
19 is a not a perfect no.
28 is a perfect no.

Conclusion

A Perfect number is a mathematical concept that has its own applications. Python allows us to implement perfect number checking programs quickly and effectively through its multiple features.