Perfect Number Program In Python: How to check if a number is perfect or not?

Published:Nov 30, 202315:55
0

Introduction

A quantity is claimed to be the proper quantity if the sum of its correct divisors (not together with the quantity itself) is the same as the quantity.

To get a greater thought let’s take into account an instance, correct divisors of 6 are 1, 2, 3. Now the sum of those divisors is the same as 6 (1+2+3=6), so 6 is claimed to be an ideal quantity. Whereas if we take into account one other quantity like 12, correct divisors of 12 are 1, 2, 3, 4, 6. Now the sum of those divisors isn't equal to 12, so 12 isn't an ideal quantity.

Programming in Python is comparatively less complicated and extra enjoyable when in comparison with different languages due to its less complicated syntax, good readability. Now that we're clear with the idea of good quantity let’s write a python program to verify if a quantity is an ideal quantity or not. Let’s construct a python code for checking if the given consumer enter is an ideal quantity or not and discover the enjoyable in coding with python.

Learn: Python Sample Applications

Python Program

A fundamental answer for locating an ideal quantity is to loop over 2 to number-1, preserve the sum of its correct divisors, and verify if the sum is the same as the quantity.

n=int(enter(“enter the number”))
sum=1
for i in vary(2,n):
if(npercenti==0):
sum=sum+i
if(sum==n):
print(n,”is an ideal quantity”)
else:
print(n,”isn't an ideal quantity”)

Let’s stroll via the code.

We're first initializing n with the consumer enter and typecasting it to an integer as a result of by default the consumer enter is learn as a string in python. We have to verify whether or not n is an ideal quantity or not. Notice that we're initializing the sum with 1 as a result of 1 is a correct divisor for all integers (excluding zero), in order that we will exclude an iteration within the loop and instantly begin from 2.

We're looping over 2 to number-1 and including the integers to sum if it's a correct divisor. And at last, once we come out of the loop we're checking if the sum obtained is the same as the quantity or not. Piece of cake proper?

Little Optimized Model

After having a dry run over the above program, we might have a query can we optimize it? Effectively, however we will cut back the variety of iterations to quantity/2 with out altering the algorithm. As a result of we obtained the concept a quantity can't have a correct divisor larger than quantity/2.

n=int(enter(“enter the number”))
sum=1
for i in vary(2,n//2+1):
if(npercenti==0):
sum=sum+i
if(sum==n):
print(n,”is an ideal quantity”)
else:
print(n, “is not a perfect number”)

The above snippet is sort of just like the earlier one, with the one distinction of looping until quantity/2. Notice that we're performing an integer division to keep away from changing it to a float sort, and we're looping until n//2+1 as a result of the final integer within the vary isn't thought of within the python loop.

Limitations

After we are requested to seek out good numbers in a given vary then our answer would eat time proportional to quantity^2 i.e., O(n²) time complexity. As a result of we have to loop over every quantity within the given vary after which verify for correct divisors for every quantity. And few numbers fulfill the proper quantity situation. For instance, the variety of good numbers within the vary of 0 to 1000 is simply 3 (6, 28, 496).

There may be an optimized answer for this the place we'd like not loop over all components to seek out correct divisors, Euclid’s method states that 2n−1(2n − 1)  is an excellent good quantity the place each n, (2n − 1) is prime numbers. For instance, 6 satisfies the above equation with n as 2 and each 2, 22 − 1 (22 − 1 = 3) are prime numbers. However we can't reply if we have been requested to seek out if there are any odd good numbers.

Additionally, we all know that each language has a restrict to the vary of integers that it may retailer. With this limitation, we might not have a technique to discover the biggest good quantity.

All these limitations are confronted if our enter quantity is massive, but when our enter quantity is small then our preliminary answer would work in much less time.

Additionally Learn: Python Framework for Internet Growth

Conclusion

We’ve recognized the definition and understood the idea behind the proper quantity. Walked via a fundamental answer for locating a quantity is an ideal quantity or not. And after watching the preliminary answer we’ve optimized it slightly bit by decreasing the variety of iterations. We’ve come via the constraints of our algorithm and mentioned Euclid’s method for locating the even good quantity.

Now that you're conscious of the python program to verify if a quantity is an ideal quantity or not. Attempt writing the code by yourself and take a look at optimizing it if discovered any overlapping iterations. Additionally, attempt constructing the code for locating good numbers within the given vary of numbers.

I hope you'll be taught lots whereas engaged on this weblog. In case you are inquisitive about studying knowledge science to be within the entrance of fast-paced technological developments, take a look at upGrad & IIIT-B’s PG Diploma in Knowledge Science and upskill your self for the longer term.

Put together for a Profession of the Future

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN DATA SCIENCE Be taught Extra



To stay updated with the latest bollywood news, follow us on Instagram and Twitter and visit Socially Keeda, which is updated daily.

sociallykeeda profile photo
sociallykeeda

SociallyKeeda: Latest News and events across the globe, providing information on the topics including Sports, Entertainment, India and world news.