Skip to content

Instantly share code, notes, and snippets.

@withs
Last active June 5, 2021 09:48
Show Gist options
  • Save withs/014cc9c43bd83b9aa57e231d2975f8d9 to your computer and use it in GitHub Desktop.
Save withs/014cc9c43bd83b9aa57e231d2975f8d9 to your computer and use it in GitHub Desktop.
Simple script wich try all 4x4 mumbers and get the highest palindrome of it in Python
"""
Simple script wich try all 4x4 mumbers and get the highest palindrome of it.
Results
-------
- 4x4 numbers (Imac 2013, i5, Python 3.9.2)
Highest palindrome -> 98344389
Found in -> 94.88048125600001 seconds
- results: 4x4 numbers (Macbook air 2020, M1, python 3.9.5)
Highest palindrome -> 98344389
Found in -> 38.435170750000005 seconds
"""
import timeit
def is_palindrome(number: int) -> bool:
"""Function to test if a number is palindrome
Parameters
----------
number : int
The number to check
Returns
-------
bool
Result if the number is palindrome
Notes
-----
Cast the int entry to a str to a list wich got reversed and
the list is joined to a string casted to an int and check if the entry is
equal to the reversed entry.
"""
return number == int("".join(reversed(list(str(number)))))
def process() -> None:
""" process function passed in timeit"""
highest_palindrome = 0
for i in range(1000, 9999):
for s in range(1000, 9999):
if is_palindrome((r := i*s)) and r > highest_palindrome:
highest_palindrome = r
print(f"Highest palindrome -> {highest_palindrome}")
time = timeit.timeit(process, number=1)
print(f"Found in -> {time} seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment