mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Happy number (new algorithm) (#10864)
* Happy number (new algorithm) adding new algorithm * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update maths/special_numbers/happy_number.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update happy_number.py added new changes * Update happy_number.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update happy_number.py * Update happy_number.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update happy_number.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update happy_number.py added ValueError part in code * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update happy_number.py modified and added raise Error code * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update happy_number.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
ad9948d5d4
commit
ddd4023fe6
48
maths/special_numbers/happy_number.py
Normal file
48
maths/special_numbers/happy_number.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
def is_happy_number(number: int) -> bool:
|
||||
"""
|
||||
A happy number is a number which eventually reaches 1 when replaced by the sum of
|
||||
the square of each digit.
|
||||
|
||||
:param number: The number to check for happiness.
|
||||
:return: True if the number is a happy number, False otherwise.
|
||||
|
||||
>>> is_happy_number(19)
|
||||
True
|
||||
>>> is_happy_number(2)
|
||||
False
|
||||
>>> is_happy_number(23)
|
||||
True
|
||||
>>> is_happy_number(1)
|
||||
True
|
||||
>>> is_happy_number(0)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: number=0 must be a positive integer
|
||||
>>> is_happy_number(-19)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: number=-19 must be a positive integer
|
||||
>>> is_happy_number(19.1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: number=19.1 must be a positive integer
|
||||
>>> is_happy_number("happy")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: number='happy' must be a positive integer
|
||||
"""
|
||||
if not isinstance(number, int) or number <= 0:
|
||||
msg = f"{number=} must be a positive integer"
|
||||
raise ValueError(msg)
|
||||
|
||||
seen = set()
|
||||
while number != 1 and number not in seen:
|
||||
seen.add(number)
|
||||
number = sum(int(digit) ** 2 for digit in str(number))
|
||||
return number == 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
Loading…
Reference in New Issue
Block a user