Categories
Koding

If my name was a number, is it perhaps a prime number?

My wife is reading a book at the moment: “The Curious Incident of the Dog in the Night-Time by Mark Haddon

Sitting in our sofa, she read out aloud for me from one of the pages, something she knew I would enjoy:

“If you see someone’s name and you give each letter a value from 1 to 26 (a = 1, b = 2 etc.) and you add the numbers up in your head and you find that it makes a prime number, like Jesus Christ…”

Firstly, I don’t have autism or such gifts to compute that in my head right away. Second, I fancy new challenges to tackle using programming.

So I wrote a small script and at the same time learned to write comprehensions in Python, hey! The script asks for on or more names in the Latin alphabet (26 letters in the English alphabet and 29 letters in the Norwegian), converts each character in the name to a number (a=1, b=2..) and check if it is a prime number summed:

The script:

#!/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()
view raw isnameprime.py hosted with ❤ by GitHub

By hoyd

Communication adviser at Andøya Space.