Esempio n. 1
0
def truncatable_prime(n):
    if n < 10: return False
    for i in range(1, len(str(n))):
        if not euler.is_prime(int(str(n)[i:])) or not euler.is_prime(
                int(str(n)[:-i])):
            return False
    return True
Esempio n. 2
0
def main():
    family_size = 8
    smallest_prime = 0
    digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    is_found = False
    p = 11
    while not is_found:
        if not euler.is_prime(p):
            p += 1
            continue

        s = str(p)
        family = []
        for c in s:
            family.clear()
            for d in digits:
                t = str.replace(s, c, d)
                if len(str(int(t))) == len(str(int(s))):
                    family.append(t)
            family = [int(x) for x in family]
            family = [x for x in family if euler.is_prime(x)]

            if len(family) == family_size:
                smallest_prime = min(family)
                is_found = True
        p += 1

    print(smallest_prime)
Esempio n. 3
0
def trunc_prime(prime):
    prime = str(prime)
    for num in range(1, len(prime)):
        if not euler.is_prime(int(prime[num:])) or not euler.is_prime(
                int(prime[:-num])):
            return False
    return True
Esempio n. 4
0
File: 37.py Progetto: fx8848/code
def trunc(n):
  c = n
  while c>10:
    c = c % ( 10**(int(log10(c))) )
    n = n//10
    if not is_prime(c) or not is_prime(n): return False 
  return True
Esempio n. 5
0
def trunc_prime(prime): 
    prime = str(prime)
    for num in range(1, len(prime)):
        print int(prime[num:]), int(prime[:num])
        if not euler.is_prime(int(prime[num:])) or not euler.is_prime(int(prime[:-num])):
            return False
    return True  
Esempio n. 6
0
def truncatable_prime(n):
    if n < 10:
        return False
    for i in range(1, len(str(n))):
        if not euler.is_prime(int(str(n)[i:])) or not euler.is_prime(int(str(n)[:-i])):
            return False
    return True
Esempio n. 7
0
def p060(primes, num):
    num_str = str(num)
    for i in primes:
        pr_str = str(i)
        if not (is_prime(int(num_str + pr_str)) and is_prime(int(pr_str + num_str))):
            return False

    return True
Esempio n. 8
0
def goldbach(n):
    if n % 2 == 0 or euler.is_prime(n):
        return False
    for i in itertools.count(1):
        k = n - 2 * (i * i)
        if k <= 0:
            return True
        elif euler.is_prime(k):
            return False
Esempio n. 9
0
File: 37.py Progetto: higgsd/euler
def istrunc(n):
    if not euler.is_prime(n):
        return False
    s = str(n)
    for i in xrange(1, len(s)):
        if not euler.is_prime(int(s[i:])):
            return False
        if not euler.is_prime(int(s[:-i])):
            return False
    return True
Esempio n. 10
0
def is_trunc(n):
    if not euler.is_prime(n):
        return False
    d = euler.digits(n)
    for c in range(1,len(d)):
        lt = euler.num_from_digits(d[:-c])
        rt = euler.num_from_digits(d[c:])
        if not (euler.is_prime(lt) and euler.is_prime(rt)):
            return False
    return True
Esempio n. 11
0
def prime_factors(num):
	factors_set = set()
	for multiple in xrange(3, int(math.sqrt(num))+1, 2):
		if num%multiple == 0:
			other_multiple = num/multiple
			if is_prime(multiple):
				factors_set.add(multiple)
			elif is_prime(other_multiple):
				factors_set.add(other_multiple)
	return factors_set
Esempio n. 12
0
def prime_factors(num):
	factors_set = set()
	limit = int(math.sqrt(num))+1
	for multiple in xrange(2,limit+1):
		if num%multiple == 0:
			other_multiple = num/multiple
			if is_prime(multiple):
				factors_set.add(multiple)
			if is_prime(other_multiple):
				factors_set.add(other_multiple)
	return factors_set
Esempio n. 13
0
def is_truncatable_prime(n):
    if not euler.is_prime(n):
        return False
    n = str(n)
    for i in range(1, len(n)):
        left = int(n[i:])
        right = int(n[:-i])
        if not euler.is_prime(left):
            return False
        if not euler.is_prime(right):
            return False
    return True
Esempio n. 14
0
def main():
    targets = []
    i = 10
    while len(targets) != 11:
        if is_prime(i):
            for j in remove_digits(i):
                if not is_prime(j):
                    break
            else:
                targets.append(i)
        i += 1
    # show results
    print sum(targets)
Esempio n. 15
0
def main():
     primes = []
     for n in range(2, 1000000):
         if euler.is_prime(n):
             is_prime = True
             for i in range(len(str(n))):
                 p = int(rotate(str(n)[:], i))
                 if not euler.is_prime(p):
                     is_prime = False
                     break
             if (is_prime):
                primes.append(n)
     print(primes)
     print(len(primes))
Esempio n. 16
0
def main():
    result = ''
    for n in range(1488, 10000):
        n_2 = n + 3330
        n_3 = n + 2 * 3330
        if euler.is_prime(n) and euler.is_prime(n_2) and euler.is_prime(n_3):
            n_str = str(n)
            n_2_str = str(n_2)
            n_3_str = str(n_3)
            perms = [''.join(p) for p in itertools.permutations(n_str)]
            if n_2_str in perms and n_3_str in perms:
                result = n_str + n_2_str + n_3_str
                break
    print(result)
Esempio n. 17
0
def is_truncatable(n):
    # Left
    i = 10
    while i <= n:
        if not euler.is_prime(n % i):
            return False
        i *= 10

    # Right
    while n > 0:
        if not euler.is_prime(n):
            return False
        n //= 10
    return True
Esempio n. 18
0
def decom(x, y):
    primes = primesieve(y, math.ceil(x / 2))
    if y > x / 2:
        if is_prime(x):
            return 1
        else:
            return 0
    elif y == x / 2 and is_prime(y):
        return 1
    else:
        counter = 0
        for i in primes:
            counter += decom(x - i, i)
        counter += decom(x, x)
        return counter
Esempio n. 19
0
def main():
    n = 9
    while True:
        if not euler.is_prime(n) and not euler.is_sum_of_prime_and_twice_square(n):
            break;
        n += 2
    print(n)
Esempio n. 20
0
def quadratic_primes(n):
    a = range(-n, n)
    b = range(40, n)
    for j in b:
        for i in a:
            if is_prime(j):
                n = 0
                while True:
                    t = n**2 + n * i + j
                    n += 1
                    if t < 0:
                        break
                    if not is_prime(t):
                        # print 'np break', n-1, i, j
                        yield n - 1, i, j
                        break
Esempio n. 21
0
def getConcatenated(primes):
	primes = [str(prime) for prime in primes]
	conCats = [int(''.join(p)) for p in permutations(primes, 2)]
	for c in conCats:
		if not is_prime(c):
			return False
	return True
Esempio n. 22
0
def prime_factors(n):
    count=0
    for i in range(1,n,1):
        if(n%i==0) and is_prime(i):
            count+=1
            if(count==4):
                return n
Esempio n. 23
0
def quadratic_primes(n):
    a = range(-n, n)
    b = range(40, n)
    for j in b:
        for i in a:
            if is_prime(j):
                n = 0
                while True:
                    t = n ** 2 + n * i + j
                    n += 1
                    if t < 0:
                        break
                    if not is_prime(t):
                        # print 'np break', n-1, i, j
                        yield n - 1, i, j
                        break
Esempio n. 24
0
def find_prime_pandigitals(upper_limit):
    digits = range(1, upper_limit + 1)
    pandigitals = [
        int(''.join(str(digit) for digit in n)) for n in permutations(digits)
    ]
    prime_pandigitals = [n for n in pandigitals if is_prime(n)]
    return prime_pandigitals
Esempio n. 25
0
def largest(n):
    current = n
    for i in range(2, n + 1):
        while current % i == 0:
            current //= i
        if (euler.is_prime(current)):
            return current
Esempio n. 26
0
def coefficientsMaxPrimes(coeff_limit):
    max_n_primes = 40  # known result from Euler
    max_a = 1  # known result from Euler
    max_b = 41  # known result from Euler

    for b in primes():
        if b <= 41:
            continue  # Euler's result is better
        if b > coeff_limit:
            break

        # to generate at least `max_n_primes` primes, a should start from here
        start = -(max_n_primes + b // max_n_primes)

        # a should be odd
        if start % 2 == 0:
            start -= 1

        for a in range(start, 0, 2):
            n_primes = 0

            while is_prime(n_primes**2 + a * n_primes + b):
                n_primes += 1

            if n_primes > max_n_primes:
                max_n_primes = n_primes
                max_a = a
                max_b = b
    return max_a, max_b
Esempio n. 27
0
def is_prime(n):

    if n < 0:
        return False
    elif n < len(cache):
        return cache[n]
    else:
        return euler.is_prime(n)
Esempio n. 28
0
def solution():
    i = 0
    primes_found = 0
    while primes_found < 10001:
        i += 1
        if euler.is_prime(i):
            primes_found += 1
    return i
Esempio n. 29
0
def kst_prime(k):
    i = 0
    while True:
        i += 1
        if is_prime(i):
            k -= 1
            if k < 0:
                return i
Esempio n. 30
0
def is_circular_prime(prime):
	digits = [c for c in str(prime)]
	for x in xrange(0,len(digits)):
		digits.append(digits.pop(0))
		rotated_num = int(''.join(digits)) 
		if not is_prime(rotated_num):
			return False
	return True
Esempio n. 31
0
def main():
    # primes = [3, 7, 109, 673, 129976621]
    primes = []
    upper_bound = 10000
    num_primes = 5

    for n in range(1, upper_bound):
        if euler.is_prime(n):
            primes.append(n)

    # build up a list of unique pairs of primes and concatenate to primes
    combos = itertools.combinations(primes, 2)
    pairs = []
    for pair in combos:
        p_1 = int(str(pair[0]) + str(pair[1]))
        p_2 = int(str(pair[1]) + str(pair[0]))
        if euler.is_prime(p_1) and euler.is_prime(p_2):
            pairs.append(pair)

    pairs = [sorted(list(p)) for p in pairs]
    pairs = {tuple(p) for p in pairs}

    print('Starting with ', len(primes), ' primes and ', len(pairs), ' pairs for ', len(primes) * len(pairs), ' operations')

    S = [list(p) for p in pairs]
    for i in range(2, num_primes):
        print('len(S) = ', len(S))
        new_S = []
        for p in primes:
            for s in S:
                concats_all = True
                for n in s:
                    p_1 = int(str(n) + str(p))
                    p_2 = int(str(p) + str(n))
                    if not euler.is_prime(p_1) or not euler.is_prime(p_2):
                        concats_all = False
                        break
                if concats_all:
                    new_S = new_S + [s + [p]]
        S = [sorted(s) for s in new_S]
        S = {tuple(s) for s in S}
        S = [list(s) for s in S]

    print(S)
    print(min([sum(s) for s in S]))
Esempio n. 32
0
    def find_prime(n):
        count = 0
        prime = 1

        while count < n:
            prime += 1
            if euler.is_prime(prime):
                count += 1
        return prime
Esempio n. 33
0
def main():
    digits = '123456789'
    max_prime = 0
    for i in range(1, len(digits) + 1):
        for p in itertools.permutations(digits[:i]):
            n = int(''.join(p))
            if euler.is_prime(n) and n > max_prime:
                max_prime = n
    print(max_prime)
Esempio n. 34
0
def compute():
  n = 10001
  i = 2
  primes = 0
  while (primes < n):
    if (euler.is_prime(i)): 
      primes += 1
    i += 1
  return i-1
Esempio n. 35
0
def f():
    i = 9
    while i > 0:
        for t in permutations(''.join(str(d) for d in range(i, 0, -1)), i):
            n = int(''.join(t))
            if is_prime(n):
                return n
        else:
            i -= 1
Esempio n. 36
0
def solution_1():
    n = 3
    SumPrimes = 2

    while n < 2 * (10 ** 6):
        if euler.is_prime(n) == True:
            SumPrimes = SumPrimes + n
        n = n + 2
    return SumPrimes
Esempio n. 37
0
def list_prime(num=None):
    i = 1
    gen = number_generator()
    while True:
        n = gen.next()
        if is_prime(n):
            i += 1
        if i >= num:
            return i, n
Esempio n. 38
0
def solution_1():
    n = 3
    SumPrimes = 2

    while n < 2 * (10**6):
        if euler.is_prime(n) == True:
            SumPrimes = SumPrimes + n
        n = n + 2
    return SumPrimes
Esempio n. 39
0
File: 27.py Progetto: fx8848/code
def get_primes_num(a, b):
	n = 0
	while 1:
		value = n*n + a*n +b
		if value <= 0:
			break
		if not is_prime(value):
			break
		n += 1
	return n
Esempio n. 40
0
def check_prime(num):
    length = len(str(num))
    inc = 1
    while inc < length:
        num = rotate_left(num)
        if not euler.is_prime(int(num)):
            return False
        inc += 1
  #  print num
    return True
Esempio n. 41
0
def truncatables():
    truncatable = []
    for d in itertools.count(2):
        # similar to 035 - 1379 only digits allowed - wrong; can start w/2,5
        for c0 in ['2','3','5','7']:
            for c in itertools.product(['1','3','7','9'], repeat=d-1):
                p = c0+''.join(list(c))
                if int(p) in truncatable or not is_prime(int(p)):
                    continue

                trunc = True
                for i in range(1,d):
                    if not is_prime(int(p[i:])) or not is_prime(int(p[:d-i])):
                        trunc = False
                        break
                if trunc:
                    truncatable.append(int(p))
                if len(truncatable) == 11:
                    return truncatable
Esempio n. 42
0
def main():
    count = 0
    for n in xrange(2, 1000000):
        for each in rotate_int(n):
            if not is_prime(each):
                break
        else:
            count += 1
            # print n
    print count
Esempio n. 43
0
def is_conjecture(n):
    if is_prime(n):
        return False
    if not n % 2:
        return False
    c = product(PRIMES, [2 * i ** 2 for i in range(1, 100)])
    for p in c:
        if n == p[0] + p[1]:
            return True
    return False
Esempio n. 44
0
def main():
    for i in odd_composite():
        j = 1
        while True:
            remain = i - 2*j**2
            if remain <= 1:
                return i
            if is_prime(remain):
                break
            j += 1
Esempio n. 45
0
def PrimeFactors(n):
    if n%2 ==0:
        print(2)
    #for j in range(1,n,2):
    j=1
    while j!=n and n!=1:
        j=j+2
        if euler.is_prime(j)== True and n%j==0:
            n=n/j
            print(j)
Esempio n. 46
0
def largest_pandigital_prime():
    """Input: None.
       Output: The largest n-digit pandigital prime."""
    digits = list(map(str, reversed(range(1, 8))))
    for i in list(map(int, digits)):
        gen = permutations(digits)
        for j in range(factorial(i)):
            result = int(''.join(next(gen)))
            if is_prime(result):
                return result
        digits.remove(str(i))
Esempio n. 47
0
def solution(): 
    num = 600851475143
    x = 2
    while True:
        if euler.is_prime(x) and num % x == 0:
            num /= x
            # found last and largest prime number
            # this will not work for some numbers, todo: find a clean fix
            if num == 1: 
                return x
        else:
            x += 1
def is_hamming_number(n, x):  #standard hamming numbers have x = 5
    if n < 0:
        return False
    factor_list = [n]
    result = factor_list
    for counter in range(1, int(sqrt(n)) + 1):
        result = list(set(factor_list))
        if n % counter == 0:
            if is_prime(counter):
                if counter > x:
                    return False
            if is_prime(int(n / counter)):
                if int(n / counter) > x:
                    return False
                else:
                    factor_list.append(counter)
                    factor_list.append(int(n / counter))
                counter += 1
        else:
            counter += 1
    else:
        return result
Esempio n. 49
0
def main():
    start = 200
    global primes
    primes = prime_list(start)
    n = 33

    while True:
        n += 2
        if n > primes[len(primes) - 1]:
            primes += prime_list(n, start)
            start = n
        if not is_prime(n) and not goldbach(n):
            break
    print(n)
Esempio n. 50
0
def main():
    maximum_l = 0
    maximum_a = 0
    maximum_b = 0
    lst = euler.prime_list(1000)
    for b in lst:
        for a in range(-b + 2, 1001, 2):
            n = 0
            length = 0
            c = b
            while c > 1 and euler.is_prime(c, lst):
                # miller-rabin is slow because they all are primes
                n += 1
                length += 1
                c = n ** 2 + a * n + b
            if length > maximum_l:
                maximum_l = length
                maximum_a = a
                maximum_b = b
    return maximum_a * maximum_b
Esempio n. 51
0
from euler import is_prime

answer = 0
counter = 0
prime_counter = 0
while prime_counter != 10001:
    counter += 1
    if is_prime(counter):
        prime_counter += 1
        answer = counter
print(answer)
Esempio n. 52
0
from euler import is_prime, rotations, prime_list

circular_primes = []
primes = prime_list(1000000)
for p in primes:
    print(p)
    rots = rotations(str(p))
    circular = True
    for r in rots:
        if not is_prime(int(r)):
            circular = False
    if circular:
        circular_primes.append(p)

print(len(circular_primes))
print(circular_primes)
Esempio n. 53
0
from euler import is_prime
from tqdm import *

answer = 0
for i in tqdm(range(2000000, 1, -1)):
    if is_prime(i):
        answer += i
print(answer)
Esempio n. 54
0
 def wrapper(*args):
     s = func(*args)
     if is_prime(args[0]):
         return set([1])
     return s.difference(args)
Esempio n. 55
0
File: 27.py Progetto: markhend/pe
from decimal import *
from time import clock
import euler

# getcontext().prec=2010
t = clock()

a = list(range(-999, 1000))
b = list(range(-999, 1000))
ans = 0
consecutive = 0

for i in a:
    for j in b:
        for n in range(0, 80):
            chk = n * n + i * n + j
            if chk < 2 or not (euler.is_prime(chk)):
                break
            consecutive += 1
        if consecutive > ans:
            ans = consecutive
            coeff_a = i
            coeff_b = j
            # print coeff_a, coeff_b, ans
        consecutive = 0

print(coeff_a, coeff_b, ans, 'answer is', coeff_a * coeff_b)

print('time is', clock() - t)
Esempio n. 56
0
# -*- coding:utf-8 -*-
"""Project Euler problem 46"""

from euler import get_plist, is_prime

mx = 10**6
primes = set(get_plist(mx))
ans = 0
for odd in range(9, mx, 2):
    if is_prime(odd, primes):
        continue
    flg = True

    for j in [i * i * 2 for i in range(int((mx**0.5) / 2) + 1)]:
        if j >= odd: break

        if is_prime(odd - j, primes):
            flg = False
            break
    if flg:
        ans = odd
        break
print("Answer: " + str(ans))
Esempio n. 57
0
# However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible
# by 41, and certainly when n = 41, 41² + 41 + 41 is clearly divisible
# by 41. Using computers, the incredible formula n² - 79n + 1601 was
# discovered, which produces 80 primes for the consecutive values n =
# 0 to 79. The product of the coefficients, -79 and 1601, is -126479.

# Considering quadratics of the form: n² + an + b, where |a| < 1000
# and |b| < 1000. Find the product of the coefficients, a and b, for
# the quadratic expression that produces the maximum number of primes
# for consecutive values of n, starting with n = 0.

import euler

MAX = 1000
k, x, y = None, None, None
f = lambda a, b, n: n**2 + a * n + b
for a in xrange(-MAX + 1, MAX):
    for b in xrange(-MAX + 1, MAX):
        n = 0
        while True:
            if not euler.is_prime(f(a, b, n)):
                break
            n += 1
        if n > k:
            k, x, y = n, a, b
    print '%d/%d: [%d] (%d,%d)' % (MAX + a, MAX * 2, k, x, y)  # some feedback

print 'Solution: [%d]: n^2 + %dn + %d' % (k, x, y)
print 'Product:', x * y