Skip to content

Added Python Program to Check Perfect Number #2244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 28, 2020

Conversation

Utsav1999
Copy link
Contributor

Describe your change:

This python program will check whether a number is a Perfect number or not.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@Utsav1999 Utsav1999 changed the title Added Python Program to Check Perfet Number Added Python Program to Check Perfect Number Jul 27, 2020
@TravisBuddy
Copy link

Travis tests have failed

Hey @Utsav1999,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: 914334c0-d060-11ea-81e3-a59d363db46c

@TravisBuddy
Copy link

Travis tests have failed

Hey @Utsav1999,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: f33c6c90-d061-11ea-81e3-a59d363db46c

Comment on lines 1 to 12
"""
== Perfect Number ==
In number theory, a perfect number is a
positive integer that is equal to the sum
of its positive divisors, excluding the
number itself.
>>> For 6 ==> divisors[1, 2, 3, 6]
Excluding 6 sum(divisors) = 1 + 2 + 3 = 6
So, 6 is a Perfect Number
Other examples of Perfect Numbers: 28, 486, ...

"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""
== Perfect Number ==
In number theory, a perfect number is a
positive integer that is equal to the sum
of its positive divisors, excluding the
number itself.
>>> For 6 ==> divisors[1, 2, 3, 6]
Excluding 6 sum(divisors) = 1 + 2 + 3 = 6
So, 6 is a Perfect Number
Other examples of Perfect Numbers: 28, 486, ...
"""
"""
== Perfect Number ==
In number theory, a perfect number is a positive integer that is equal to the sum of
its positive divisors, excluding the number itself.
For 6 ==> divisors[1, 2, 3, 6]
Excluding 6, the sum(divisors) is 1 + 2 + 3 = 6
So, 6 is a Perfect Number
Other examples of Perfect Numbers: 28, 486, ...
https://en.wikipedia.org/wiki/Perfect_number
"""

Comment on lines 16 to 34
divisors = []
for i in range(1, ((number // 2) + 1)):
"""
starting from 1 as division by 0
will raise error.
A number at most can be divisible
by the half of the number except
the number itself
>>> 6 at most can be divisible by 3
except 6 itself
"""

if (number % i) == 0:
divisors.append(i)

if sum(divisors) == number:
return True
else:
return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put all of this on one line using a Python list comprehension.

Doctests chould be:

"""
>>> perfect(27)
False
>>> perfect(28)
True
>>> perfect(29)
False
"""

The content to the right of >>> must be valid Python code that will be run by the test harness. This explains why the Travis CI tests are currently failing.

Comment on lines 40 to 44
check = perfect(number)
if check:
print("{} is a Perfect Number.".format(number))
else:
print("{} is not a Perfect Number.".format(number))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
check = perfect(number)
if check:
print("{} is a Perfect Number.".format(number))
else:
print("{} is not a Perfect Number.".format(number))
print(f"{number} is a {'' if perfect(number) else 'not '}Perfect Number.")

Comment on lines 27 to 29
"""

"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doctests must be in the (first) docstring of the function.

Suggested change
"""
"""
"""
"""

print("{} is a Perfect Number.".format(number))
else:
print("{} is not a Perfect Number.".format(number))
print(f"{number} is {'' if perfect(number) else 'not'} a Perfect Number.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(f"{number} is {'' if perfect(number) else 'not'} a Perfect Number.")
print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it is taking two spaces.

Copy link
Member

@cclauss cclauss Jul 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check again.

>>> a = True
>>> print(f"a is {'' if a else 'not '}True.")
a is True.
>>> a = False
>>> print(f"a is {'' if a else 'not '}True.")
a is not True.

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@cclauss cclauss merged commit 9ea144f into TheAlgorithms:master Jul 28, 2020
@TravisBuddy
Copy link

Hey @Utsav1999,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

View build log

TravisBuddy Request Identifier: 64c29ba0-d0a8-11ea-b8d2-e37d5f97d09d

stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Added Python Program to Check Perfet Number

* CodeSpell Error Fix - 1

* Build Error Fix - 1

* Made suggested changes

* Use generator expression

Co-authored-by: Christian Clauss <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants