Added Python Program to Check Perfect Number - #2244
Conversation
Travis tests have failedHey @Utsav1999, TravisBuddy Request Identifier: 914334c0-d060-11ea-81e3-a59d363db46c |
Travis tests have failedHey @Utsav1999, TravisBuddy Request Identifier: f33c6c90-d061-11ea-81e3-a59d363db46c |
| """ | ||
| == 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, ... | ||
|
|
||
| """ |
There was a problem hiding this comment.
| """ | |
| == 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 | |
| """ |
| 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 |
There was a problem hiding this comment.
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.
| check = perfect(number) | ||
| if check: | ||
| print("{} is a Perfect Number.".format(number)) | ||
| else: | ||
| print("{} is not a Perfect Number.".format(number)) |
There was a problem hiding this comment.
| 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.") |
| """ | ||
|
|
||
| """ |
There was a problem hiding this comment.
Doctests must be in the (first) docstring of the function.
| """ | |
| """ | |
| """ | |
| """ |
| 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.") |
There was a problem hiding this comment.
| print(f"{number} is {'' if perfect(number) else 'not'} a Perfect Number.") | |
| print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.") |
There was a problem hiding this comment.
Then it is taking two spaces.
There was a problem hiding this comment.
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.
|
Hey @Utsav1999, TravisCI finished with status TravisBuddy Request Identifier: 64c29ba0-d0a8-11ea-b8d2-e37d5f97d09d |
* 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 <cclauss@me.com>
Describe your change:
This python program will check whether a number is a Perfect number or not.
Checklist:
Fixes: #{$ISSUE_NO}.