示例#1
0
"""
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?
"""

from math import sqrt

import primes
import permutations

primesToCheck = primes.create_primes(sqrt(987654321))
primeFound = False


def is_prime(number):
    limit = sqrt(number)
    for p in primesToCheck:
        if p > limit:
            return True
        elif number % p == 0:
            return False

    return True


for x in xrange(10, 1, -1):
    allPermutations = permutations.permutations(range(1, x))

    for perm in sorted([permutations.combine_numbers(y) for y in allPermutations], reverse=True):
"""
The number 3797 has an interesting property. Being prime itself,
it is possible to continuously remove digits from left to right,
and remain prime at each stage: 3797, 797, 97, and 7.
Similarly we can work from right to left: 3797, 379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to right and right to left.

NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
"""

import primes

allPrimes = primes.create_primes(1000*1000)


def is_left_truncatable(prime):
    for i in xrange(1, len(str(prime))):
        if (prime % (10 ** i)) not in allPrimes:
            return False
    return True


def is_right_truncatable(prime):
    for i in xrange(1, len(str(prime))):
        if (prime / (10 ** i)) not in allPrimes:
            return False
    return True


sumOfTruncatablePrimes = 0
"""
The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?
"""

import math
import primes

highestNumber = 600851475143
highestPrime = int(math.sqrt(highestNumber))
relevantPrimes = reversed(primes.create_primes(highestPrime))

for p in relevantPrimes:
    if highestNumber % p == 0:
        print str(p)
        break
"""
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.
"""

import primes

print str(sum(primes.create_primes(2 * 1000 * 1000)))
"""
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
"""

import primes

primeNumbers = primes.create_primes(20)
primeFactors = {}


def get_prime_factors(number):
    prime_factors = {}
    original_number = number

    for p in primeNumbers:
        number = original_number
        factor = 0

        while number % p == 0:
            number /= p
            factor += 1

        prime_factors[p] = factor

    return prime_factors


def merge_prime_factors(new_prime_factors):
    for p in new_prime_factors: