"""
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: as 1! = 1 and 2! = 2 are not sums they are not included.
"""

import permutations

fact = {}

for x in xrange(0, 10):
    f = 1
    for y in xrange(2, x + 1):
        f *= y

    fact[x] = f

factSum = 0

for i in xrange(10, 1000*100):  # found out experimentally
    if i == sum([fact[j] for j in permutations.split_into_digits(i)]):
        factSum += i

print str(factSum)
Example #2
0
The number, 197, is called a circular prime because all rotations of the digits:
197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?
"""

from collections import deque
import primes
import permutations

allPrimes = primes.create_primes(1000*1000)
countOfCircularPrimes = 0

for prime in allPrimes:
    isCircularPrime = True
    digits = deque(permutations.split_into_digits(prime))

    for i in xrange(0, len(digits)):
        digits.rotate(1)
        newPrime = permutations.combine_numbers(digits)

        if newPrime not in allPrimes:
            isCircularPrime = False
            break

    if isCircularPrime:
        countOfCircularPrimes += 1

print str(countOfCircularPrimes)