示例#1
0
def sol():
    '''
    Solution to project euler problem.
    '''
    def rotate(string, n):
        '''
        Returns rotated string, shifted by n positions
        '''
        return string[n:] + string[:n]

    def isCircular(n):
        '''
        Returns true if all permutations of a given prime are also prime.
        '''
        a = []
        for i in range(len(str(n))):
            a.append(int(rotate(str(n), i)))

        for val in a:
            if not pe.isPrime(val):
                return False
        return True

    primes = [n for n in range(2, 1000000) if pe.isPrime(n)]
    circular_values = [i for i in primes if isCircular(i)]

    return len(circular_values)
示例#2
0
def sol():
    '''
    Solution to project euler problem.
    '''
    def find_cycle(d):
        '''
        Return the length of the repeating portion of the decimal
        representation of 1/d where d is a positive integer.
        '''
        remainder = 10
        i = 0
        while remainder != 10 or i < 1:
            remainder = (remainder % d) * 10
            i += 1
        return i

    longest_cycle = 0
    current_cycle = 0
    max_d = 0
    sample_set = [
        i for i in range(2, 1000)
        if i % 2 != 0 and i % 5 != 0 and pe.isPrime(i)
    ]
    for i in sample_set:
        current_cycle = find_cycle(i)
        if current_cycle > longest_cycle:
            longest_cycle = current_cycle
            max_d = i
    return max_d
示例#3
0
def sol():
    '''
    Solution to project euler problem.
    '''
    sum = 0
    for i in range(2000000):
        if pe.isPrime(i):
            sum += i
    return (sum)
示例#4
0
 def count_consecutive_primes(a, b):
     '''
     Finds the number of consecutive primes generated by 
     y = n^2 + an + b.
     '''
     fx = 0
     for n in range(100000):
         fx = n * n + a * n + b
         if not pe.isPrime(fx):
             return n
示例#5
0
def sol():
    '''
    Solution to project euler problem.
    '''
    prime_divisors = []
    distinct_primes = []
    for i in range(134000, 135000):
        prime_divisors = [d for d in pe.divisors(i) if pe.isPrime(d)]
        if len(prime_divisors) == 4:
            distinct_primes.append(i)
    return distinct_primes
示例#6
0
    def isCircular(n):
        '''
        Returns true if all permutations of a given prime are also prime.
        '''
        a = []
        for i in range(len(str(n))):
            a.append(int(rotate(str(n), i)))

        for val in a:
            if not pe.isPrime(val):
                return False
        return True
示例#7
0
 def isTruncable(n):
     '''
     Returns if prime is truncable.
     '''
     truncated_vals = []
     a = str(n)
     for i in range(len(a)):
         truncated_vals.append(int(a[i:]))
         truncated_vals.append(int(a[:i+1]))
     for n in truncated_vals:
         if not pe.isPrime(n):
             return False
     return True
示例#8
0
def sol():
    '''
    Solution to project euler problem.
    '''
    primes = [n for n in range(1000000) if pe.isPrime(n)]
    temp_sum = 0
    sums = []
    for i in primes:
        temp_sum += i
        if temp_sum >= 1000000:
            break
        sums.append(temp_sum)

    return max(sums)
示例#9
0
def sol():
    '''
    Solution to project euler problem.
    '''
    # Generate permutations of digits 1-9
    digits = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
    perms = []
    perms_digits = []
    listprime = []
    for i in range(1, 10):
        perms = list(itertools.permutations(digits[:i]))
        perms_digits = [(int(''.join(j))) for j in perms]
        listprime += perms_digits
    return max([num for num in listprime if pe.isPrime(num)])
示例#10
0
def sol():
    '''
    Solution to project euler problem.
    '''
    primes = [i for i in range(1488, 10000) if pe.isPrime(i)]
    STEP = 3330
    for n in primes:
        perms = list(itertools.permutations(str(n)))
        int_perms = []
        for i in perms:
            res = ''
            for g in i:
                res += g
            int_perms.append(int(res))

        if n + STEP in int_perms and n + 2 * STEP in int_perms and n + STEP in primes and n + 2 * STEP in primes:
            return str(n) + str(n + STEP) + str(n + 2 * STEP)
示例#11
0
def sol():
    '''
    Solution to project euler problem.
    '''
    def isTruncable(n):
        '''
        Returns if prime is truncable.
        '''
        truncated_vals = []
        a = str(n)
        for i in range(len(a)):
            truncated_vals.append(int(a[i:]))
            truncated_vals.append(int(a[:i+1]))
        for n in truncated_vals:
            if not pe.isPrime(n):
                return False
        return True

    primes = [n for n in range(11,1000000) if pe.isPrime(n)]
    trunc_primes = [s for s in primes if isTruncable(s)]
    
    return sum(trunc_primes)