Exemple #1
0
 def split_on_primes(before: tuple, n: int, after: tuple):
     if n < 10: return
     idx = 0
     left = 0
     lst = list(map(int, list(str(n))))
     while idx < len(lst):
         left *= 10
         left += lst[idx]
         right = join(lst[idx + 1:])
         if is_prime(left):
             tup = []
             if left > 0:
                 tup.append(left)
             if right > 0:
                 tup.append(right)
             splits.add(before + tuple(tup) + after)
             if left and right:
                 if left > 0:
                     split_on_primes(before, left, (right, ) + after)
                 if right > 0:
                     split_on_primes(before + (left, ), right, after)
         idx += 1
     res = []
     for s in splits:
         if is_prime(s[-1]):
             res.append(s)
     return res
Exemple #2
0
def primes(limit):
    '''Generates prime numbers up to limit.  Pass 'None' as argument to infinite loop'''
    if not limit: print('WARNING: Running in infinite loop mode')
    yield 2
    i = 3
    while not limit or i < limit:
        if is_prime(i):
            yield i
        i += 2
Exemple #3
0
def possible_primes(num_digits: int):
    primes = set()
    digits = [n for n in range(1, 10)]
    for c in combinations(digits, num_digits):
        if num_digits == 8 and 2 in c and 3 in c and 5 in c and 7 in c:
            continue
        for p in permutations(c):
            number = join(p)
            if is_prime(number):
                primes.add(number)
    return primes
Exemple #4
0
def possibilities(n, d):
    repeat = True
    for i in range(10):
        if not repeat: break
        t = [True] * (n-i) + [False] * i
        print('__________')
        for p in set(permutations(t)):
            for x in fill(p, d):
                if is_prime(x) and x > 10**(n-1):
                    repeat = False
                    print(x)
                    yield(x)
Exemple #5
0
def run(n):
    odds = 0
    even_primes = set()
    for i in range(2, n):
        if is_prime(i):
            period = frac_decompose(i)
            if period % 2:
                odds += 1
            else:
                even_primes.add(i)
        else:
            if is_possibly_odd_period(i, even_primes):
                x = frac_decompose(i)
                if x % 2:
                    odds += 1
    return odds
Exemple #6
0
def primetest(n):
    if n not in prime_cache:
        prime_cache[n] = is_prime(n)
    return prime_cache[n]
Exemple #7
0
def circular_prime(n):
    for p in rotate(n):
        if not is_prime(p):
            return False
    return True
Exemple #8
0
def prime_diffs(n):
    neighbors = get_neighbors(n)
    count = 0
    for neighbor in neighbors:
        if is_prime(abs(n - neighbor)): count += 1
    return count
Exemple #9
0
 def primetest(n):
     if n in primes:
         return primes[n]
     else:
         primes[n] = is_prime(n)
         return primes[n]