Instantly share code, notes, and snippets.

# hugovk/todayschallenge.py Created Sep 3, 2015

Convert the letters of your full name into numbers, add them together, & determine the prime factorization of the sum
 #!/usr/bin/env python # encoding: utf-8 """ Today's challenge: Convert the letters of your full name into numbers, add them together, & determine the prime factorization of the sum https://twitter.com/todayschallenge/status/638713672278351875 https://twitter.com/todayschallenge/status/639075809060356096 """ from __future__ import print_function, unicode_literals import argparse # How many points does each letter have (in the font in this text editor)? NONE = "BDO" ONE = "Q" TWO = "ACGJLMNPRSUVWZ" THREE = "EFTY" FOUR = "HIKX" def letters_into_numbers(words): sum = 0 for c in words.upper(): if c in NONE: sum += 0 elif c in ONE: sum += 1 elif c in TWO: sum += 2 elif c in THREE: sum += 3 elif c in FOUR: sum += 4 print("Sum = ", sum) return sum def prime_factorisation(number): # http://www.calculatorsoup.com/calculators/math/prime-factors.php saved = [] divider = 2 while(True): result = number / float(divider) print(number, "/", divider, "=", result, " integer:", result.is_integer()) if result.is_integer(): saved.append(divider) print("Save:", divider) number = result else: divider += 1 if result == 1: break print("Prime factorisation:", saved) return saved if __name__ == "__main__": parser = argparse.ArgumentParser( description="Today's challenge: Convert the letters of your full name " "into numbers, add them together, & determine the prime factorization " "of the sum", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('name', help="A name") args = parser.parse_args() sum = letters_into_numbers(args.name) result = prime_factorisation(sum) # End of file
Owner Author

### hugovk commented Sep 3, 2015

 ``````\$ python todayschallenge.py "hugovk" Sum = 14 14 / 2 = 7.0 integer: True Save: 2 7.0 / 2 = 3.5 integer: False 7.0 / 3 = 2.33333333333 integer: False 7.0 / 4 = 1.75 integer: False 7.0 / 5 = 1.4 integer: False 7.0 / 6 = 1.16666666667 integer: False 7.0 / 7 = 1.0 integer: True Save: 7 Prime factorisation: [2, 7] ``````
Owner Author

### hugovk commented Sep 3, 2015

 ``````\$ python todayschallenge.py "Today's Challenge" Sum = 32 32 / 2 = 16.0 integer: True Save: 2 16.0 / 2 = 8.0 integer: True Save: 2 8.0 / 2 = 4.0 integer: True Save: 2 4.0 / 2 = 2.0 integer: True Save: 2 2.0 / 2 = 1.0 integer: True Save: 2 Prime factorisation: [2, 2, 2, 2, 2] ``````