Example #1
0
"""
Project Euler Problem 41
========================

We shall say that an n-digit number is pandigital if it makes use of all
the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital
and is also prime.

What is the largest n-digit pandigital prime that exists?
"""


import euler_utils as utils

primes = utils.primes_till((10**7))

ndigit = [str(i) for i in range(1,10)]
#print ndigit

for i in range(len(primes)-1, 1, -1):
    number_str = str(primes[i])
    length = len(number_str)
    if len(set(number_str).intersection(set(ndigit[0:length]))) == length:
        print primes[i]
        break

Example #2
0
"""
Project Euler Problem 35
========================

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?
"""


import euler_utils as utils

primes = set(utils.primes_till(1000000))

count = 0

for num in primes:
    num_str = str(num)
    if all([x in primes for x in [int(num_str[i:]+num_str[:i])
                                  for i in range(len(num_str))]]):
        count += 1

print count
Example #3
0
Let d(n) be defined as the sum of proper divisors of n (numbers less than
n which divide evenly into n).
If d(a) = b and d(b) = a, where a =/= b, then a and b are an amicable pair
and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22,
44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1,
2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.
"""

import euler_utils as utils

no_op_set = set(utils.primes_till(10000))

dn_map = {}
amicable_pairs = []

dn = lambda x:sum(utils.divisors_of(x))

for num in range(4,10001):
    if num not in no_op_set and num not in dn_map:
        dsum = dn(num)
        dn_map[num] = dsum
        if dsum < 10000 and dsum > num:
            dsum_sum = dn(dsum)
            if dsum_sum == num:
                amicable_pairs.append((num, dsum))
            dn_map[dsum] = dsum_sum