Пример #1
0
def problem_5(num = 20):
    """# 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?
"""

    primes = utils.get_primes_below(num)
    factors = [pow(prime, int(math.log(num)/math.log(prime))) for prime in primes]

    value = reduce(operator.mul, factors, 1)
    return value 
Пример #2
0
def problem_27():
    max_n_estimate = 200
    primes = set(utils.get_primes_below(max_n_estimate**2 + max_n_estimate*1000 + 1000))
    b_primes = utils.get_primes_below(1000)

    best_count = 0
    best_a = 0
    best_b = 0
    for a in xrange(-999,1000):
        for b in b_primes:
            n = 1
            while(True):
                if (n**2 + n*a + b) in primes:
                    n+=1
                else:
                    break
            if n > best_count:
                best_count = n
                best_a = a
                best_b = b
    return best_a * best_b
Пример #3
0
def problem_5(num=20):
    """# 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?
"""

    primes = utils.get_primes_below(num)
    factors = [
        pow(prime, int(math.log(num) / math.log(prime))) for prime in primes
    ]

    value = reduce(operator.mul, factors, 1)
    return value
Пример #4
0
def problem_37():
    def is_truncatable_both_directions(prime, primes):
        
        str_prime = str(prime)
        for i in xrange(len(str_prime)):
            if not (int(str_prime[i:]) in primes and int(str_prime[:i+1]) in primes):
                return False
        return True

    primes = set(utils.get_primes_below(1000000))

    results = [prime for prime in primes if is_truncatable_both_directions(prime, primes)]
    return sum(results[4:])
Пример #5
0
def problem_27():
    max_n_estimate = 200
    primes = set(
        utils.get_primes_below(max_n_estimate**2 + max_n_estimate * 1000 +
                               1000))
    b_primes = utils.get_primes_below(1000)

    best_count = 0
    best_a = 0
    best_b = 0
    for a in xrange(-999, 1000):
        for b in b_primes:
            n = 1
            while (True):
                if (n**2 + n * a + b) in primes:
                    n += 1
                else:
                    break
            if n > best_count:
                best_count = n
                best_a = a
                best_b = b
    return best_a * best_b
Пример #6
0
def problem_37():
    def is_truncatable_both_directions(prime, primes):

        str_prime = str(prime)
        for i in xrange(len(str_prime)):
            if not (int(str_prime[i:]) in primes
                    and int(str_prime[:i + 1]) in primes):
                return False
        return True

    primes = set(utils.get_primes_below(1000000))

    results = [
        prime for prime in primes
        if is_truncatable_both_directions(prime, primes)
    ]
    return sum(results[4:])
Пример #7
0
def problem_50():
    primes = utils.get_primes_below(1000000)
    primes_set = set(primes)
    max_length = 0
    best_prime = 0

    for i, start_prime in enumerate(primes):
        num_sum = 0
        for j, add_prime in enumerate(primes[i:]):
            num_sum += add_prime
            if num_sum > primes[-1]:
                break
            if num_sum in primes_set:
                if j + 1 > max_length:
                    max_length = j + 1
                    best_prime = num_sum

    return best_prime
Пример #8
0
def problem_50():
    primes = utils.get_primes_below(1000000)
    primes_set = set(primes)
    max_length = 0
    best_prime = 0

    for i, start_prime in enumerate(primes):
        num_sum = 0
        for j, add_prime in enumerate(primes[i:]):
            num_sum += add_prime
            if num_sum > primes[-1]:
                break
            if num_sum in primes_set:
                if j + 1 > max_length:
                    max_length = j + 1
                    best_prime = num_sum

    return best_prime
Пример #9
0
def problem_49():
    primes = np.array(utils.get_primes_below(10000))
    primes = primes[primes > 999]
    primes_set = set(primes)
    sets_of_3 = []
    for i, primei in enumerate(primes[:-4]):
        for j, primej in enumerate(primes[i + 1:]):
            step = primej - primei
            if (primej + step) in primes_set:
                sets_of_3.append((primei, primej, primej + step))

    answers = []
    for s in sets_of_3:
        sets = [set(str(x)) for x in s]
        if all(e == sets[0] for e in sets):
            answers.append(s)

    answer = answers[1] #is this cheating? it says there are only two and specifies which it wants, turns out it is this one...
    answer = [str(x) for x in answer]
    return int(reduce(operator.add, answer))
Пример #10
0
def problem_46():
    # try brute forcing it
    primesl = utils.get_primes_below(10000)
    primess = set(primesl)
    num_primes = len(primesl)

    for current_num in xrange(9, primesl[-1], 2):
        if current_num in primess:
            continue

        found_solution = False
        for prime in primesl:
            if prime > current_num:
                break
            res = np.sqrt((current_num - prime)/2)
            if res == int(res):
                found_solution = True
                break

        if not found_solution:
            return current_num
Пример #11
0
def problem_46():
    # try brute forcing it
    primesl = utils.get_primes_below(10000)
    primess = set(primesl)
    num_primes = len(primesl)

    for current_num in xrange(9, primesl[-1], 2):
        if current_num in primess:
            continue

        found_solution = False
        for prime in primesl:
            if prime > current_num:
                break
            res = np.sqrt((current_num - prime) / 2)
            if res == int(res):
                found_solution = True
                break

        if not found_solution:
            return current_num
Пример #12
0
def problem_35():
    def rotate_num(n):
        if n < 10:
            return n
        chars = [c for c in str(n)]
        chars.insert(0, chars.pop())
        return int(reduce(operator.add, chars))

    primes = set(utils.get_primes_below(1000000))
    count = 0
    for p in primes:
        worked = True
        for i in xrange(len(str(p))):
            p = rotate_num(p)
            if not p in primes:
                worked = False
                break

        if worked:
            count += 1
    return count
Пример #13
0
def problem_35():
    def rotate_num(n):
        if n < 10:
            return n
        chars = [c for c in str(n)]
        chars.insert(0, chars.pop())
        return int(reduce(operator.add, chars))

    primes = set(utils.get_primes_below(1000000))
    count = 0
    for p in primes:
        worked = True
        for i in xrange(len(str(p))):
            p = rotate_num(p)
            if not p in primes:
                worked = False
                break

        if worked:
            count += 1
    return count
Пример #14
0
def problem_49():
    primes = np.array(utils.get_primes_below(10000))
    primes = primes[primes > 999]
    primes_set = set(primes)
    sets_of_3 = []
    for i, primei in enumerate(primes[:-4]):
        for j, primej in enumerate(primes[i + 1:]):
            step = primej - primei
            if (primej + step) in primes_set:
                sets_of_3.append((primei, primej, primej + step))

    answers = []
    for s in sets_of_3:
        sets = [set(str(x)) for x in s]
        if all(e == sets[0] for e in sets):
            answers.append(s)

    answer = answers[
        1]  #is this cheating? it says there are only two and specifies which it wants, turns out it is this one...
    answer = [str(x) for x in answer]
    return int(reduce(operator.add, answer))
Пример #15
0
def problem_10():
    """The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million."""
    return sum(utils.get_primes_below(2000000))
Пример #16
0
def problem_7():
    """By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. 
What is the 10 001st prime number?"""
    primes = utils.get_primes_below(1000000)
    return primes[10000]
Пример #17
0
def problem_7():
    """By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. 
What is the 10 001st prime number?"""
    primes = utils.get_primes_below(1000000)
    return primes[10000]
Пример #18
0
def problem_10():
    """The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million."""
    return sum(utils.get_primes_below(2000000))