Last active
February 10, 2022 21:26
-
-
Save orjanv/966a420e8853b901da12b6d19a066210 to your computer and use it in GitHub Desktop.
If your sum each letter in a name using a=1, b=2... # will the name be a prime number? Or even better, a palindrome?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# coding: utf-8 | |
# | |
# If you sum each letter in a name using a=1, b=2... | |
# will the name be a prime number? Or even better, a palindrome? | |
# | |
import sys | |
def main(): | |
'''Convert each character in a name to a number (a=1, b=2..) | |
and check if it is a prime number summed.''' | |
chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', \ | |
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'æ', 'ø', 'å'] | |
# Get a name input, can be one or more names | |
try: | |
input_name = sys.argv[1] | |
except: | |
input_name = input('Enter a name: ') | |
# Clean up name | |
input_name_clean = input_name.lower().replace(" ", "").replace("-", "") | |
# Check each character againts the alphabet list, and sum it | |
sum_name = (sum(chars.index(c) + 1 for c in input_name_clean)) | |
# Check if the sum is a prime number or not | |
if all(sum_name % i for i in range(2, sum_name)): | |
# Also, check if number is palindrom | |
reverse_name = str(sum_name)[::-1] | |
if sum_name == int(reverse_name): | |
print("{} is a palindromeprime ({})".format(input_name, str(sum_name))) | |
else: | |
print("{} is a prime ({})".format(input_name, str(sum_name))) | |
else: | |
print("{} is not a prime ({})".format(input_name, str(sum_name))) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment