def main():
    known_prime_list = [2, 3, 5, 7]
    known_truncatable_prime = list()

    for i in prime_list(start_from=10):
        if len(known_truncatable_prime) == 11:
            print 'sum of all truncatable primes is {}'.format(
                sum(known_truncatable_prime))
            break

        known_prime_list.append(i)
        digit_len = len(str(i))
        is_truncatable = True
        #from left to right
        for n in range(1, digit_len):
            if int(str(i)[n:]) not in known_prime_list:
                # print '{} is not truncatable because of {}'.format(i, int(str(i)[n:]))
                is_truncatable = False
                break
        if is_truncatable:
            for n in range(1, digit_len):
                if int(str(i)[0:n]) not in known_prime_list:
                    #print '{} is not truncatable because of {}'.format(i, int(str(i)[0:n]))
                    break
            else:
                print 'found truncatable prime {}'.format(i)
                known_truncatable_prime.append(i)
Example #2
0
def prime_factorization(n):
    num = n
    sqr_root = int(math.sqrt(num))
    factors = prime.prime_list(sqr_root)
    prime_factor = []
    for i in factors:
        if i * i > num:
            prime_factor.append(num)
            return prime_factor
        while num % i == 0:
            num /= i
            prime_factor.append(i)
        if num == 1:
            return prime_factor
    prime_factor.append(num)
    return prime_factor
Example #3
0
def prime_factorization(n):
    num = n
    sqr_root = int(math.sqrt(num))
    factors = prime.prime_list(sqr_root)
    prime_factor = []
    for i in factors:
        if i*i > num:
            prime_factor.append(num)
            return prime_factor
        while num%i == 0:
            num /= i
            prime_factor.append(i)
        if num == 1:
            return prime_factor
    prime_factor.append(num)
    return prime_factor
Example #4
0
#-*- coding:utf-8 -*-

"""
The prime 41, can be written as the sum of six consecutive primes:

41 = 2 + 3 + 5 + 7 + 11 + 13
This is the longest sum of consecutive primes that adds to a prime below one-hundred.

The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

Which prime, below one-million, can be written as the sum of the most consecutive primes?
"""

import prime

primes = prime.prime_list(1000000)
primeSum = [0]*(len(primes)+1)
num = 0

for i in xrange(0, len(primes)):
    primeSum[i+1] = primeSum[i] + primes[i]

for i in xrange(0, len(primes)):
    j = i - (num + 1)
    while j >= 0:
        if primeSum[i] - primeSum[j] > 1000000:
            break
        if primeSum[i] - primeSum[j] in primes:
            num = i - j
            res = primeSum[i] - primeSum[j]
        j -= 1
 def test_prime_list_success(self):
     result = prime_list(3)
     self.assertEqual(list(result), [2,3,5])