Skip to content

Instantly share code, notes, and snippets.

@orjanv

orjanv/isnameprime.py

Last active Jul 23, 2020
Embed
What would you like to do?
Convert each character in a name to a number (a=1, b=2..) and check if it is a prime number summed.
#!/usr/bin/python3
# coding: utf-8
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
input_name = input('Enter a name: ')
# Check each character againts the alphabet list, and sum it
sum_name = (sum(chars.index(c) + 1 for c in input_name.lower().replace(" ", "").replace("-", "")))
# Check if the sum is a prime number or not
if all(sum_name % i for i in range(2, sum_name)):
print("{} is prime ({})".format(input_name, str(sum_name)))
else:
print("Sorry, {} is not prime".format(input_name))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment