Skip to content

Updated automorphic.py #11696

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

Closed
wants to merge 6 commits into from
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
76 changes: 37 additions & 39 deletions maths/special_numbers/automorphic_number.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,57 @@
"""
== Automorphic Numbers ==
A number n is said to be a Automorphic number if
the square of n "ends" in the same digits as n itself.

Examples of Automorphic Numbers: 0, 1, 5, 6, 25, 76, 376, 625, 9376, 90625, ...
https://en.wikipedia.org/wiki/Automorphic_number
"""

# Author : Akshay Dubey (https://github.com/itsAkshayDubey)
# Time Complexity : O(log10n)
def is_automorphic_number(number: int, verbose: bool = False) -> bool:
"""
This function checks if a given number is an automorphic number.

Returns:
bool: True if the number is automorphic, False otherwise.

def is_automorphic_number(number: int) -> bool:
"""
# doctest: +NORMALIZE_WHITESPACE
This functions takes an integer number as input.
returns True if the number is automorphic.
>>> is_automorphic_number(-1)
False
>>> is_automorphic_number(0)
True
>>> is_automorphic_number(5)
True
>>> is_automorphic_number(6)
True
>>> is_automorphic_number(7)
False
>>> is_automorphic_number(25)
True
>>> is_automorphic_number(259918212890625)
True
>>> is_automorphic_number(259918212890636)
False
>>> is_automorphic_number(740081787109376)
True
>>> is_automorphic_number(5.0)
Traceback (most recent call last):
...
TypeError: Input value of [number=5.0] must be an integer
Raises:
TypeError: If the input number is not an integer.
ValueError: If the input number is negative.
"""
if not isinstance(number, int):
msg = f"Input value of [number={number}] must be an integer"
raise TypeError(msg)
if number < 0:
return False
msg = f"Input value of [number={number}] must be a non-negative integer"
raise ValueError(msg)
number_square = number * number
if verbose:
print(f"Square of {number}: {number_square}")
while number > 0:
if number % 10 != number_square % 10:
if verbose:
print(f"{number} is not an automorphic number")
return False
number //= 10
number_square //= 10
if verbose:
print(f"{number} is an automorphic number")
return True


def find_automorphic_numbers(n: int) -> list:
"""
This function finds all automorphic numbers up to a given number.

Args:
n (int): The upper limit.

Returns:
list: A list of automorphic numbers up to n.
"""
automorphic_numbers = []
for i in range(n + 1):
if is_automorphic_number(i):
automorphic_numbers.append(i)
return automorphic_numbers


if __name__ == "__main__":
import doctest

doctest.testmod()

# Example usage:
print(is_automorphic_number(25, verbose=True))
print(find_automorphic_numbers(100))
Loading