def r_truncatable(p): """Return True if all right-side truncations of p are prime (does not test p itself).""" if p < 10: return is_prime(p) else: r_trunc = p // 10 return is_prime(r_trunc) and r_truncatable(r_trunc)
def l_truncatable(p): """Return True if all left-side truncations of p are prime (does not test p itself).""" if p < 10: return is_prime(p) else: l_trunc = int(str(p)[1:]) return is_prime(l_trunc) and l_truncatable(l_trunc)
def is_solution(n): if not is_prime(n): for i in range(int(sqrt(n))): if is_prime(n - 2 * (i**2)): return False return True return False
def is_desired(primeset): already_test = primeset[:-1] prime = primeset[-1] for iter in already_test: if (not is_prime(int(str(iter)+str(prime)))) or (not is_prime(int(str(prime)+str(iter)))): return False return True
def populate_prime_pairs(p, q): """Return True if concatening m and n in both orders produces two primes.""" if p in prime_pairs and q in prime_pairs[p]: return True elif is_prime(int(str(p) + str(q))) and is_prime(int(str(q) + str(p))): prime_pairs[p].add(q) return True
def test_is_prime(self): prime_nums = [5, 7, 11, 13, 17, 29] not_primes = [4, 6, 8, 10, 25, 50, 75, 102] for n in prime_nums: self.assertTrue(is_prime(n), '{} is not prime'.format(n)) for n in not_primes: self.assertFalse(is_prime(n), '{} is actually prime'.format(n))
def gen_primes(num_list): value_ = ''.join(str(num) for num in num_list) last_ = ['1', '3', '7', '9'] prime = int(value_) / sum(num_list) if is_prime(prime): for last in last_: value = int(value_ + last) if is_prime(value): yield value
def factorize(number): """factorizes a number and returns the factors as a list""" factors = [] if prime.is_prime(number): return [number] while number > 1: for i in range(2, number + 1): if ((i == 2 or prime.is_prime(i)) and number % i == 0): factors.append(i) number = number // i return factors
def test_is_prime(): assert not is_prime(1) assert is_prime(2) assert is_prime(3) assert not is_prime(4) assert is_prime(5) assert not is_prime(6) assert is_prime(7) assert not is_prime(8) assert not is_prime(9) assert not is_prime(10)
def is_truncatable(num): if not is_prime(num): return False prime = str(num) for n in xrange(1, len(prime)): if not is_prime(int(prime[:n])): return False if not is_prime(int(prime[n:])): return False return True
def factorization(num): import prime i = 3 while num >= i ** 2: if num % i == 0: num1 = i - 1 num2 = (num // i) - 1 if prime.is_prime(num1) and prime.is_prime(num2): print(num1) print(num2) return i += 1 print('None')
def is_truncatable(num): n = num while n: if is_prime(n): n //= 10 else: return False n = remove_first(num) while n: if is_prime(n): n = remove_first(n) else: return False return True
def gen_odd_composites(): step_size = 10000 sieve_primes(step_size) # Cache primes # Avoiding lower limit. This avoids running an unnecessary filter for each caching step for j in range(9, step_size + 1, 2): if not is_prime(j): yield j for i in count(2): sieve_primes(i * step_size + 1) # Cache primes for j in range(step_size * (i - 1) + 1, step_size * i + 1, 2): if not is_prime(j): yield j
def percent(n): if n == 1: return 0 if n % 2: while len(d2) < (n - 1) / 2: j = 2 * len(d2) + 3 d2.append(is_prime(j * j - j + 1)) d3.append(is_prime(j * j - 2 * j + 2)) d4.append(is_prime(j * j - 3 * j + 3)) return (sum(d2[0:int((n - 1) / 2)]) + sum(d3[0:int((n - 1) / 2)]) + sum(d4[0:int((n - 1) / 2)])) / (2 * n - 1) else: raise Exception("Solo puedo recibir argumentos impares")
def test_main(self): print("Fibonacci") for i in range(10): print("{}: {}".format(i, fib(i))) print("Geometric numbers (square, triangle, cube)") for i in range(10): print("{}: {} {} {}".format(i, square(i), triangle(i), cube(i))) print("Primes") prime_list = primes(1000) for p in prime_list[-10:]: print(p) print("Is 999981 prime? {}".format(is_prime(999981))) print("Is 999983 prime? {}".format(is_prime(999983)))
def find_largest_prime_factor_by_bruteforce(): largest_prime_factor = 0 for number in range(2, int(math.sqrt(VICTIM_NUMBER)) + 1): if not VICTIM_NUMBER % number and is_prime(number): if largest_prime_factor < number: largest_prime_factor = number return largest_prime_factor
def main(le): global primes, record primes = [{} for i in range(le + 1)] circulars = [[] for i in range(le + 1)] circulars[1] = [2, 3, 5, 7] res = len(circulars[1]) for size in range(2, le + 1): record = [None] * size dfs(0, size) for string in primes[size]: if primes[size][string]: num = 1 primes[size][string] = False for i in range(size - 1): string = string[-1:] + string[:-1] if string in primes[size] and primes[size][string]: num += 1 primes[size][string] = False if num == size: circulars[size].append(int(string)) res += size * len(circulars[size]) if prime.is_prime(int('1' * size)): res += 1 return res
def sum_to_p(p, limit=limit): """ Return a 2-tuple containing the largest possible prime that is a sum of consecutive primes ending in p, and the number of terms in the sum. """ res = (p, 1) total = p # This generator will ensure that we are always list primes smallest to largest, starting 1 below p for i, q in enumerate(filter(lambda x: x < p, primes(reverse=True, step=p)), start=2): total += q if total > limit: return res elif is_prime(total): res = (total, i) if q == 2: return res return res
def from_right(num): string = str(num) length = len(string) for beg in range(length): if not is_prime(int(string[beg:])): return False return True
def prime_factors_sum(number): arr = [] for i in range(2, int(number ** 0.5)): if number % i == 0 and is_prime(i): arr.append(i) print arr return sum(arr)
def get(a, b): n = 2 while True: x = n ** 2 + n * a + b if not prime.is_prime(x): return n n += 1
def largest_prime_helper(n, start_prime): if is_prime(n): return n for x in xrange(start_prime + 1, int(sqrt(n)) + 1): if n % x == 0: return largest_prime_helper(remove_factor(n, x)[0], x) return start_prime
def prime_number_of_divisors(n): count = 0 for i in range(1, n + 1): if n % i == 0: count += 1 return is_prime(count)
def weak_low_p_1(l): small_p = [[3], [5, 7], [11, 13], [17, 19, 23, 29, 31], [37, 41, 43, 47, 53, 59, 61], [67, 71, 73, 79, 83, 89, 97, 101]] p = 2 size = [2, 3, 4, 5, 6, 7] cur_size = 2 while True: if l - cur_size < 7: size = [elem for elem in size if elem <= l - cur_size] if len(size) == 0: p = 2 cur_size = 2 size = [2, 3, 4, 5, 6, 7] continue if len(size) == 1: next_size = 0 else: next_size = random.randint(0, len(size) - 1) p = p * random.choice(small_p[next_size]) cur_size = int(math.log2(p + 1) + 1) if int(math.log2(p + 1) + 1) == l: if is_prime(p + 1, l): return p p = 2 cur_size = 2 size = [2, 3, 4, 5, 6, 7]
def prime_family(x, num): MAX = 10 - num L = [[] for i in range(MAX + 1)] array = list(str(x)) for i in range(len(array)): k = int(array[i]) if k <= MAX: L[k].append(i) for i in range(MAX + 1): size = len(L[i]) if size == 0: continue key_max = 2**size for key in range(1, key_max): key_array = [] key_count = 0 while key: if key % 2: key_array.append(key_count) key //= 2 key_count += 1 count = 10 - i for j in range(i + 1, 10): str_j = str(j) for k in key_array: array[L[i][k]] = str_j if not is_prime(int(''.join(array))): count -= 1 if count < num: break else: return True return False
def minimum_prime_number(X: int): answer = 0 for i in range(X, (10 ** 14 + 32)): if is_prime(i): answer = i break print(answer)
def prime_family(x, num): MAX = 10 - num L = [[] for i in range(MAX + 1)] array = list(str(x)) for i in range(len(array)): k = int(array[i]) if k <= MAX: L[k].append(i) for i in range(MAX + 1): size = len(L[i]) if size == 0: continue key_max = 2 ** size for key in range(1, key_max): key_array = [] key_count = 0 while key: if key % 2: key_array.append(key_count) key //= 2 key_count += 1 count = 10 - i for j in range(i + 1, 10): str_j = str(j) for k in key_array: array[L[i][k]] = str_j if not is_prime(int(''.join(array))): count -= 1 if count < num: break else: return True return False
def sum_primes(n): """Sums the primes below n""" sumprimes = 0 for x in xrange(n): if prime.is_prime(x): sumprimes += x print sumprimes
def test_is_four_prime(self): ''' :return: ''' self.assertFalse(is_prime(4))
def test_is_zero_prime(self): ''' :return: ''' self.assertFalse(is_prime(0))
def sum_of_primes(n): """Returns the sum of all prime numbers between 1 and n (not inclusive).""" sum = 0 for num in range(1, n): if prime.is_prime(num) == True: sum += num return sum
def test_is_five_prime(self): ''' :return: ''' self.assertTrue(is_prime(5))
def list_of_primes(ceiling): return_list = [] for p in range(2,ceiling): if is_prime(p): return_list.append(p) return return_list
def test_is_prime(n): for x in xrange(n + 1): if prime.is_prime(x): is_prime.append(x) for x in xrange(n + 1): if prime.is_prime_with_half(x): is_prime_with_half.append(x)
def yes(num): for i in range(1, num + 1): x = 2 * i**2 if x >= num: break if is_prime(num - x): return True return False
def _find_prime(nbits, pipe): while True: integer = randnum.read_random_odd_int(nbits) # Test for primeness if prime.is_prime(integer): pipe.send(integer) return
def best_family(p): best = 0 for fam in generate_families(p): prime_count = len(list(filter(None, (is_prime(n) for n in fam)))) if prime_count > best: best = prime_count return best
def find_solution(): res = 0 for n in range(1, 10): for i in permutations(range(1, n+1)): number = int(''.join([str(a) for a in i])) if number > res and is_prime(number): res = number return res
def yes(num): for i in range(1, num + 1): x = 2 * i ** 2 if x >= num: break if is_prime(num - x): return True return False
def main(): '''[sum(range(1, x + 1)) % 3 for x in range(1, 10)] => [1, 0, 0, 1, 0, 0, 1, 0, 0]''' MAX = 0 x = ''.join(map(str,range(1, 5))) gener = all_permutations(x) for num in gener: if num[-1] in ('1', '3', '7', '9'): num = int(''.join(num)) if is_prime(num): MAX = max(MAX, num) x = ''.join(map(str,range(1, 8))) gener = all_permutations(x) for num in gener: if num[-1] in ('1', '3', '7', '9'): num = int(''.join(num)) if is_prime(num): MAX = max(MAX, num) return MAX
def zz(maxlen): for currlen in range(maxlen,1,-1): #l = [p[i:j] for i in xrange(0,nbprimes-mylen) for j in xrange(mylen] l = (primes[i-currlen:i] for i in range(currlen,nbprimes)) #print( l ) for sub in l: mylist,mysum,mylen = sub,sum(sub),len(sub) if(prime.is_prime(mysum) and mysum<1000000): return(mylist,mysum,mylen)
def is_circular(num): y = str(num) for z in range(0, len(y)): n = int(y) if not is_prime(n): return False y = y[-1] + y[0:-1] return True
def main(): upperbound = 1000000 for prime in gen_prime(upperbound): for number_family in number_families(prime): prime_family = [n for n in number_family if is_prime(n) and len(str(n))==len(str(prime))] if len(prime_family)==8 and prime in prime_family: print '\n',prime return sys.stdout.write("Testing number %d\r"%(prime)) sys.stdout.flush()
def deglob(x): # combinations = list() count = 0 for i in range(x.index('*') == 0, 10): val = int(x.replace('*', str(i))) if prime.is_prime(val): # combinations.append(val) count += 1 # return combinations return count
def dfs(pos, size): if pos == size: string = ''.join(record) if prime.is_prime(int(string)): primes[size][string] = True return digits = '1379' for i in digits: record[pos] = i dfs(pos + 1, size)
def main(): """ cn = p + 2 * s ^ 2 is transformed to s = sqrt((cn - p) / 2) and if s is an integer and p a prime, then cn is buildable by the formula, otherwise not. >>> main() 5777 """ composite_number = (n for n in count(9) if is_odd(n) and not is_prime(n)) counter_example = (cn for cn in composite_number if not is_goldbach_number(cn)) print((next(counter_example)))
def prime_factors(n): res = [] global prime_numbers if is_prime(n): return [n] for i in prime_numbers: if n == 1: break if n % i == 0: res.append(i) n /= i return res
def col_pairs_is_prime(col): for x in range(0, len(col)): i = x for a in range(1, len(col)): i += 1 if i >= len(col): i -= len(col) combined = str(col[x]) + str(col[i]) is_prime = prime.is_prime(int(combined)) if not is_prime: return False return True
def is_composite(n): global primes global ordered_primes global max_prime if n <= max_prime: return n not in primes elif prime.is_prime(n): primes.add(n) ordered_primes.append(n) max_prime = n return False else: return True
def is_circular_prime(n): """Returns true if n and all rotations of n are also prime Note that a number like 333 does not have 3 rotations! """ # pdb.set_trace() s = str(n) for i in xrange(len(s)): if not is_prime(n): return False s = s[1:] + s[0] n = int(s) return True
def find_solution(): result = [] for c in combinations_with_replacement(range(1, 10), 4): tmp_res = [] for p in permutations(c): number = int(''.join([str(e) for e in p])) if is_prime(number): tmp_res.append(number) if len(tmp_res) >= 3: for p in permutations(tmp_res, 3): number = int(''.join([str(e) for e in p])) if p[2] > p[1] > p[0] and p[2] - p[1] == p[1] - p[0] \ and number not in result and number != 148748178147: result.append(number) return result[0]
def pandigitalPrimes(string, s): if len(s) == 0: number = int(string) if is_prime(number): return [number] else: return [] else: result_list = [] for e in s: test_string = str(string) test_string += e test_set = set(s) test_set.remove(e) result_list += pandigitalPrimes(test_string, test_set) return result_list
def main(): L = list(filter(lambda x: is_prime(x), range(1000, 10000))) S = set(L) size = len(L) for i in range(size - 2): a = L[i] for j in range(i + 1, size - 1): b = L[j] if a == 1487 and b == 4817: continue c = 2 * b - a if c > L[-1]: break if c in S and sorted_digit(a) == sorted_digit(b) == sorted_digit(c): return str(a) + str(b) + str(c)
def main(): layer = 2 benchmark = 0.1 diagonals = 1 nprime = 0 while True: corner = corners(layer) for i in corner: if is_prime(i): nprime += 1 diagonals += 4 if float(nprime)/diagonals < benchmark: break layer += 1 print layer*2-1