def compositenum(number): if is_prime(number): return False primelist = [i for i in range(start-1,1,-1) if is_prime(i)] for i in primelist: for j in range(1,int(number**0.5)+1): if i + 2*j**2 == number: return False return True
def qf(a, b): ip = True n = -1 while ip: n = n + 1 ip = is_prime(n**2 + a*n + b) return n
def circular_right_prime(num): res = [x for x in str(num)] for i in range(len(res)): if is_prime(convert(res)) is True: a = res.pop(0) res.append(a) else: return False return True
from tools import is_prime_old fl = [1,2,3,4,5,6,7]#,8]#,9] l = fl[len(fl)-1] pands = [] def permut(list, string): val = 0 if string != "": val = int(string) pands.append(val) for i in range(0, len(list)): tmp = list[i] del list[i] string = string + str(tmp) permut(list, string) list.insert(i, tmp) string = string[:-1] permut(fl, "") pands.sort() pands.reverse() for pan in pands: if is_prime(pan): print pan break
def is_composite_odd(x): if (is_prime(x) is False) and (x % 2 != 0): return True else: return False
def is_composite_odd(x): if (is_prime(x) is False) and (x % 2 != 0): return True else: return False prime_list = [] square_list = [2] for x in range(2, large_num): square_list.append(2*x*x) if is_prime(x) is True: prime_list.append(x) if is_composite_odd(x) is True: found: bool = True # check if it's a prime + twice the square of any number for prime in prime_list: if prime > x: break for square in square_list: if (square + prime == x): found = False break
# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. # What is the 10 001st prime number? from tools import is_prime primes = [] n = 3 primes.append(2) while len(primes) <= 10000: if is_prime(n): primes.append(n) n += 2 print primes[10000]
def check_is_perm(number): return is_prime(number+3330) and sorted([int(i) for i in str(number)])==sorted([int(i) for i in str(number+3330)])
known_primes = [-1]*max sum = 0 for i in range(min,max): s = str(i) t = [] prime = True for j in range(0,len(s)): temp = s[j:].lstrip('0') if temp != '': t.append(temp) for j in range(1,len(s)+1): temp = s[:j] if temp != '': t.append(temp) for sp in t: i = int(sp) if known_primes[i] == 0: prime = False break if known_primes[i] == -1: if is_prime(i): known_primes[i] = 1 else: known_primes[i] = 0 prime = False break if prime: sum += i print sum
t3=6 c=1 t4=8 d=1 nr = 0 nr_of_primes = 0 while True: a += t1 t1 += 8 b += t2 t2 += 8 c += t3 t3 += 8 d += t4 t4 += 8 nr += 4 if is_prime(a): nr_of_primes += 1 if is_prime(b): nr_of_primes += 1 if is_prime(c): nr_of_primes += 1 #if is_prime(d): # nr_of_primes += 1 if nr_of_primes*10 < nr: break print b-a-1
import itertools import tools four_digit_primes = [] for n in range(1001, 9999, 2): if tools.is_prime(n): four_digit_primes.append(n) print(four_digit_primes) for prime_1 in four_digit_primes: permutations_str = list(itertools.permutations([e for e in str(prime_1)])) permutations = [] for permutation in permutations_str: permutations.append(int(''.join(e for e in permutation))) for prime_2 in list((set(four_digit_primes) & set(permutations)) - set([prime_1])): for prime_3 in list((set(four_digit_primes) & set(permutations)) - set([prime_1, prime_2])): if abs(prime_1 - prime_2) == abs(prime_2 - prime_3) or abs( prime_1 - prime_2) == abs(prime_1 - prime_3) or abs( prime_1 - prime_3) == abs(prime_2 - 3): print(prime_1, prime_2, prime_3)
from tools import is_prime best = 0 for a in range(-1000, 1000): for b in range(-1000, 1000): running = True n = 0 while running: num = n * n + a * n + b if is_prime(num): n += 1 else: if n >= best: print n, a, b, a*b best = n running = False
# The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. # Find the sum of all the primes below two million. from tools import is_prime primes = [2] for i in range(3, 2 * 10 ** 6, 2): if is_prime(i): primes.append(i) print sum(primes)
import tools def f(n, a, b): return n**2 + a * n + b primes_under_1000 = tools.sieve_of_eratosthenes(int(1e3)) consecutive_primes = [] a_max = 0 b_max = 0 for b in primes_under_1000: for a in range(-b - 1, 1000): if tools.is_prime(1 + a + b): temp = [] n = 0 while tools.is_prime(f(n, a, b)): temp.append(f(n, a, b)) n += 1 if len(temp) > len(consecutive_primes): consecutive_primes = temp a_max = a b_max = b print(a_max, b_max) print(a_max * b_max) print(consecutive_primes) print(len(consecutive_primes))
import tools upper_limit = 1000000 primes = tools.sieve_of_eratosthenes(upper_limit) res = 0 count = 0 for i, prime_1 in enumerate(primes): temp = [prime_1] for prime_2 in primes[i + 1:]: if temp[-1] + prime_2 < upper_limit: temp.append(temp[-1] + prime_2) else: break for j, p in enumerate(reversed(temp)): if tools.is_prime(p): temp = temp[:len(temp) - j] break if len(temp) > count: res = temp[-1] count = len(temp) print(res) print(count)
import math import tools found = False n = 35 primes_smaller_than_n = [i for i in range(2, n + 1) if tools.is_prime(i)] print(primes_smaller_than_n) while not found: if tools.is_prime(n): primes_smaller_than_n.append(n) n += 2 continue else: for prime in primes_smaller_than_n: temp = (n - prime) / 2 temp_list = [ i for i in range(1, int(math.sqrt(n) + 1)) if i**2 == temp ] if len(temp_list) > 0: print( str(n) + ' = ' + str(prime) + ' + 2x' + str(temp_list[0]) + '^2') n += 2 break elif prime == primes_smaller_than_n[-1] and len(temp_list) == 0: found = True print(n)
Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed. 37 36 35 34 33 32 31 38 17 16 15 14 13 30 39 18 5 4 3 12 29 40 19 6 1 2 11 28 41 20 7 8 9 10 27 42 21 22 23 24 25 26 43 44 45 46 47 48 49 It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13 62%. If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%? """ from tools import is_prime start = 1 diag = [1] step = 2 prime = [] while True: for i in range(4): start +=step diag.append(start) if is_prime(start): prime.append(start) step +=2 if len(prime) / float(len(diag)) < 0.1 : print step -1 break
from tools import is_prime x,n = 1,0 while n < 10001: x = x + 1 if is_prime(x): n = n + 1 print x
import math from tools import is_prime x = 600851475143 y = int(math.sqrt(x))+1 while y > 1: if x % y == 0 and is_prime(y): print y break y = y - 1
#!/usr/bin/python #-*- coding: utf-8 -*- """ Prime permutations Problem 49 The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence. What 12-digit number do you form by concatenating the three terms in this sequence?""" from tools import is_prime import itertools primelist = (i for i in range(1000,10000) if is_prime(i)) def check_is_perm(number): return is_prime(number+3330) and sorted([int(i) for i in str(number)])==sorted([int(i) for i in str(number+3330)]) permprimelist = [] for i in primelist: if check_is_perm(i): if check_is_perm(i+3330): permprimelist.append([i,i+3330,i+6660]) solution = '' for i in permprimelist[1]: solution +=str(i) print solution