2020-07-28 07:59:44 +00:00
|
|
|
"""
|
|
|
|
== 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 example: 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
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def perfect(number: int) -> bool:
|
|
|
|
"""
|
2023-10-21 22:05:37 +00:00
|
|
|
Check if a number is a perfect number.
|
|
|
|
|
|
|
|
A perfect number is a positive integer that is equal to the sum of its proper
|
|
|
|
divisors (excluding itself).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
number: The number to be checked.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the number is a perfect number, False otherwise.
|
|
|
|
|
added a function to calculate perceived frequency by observer using Doppler Effect (#10776)
* avg and mps speed formulae added
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* avg and mps speed formulae added
* fixed_spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* ws
* added amicable numbers
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* added amicable numbers
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* removed
* changed name of file and added code improvements
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* issues fixed due to pi
* requested changes added
* Created doppler_effect_of_sound.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Updated doppler_effect_of_sound.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* added desc names
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixed spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* renamed doppler_effect_of_sound.py to doppler_frequency.py
* used expection handling rather than print statements
* fixed spacing for ruff
* Update doppler_frequency.py
This is super slick! Well done.
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2023-10-21 22:33:50 +00:00
|
|
|
Start from 1 because dividing by 0 will raise ZeroDivisionError.
|
|
|
|
A number at most can be divisible by the half of the number except the number
|
|
|
|
itself. For example, 6 is at most can be divisible by 3 except by 6 itself.
|
|
|
|
|
2023-10-21 22:05:37 +00:00
|
|
|
Examples:
|
2020-07-28 07:59:44 +00:00
|
|
|
>>> perfect(27)
|
|
|
|
False
|
|
|
|
>>> perfect(28)
|
|
|
|
True
|
|
|
|
>>> perfect(29)
|
|
|
|
False
|
2023-10-21 22:05:37 +00:00
|
|
|
>>> perfect(6)
|
|
|
|
True
|
|
|
|
>>> perfect(12)
|
|
|
|
False
|
|
|
|
>>> perfect(496)
|
|
|
|
True
|
|
|
|
>>> perfect(8128)
|
|
|
|
True
|
|
|
|
>>> perfect(0)
|
added a function to calculate perceived frequency by observer using Doppler Effect (#10776)
* avg and mps speed formulae added
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* avg and mps speed formulae added
* fixed_spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* ws
* added amicable numbers
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* added amicable numbers
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* removed
* changed name of file and added code improvements
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* issues fixed due to pi
* requested changes added
* Created doppler_effect_of_sound.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Updated doppler_effect_of_sound.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* added desc names
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixed spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* renamed doppler_effect_of_sound.py to doppler_frequency.py
* used expection handling rather than print statements
* fixed spacing for ruff
* Update doppler_frequency.py
This is super slick! Well done.
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2023-10-21 22:33:50 +00:00
|
|
|
False
|
|
|
|
>>> perfect(-1)
|
|
|
|
False
|
2023-10-21 22:05:37 +00:00
|
|
|
>>> perfect(12.34)
|
added a function to calculate perceived frequency by observer using Doppler Effect (#10776)
* avg and mps speed formulae added
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* avg and mps speed formulae added
* fixed_spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* ws
* added amicable numbers
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* added amicable numbers
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* removed
* changed name of file and added code improvements
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* issues fixed due to pi
* requested changes added
* Created doppler_effect_of_sound.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Updated doppler_effect_of_sound.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* added desc names
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixed spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* renamed doppler_effect_of_sound.py to doppler_frequency.py
* used expection handling rather than print statements
* fixed spacing for ruff
* Update doppler_frequency.py
This is super slick! Well done.
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2023-10-21 22:33:50 +00:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: number must be an integer
|
|
|
|
>>> perfect("Hello")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: number must be an integer
|
2020-07-28 07:59:44 +00:00
|
|
|
"""
|
added a function to calculate perceived frequency by observer using Doppler Effect (#10776)
* avg and mps speed formulae added
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* avg and mps speed formulae added
* fixed_spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* ws
* added amicable numbers
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* added amicable numbers
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* removed
* changed name of file and added code improvements
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* issues fixed due to pi
* requested changes added
* Created doppler_effect_of_sound.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Updated doppler_effect_of_sound.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* added desc names
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixed spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* renamed doppler_effect_of_sound.py to doppler_frequency.py
* used expection handling rather than print statements
* fixed spacing for ruff
* Update doppler_frequency.py
This is super slick! Well done.
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2023-10-21 22:33:50 +00:00
|
|
|
if not isinstance(number, int):
|
|
|
|
raise ValueError("number must be an integer")
|
|
|
|
if number <= 0:
|
|
|
|
return False
|
2020-07-29 08:54:05 +00:00
|
|
|
return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number
|
2020-07-28 07:59:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
added a function to calculate perceived frequency by observer using Doppler Effect (#10776)
* avg and mps speed formulae added
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* avg and mps speed formulae added
* fixed_spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* ws
* added amicable numbers
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* added amicable numbers
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* removed
* changed name of file and added code improvements
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* issues fixed due to pi
* requested changes added
* Created doppler_effect_of_sound.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Updated doppler_effect_of_sound.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* added desc names
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixed spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* renamed doppler_effect_of_sound.py to doppler_frequency.py
* used expection handling rather than print statements
* fixed spacing for ruff
* Update doppler_frequency.py
This is super slick! Well done.
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2023-10-21 22:33:50 +00:00
|
|
|
from doctest import testmod
|
|
|
|
|
|
|
|
testmod()
|
2020-07-29 08:54:05 +00:00
|
|
|
print("Program to check whether a number is a Perfect number or not...")
|
added a function to calculate perceived frequency by observer using Doppler Effect (#10776)
* avg and mps speed formulae added
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* avg and mps speed formulae added
* fixed_spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* ws
* added amicable numbers
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* added amicable numbers
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* removed
* changed name of file and added code improvements
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* issues fixed due to pi
* requested changes added
* Created doppler_effect_of_sound.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Updated doppler_effect_of_sound.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* added desc names
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixed spacing
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* renamed doppler_effect_of_sound.py to doppler_frequency.py
* used expection handling rather than print statements
* fixed spacing for ruff
* Update doppler_frequency.py
This is super slick! Well done.
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2023-10-21 22:33:50 +00:00
|
|
|
try:
|
|
|
|
number = int(input("Enter a positive integer: ").strip())
|
|
|
|
except ValueError:
|
|
|
|
msg = "number must be an integer"
|
|
|
|
print(msg)
|
|
|
|
raise ValueError(msg)
|
|
|
|
|
2020-07-29 08:54:05 +00:00
|
|
|
print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")
|