Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions maths/special_numbers/magic_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
== Magic Number ==
A number where the recursive sum of digits eventually results in 1.
Example: (172) ((1+7+2=10 -> 1+0=1)).
https://www.scribd.com/document/895653665/Interesting-Number-Programs
"""


def is_magic_number(number: int) -> bool:
"""
This functions takes an integer number as input.
returns True if the number is magic.
>>> is_magic_number(-1)
False
>>> is_magic_number(0)
False
>>> is_magic_number(172)
True
>>> is_magic_number(19)
True
>>> is_magic_number(124)
False
>>> is_magic_number(5.0)
Traceback (most recent call last):
...
TypeError: Input value of [number=5.0] must be an integer
"""
if not isinstance(number, int):
msg = f"Input value of [number={number}] must be an integer"
raise TypeError(msg)
if number <= 0:
return False
# the loop continues if n is 0

Check failure on line 33 in maths/special_numbers/magic_number.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W291)

maths/special_numbers/magic_number.py:33:35: W291 Trailing whitespace help: Remove trailing whitespace
# and sum is non-zero.
# It stops when n becomes

Check failure on line 35 in maths/special_numbers/magic_number.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W291)

maths/special_numbers/magic_number.py:35:30: W291 Trailing whitespace help: Remove trailing whitespace
# 0 and sum becomes single digit.
sum = 0

Check failure on line 37 in maths/special_numbers/magic_number.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (A001)

maths/special_numbers/magic_number.py:37:5: A001 Variable `sum` is shadowing a Python builtin
while (number > 0 or sum > 9):
if (number == 0):
number = sum
sum = 0

Check failure on line 41 in maths/special_numbers/magic_number.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (A001)

maths/special_numbers/magic_number.py:41:13: A001 Variable `sum` is shadowing a Python builtin
sum = sum + number % 10

Check failure on line 42 in maths/special_numbers/magic_number.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (A001)

maths/special_numbers/magic_number.py:42:9: A001 Variable `sum` is shadowing a Python builtin
number = number // 10

# Return true if sum becomes 1.
return sum == 1

if __name__ == "__main__":
import doctest

doctest.testmod()

Check failure on line 51 in maths/special_numbers/magic_number.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W292)

maths/special_numbers/magic_number.py:51:22: W292 No newline at end of file help: Add trailing newline
Loading