def is_truncatable_prime(n): if n in ["2", "3", "5", "7"]: return False l = len(n) for i in range(0, l): m = int(n[i:]) if not lib.is_prime(m): return False m = int(n[0:l-i]) if not lib.is_prime(m): return False return True
def ok(a, b): x = lib.concat(a, b) if x < N: if not sieve[x]: return False elif not lib.is_prime(x): return False y = lib.concat(b, a) if y < N: if not sieve[y]: return False elif not lib.is_prime(y): return False return True
def count_quandratic_primes(a, b): count = 0 n = 0 while n == count: count += int(is_prime(n ** 2 + a * n + b)) n += 1 return count
def get_prime_n(n): count = 0 num = 1 while count != n: num += 1 if (is_prime(num)): count += 1 return num
def euler_27(max_a, max_b): a = range(-max_a, max_a + 1) #n = 0 means that the equation simplifies to b, so b must be prime. b = filter(lambda x: lib.is_prime(abs(x)), range(-max_b, max_b + 1)) vars = filter(a_filter, itertools.product(a, b)) solution = max((consec_primes_for_eq(*var), var) for var in vars)[1] return operator.mul(*solution)
def euler_46(): r = itertools.count(3, 2) composite_odd = filter(lambda x: not lib.is_prime(x), r) for n in composite_odd: squares = itertools.takewhile(lambda x: x < n, (2 * (i ** 2) for i in itertools.count())) if not any(map(lib.is_prime, (n - s for s in squares))): return n
def prime_factorization(n): primes = [] candidates = xrange(2, n + 1) candidate = 2 while not primes and candidate in candidates: if n % candidate == 0 and lib.is_prime(candidate): primes = primes + [candidate] + prime_factorization(n / candidate) candidate += 1 return primes
def prime_factorization(n): primes = [] candidates = xrange(2, n+1) candidate = 2 while not primes and candidate in candidates: if n%candidate == 0 and lib.is_prime(candidate): primes = primes + [candidate] + prime_factorization(n/candidate) candidate += 1 return primes
def main(): pandigital_length = 9 while pandigital_length > 1: pandigitals = generate_pandigitals(pandigital_length) pandigitals.sort(reverse=True) for pandigital in pandigitals: if lib.is_prime(pandigital): print(pandigital) pandigital_length = 0 break pandigital_length -= 1 return 0
def is_prime_cached(n, primes): """ Checks if a number is prime. Caches primes found in primes :param n: the number to check :param primes: list of known primes :return: True if prime, False otherwise """ if n in primes: return True if lib.is_prime(n): primes.add(n) return True return False
def main(): circular_primes = [2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97] for i in range(101, 1000000, 2): n = i digit_count = 0 while n > 0: if n % 5 == 0 or n % 2 == 0: digit_count = -1 break n //= 10 digit_count += 1 if digit_count == -1 or i in circular_primes or not is_prime(i): continue rotated_primes = [i] n = (i % 10 * 10 ** (digit_count - 1)) + (i // 10) while n != i: if not is_prime(n): break rotated_primes.append(n) n = (n % 10 * 10 ** (digit_count - 1)) + (n // 10) else: circular_primes += rotated_primes print(len(circular_primes))
fig, axs = plt.subplots(2, 2, figsize=(20.0/2.54, 20.0/2.54)) ax1, ax2, ax3, ax4 = axs.flat print('Mean Error Real FFT (Non-Primes)') i = 0 markers = ['>', '<', '+', 'x', '^'] styles = ['--', ':', '-.', '--', ':'] for k in sorted(results.keys()): v = results[k] kwargs = dict(label=k, marker=markers[i], ls=styles[i]) n = np.array(v['sizes'][:], dtype=np.int) # the values which are a power of two f = (n != 0) & ((n & (n - 1)) == 0) # the primes fp = lib.is_prime(n) # the non-power of two, non-primes -> regular (or Hamming) numbers f2 = ~(f | fp) lc1, = ax1.plot(n[f], v['real_l2'][f], **kwargs) lc2, = ax2.plot(n[f2], v['real_l2'][f2], **kwargs) lc3, = ax3.plot(n[fp], v['real_linf'][fp], **kwargs) lc4, = ax4.plot(v['identity_sizes'], v['real_identity'], **kwargs) i += 1 # ignore prime sizes as the error grows strongly with N print(k.ljust(20), np.mean(v['real_l2'][~fp])) ax1.set_title('Power of Two') ax2.set_title('Regular Numbers') ax3.set_title('Primes') ax4.set_title('Identity Error')
def test2(self): self.assertTrue(lib.is_prime(3))
def test11(self): self.assertFalse(lib.is_prime(6))
def test13(self): self.assertFalse(lib.is_prime(9))
def test7(self): self.assertTrue(lib.is_prime(17))
def test9(self): self.assertTrue(lib.is_prime(23))
for consecutive values of n, starting with n = 0 """ from lib import is_prime def count_quandratic_primes(a, b): count = 0 n = 0 while n == count: count += int(is_prime(n ** 2 + a * n + b)) n += 1 return count max_n = 0 max_i = 0 max_j = 0 for i in range(-999, 1000): if not is_prime(abs(i)): continue for j in reversed(range(-999, 1000)): if not is_prime(abs(j)): continue n_count = count_quandratic_primes(i, j) if max_n < n_count: max_n = n_count max_i = i max_j = j print(max_i * max_j)
def test8(self): self.assertTrue(lib.is_prime(19))
def test15(self): self.assertFalse(lib.is_prime(100))
def test12(self): self.assertFalse(lib.is_prime(8))
import lib lib._prime_limit = 10000 for x in range(1001, 10000, 2): if not lib.is_prime(x): continue inc_limit = ((10000 - x) / 2) for inc in range(10, inc_limit, 2): seq = x + inc, x + inc + inc if lib.is_prime(seq[0]) and lib.is_prime(seq[1]): s1 = sorted(str(x)) if s1 == sorted(str(seq[0])) and s1 == sorted(str(seq[1])): print "%d%d%d" % (x, x+inc, x+inc+inc)
""" The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. """ from lib import is_prime limit = 2000000 primesum = 0 for i in range(1, limit+1): if(is_prime(i)): primesum += i print(primesum)
import lib sum = 2 below = 2000000 for i in range(3, below, 2): if lib.is_prime(i): sum += i print(sum)
def test6(self): self.assertTrue(lib.is_prime(13))
def test5(self): self.assertTrue(lib.is_prime(11))
def test4(self): self.assertTrue(lib.is_prime(7))
def test10(self): self.assertFalse(lib.is_prime(4))
import lib lim = 1000000 primes = list(lib.get_primes(lim)) max_prime = (0, 0, 0) for idx, start in enumerate(primes): total = start for num, p in enumerate(primes[idx+1:]): if total >= lim: break if lib.is_prime(total): max_prime = max(max_prime, (num + 1, total, start)) total += p print max_prime
def consecutive_prime(a, b): n, length = 0, 0 while (n * n + a * n + b) > 1 and is_prime(n * n + a * n + b): n += 1 length += 1 return length
def test3(self): self.assertTrue(lib.is_prime(5))
def num_euler(a, b): for n in itertools.count(): if not lib.is_prime(n*n + a*n + b): return n
def test1(self): self.assertTrue(lib.is_prime(2))
def circular_filter(n): return all((lib.is_prime(x) for x in rotate_gen(n)))
""" Largest prime factor Problem 3 The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? """ from math import sqrt, ceil from lib import is_prime, iterate_odd number = 600851475143 limit = ceil(sqrt(number)) pf = [] for n in range(3, limit): if number % n == 0 and is_prime(n): pf.append(n) print(max(pf))
""" Summation of primes Problem 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. """ from lib import is_prime p = [2] p_sum = 2 max_n = 2000000 for n in range(3, max_n, 2): if is_prime(n): p.append(n) p_sum += n print(p_sum)