def prime_run(a, b): for n in xrange(100): p = math.pow(n, 2) + a*n + b if p > 0 and isprime(p): continue else: return n
def main(): sum = 0 for n in xrange(2, 2000000+1): if isprime(n): sum += n print sum
def distinct_prime_factor_count(n): f = factors(n) - set([1, n]) prime_factors = set([]) for p in f: if isprime(p): prime_factors.add(p) return len(prime_factors)
def prime_factors(n): ret = [] for x in xrange(1, int(n**0.5) + 1): if (n % x == 0): if isprime(x): print ret ret.append(x) return ret
def __next__(self): while True: self.n += 1 if self.n == 1: self.prime = self.n return self.prime if self.n <= self.max: if isprime(self.n): self.prime = self.n return self.prime raise StopIteration
def truncatable_prime(n, truncate_fn): N = str(n) original_n = N while True: if isprime(int(N)): if len(N) == 1: return True else: N = str(truncate_fn(N)) else: return False
def main(n): primes = [] for x in xrange(2, 2*int(n*log(n))): if isprime(x): primes.append(x) if len(primes) == n: print primes return primes[-1]
def isCircularPrime(n): all_rotations = [] all_rotations.append(n) for i in xrange(len(str(n))-1): all_rotations.append(rotate_number(all_rotations[i])) for num in all_rotations: if not isprime(int(num)): return False return True
def isPandigital(n): number = "" for i in range(1, n+1): number += str(i) perms = permutations(number) primes = [] for perm in perms: if isprime(int(perm)): primes.append(int(perm)) return primes
#! /usr/bin/env python from common import isprime, permutations primes = [] for n in xrange(1000, 10000): if isprime(n): primes.append(n) primelist = {} for prime in primes: string_perms = set(permutations(str(prime))) perms = [] for perm in string_perms: p = int(perm) if p > prime and isprime(p): perms.append(p) if len(perms) > 1: perms.sort() for perm_prime in perms: next_prime = perm_prime + perm_prime - prime if next_prime in perms: print prime, perm_prime, next_prime, "- all terms concatenated:", str(prime) + str(perm_prime) + str(next_prime)
#! /usr/bin/env python3 from common import isprime primes = [n for n in range(1000000) if isprime(n)] longest_subset_length = 0 for p in primes[-1000:]: # subset starts at the first prime and grows min = 0 max = min + 1 current_sum = sum(primes[min:max+1]) while min < max: if current_sum < p: # grow subset with larger primes max += 1 current_sum += primes[max] elif current_sum > p: # shrink subset by removing the lowest primes min += 1 current_sum -= primes[min-1] else: # found the subset subset_len = len(primes[min:max+1]) if subset_len > longest_subset_length: longest_subset_length = subset_len print("prime =", p, "number of terms =", subset_len) break
#! /usr/bin/env python3 from common import isprime primes = [n for n in range(1000000) if isprime(n)] max_subset_length = 0 for min, p1 in enumerate(primes): for max, p2 in enumerate(primes[min:]): subset = primes[min:max] prime_sum = sum(subset) if prime_sum < 1000000 and isprime(prime_sum): if len(subset) > max_subset_length: max_subset_length = len(subset) print(prime_sum, max_subset_length, p1, p2)
#! /usr/bin/env python from common import isprime import math MAX = 10000 primes = [] for n in xrange(MAX): if isprime(n): primes.append(n) def prime_and_twice_sqaure(n): for p in primes: if n <= p: return False for x in xrange(int(math.sqrt((n-p)/2)) + 1): if n == p + 2 * x**2: #print n, "=", p, "+ 2 x", x, "^2" return True return False odd_composites = [] for n in xrange(1, MAX): i = 2*n + 1 if i not in primes: odd_composites.append(i) for c in odd_composites:
#! /usr/bin/env python from common import permutations, isprime n = '0123456789' perms = permutations(n) primes = [] for p in range(1, 18): if isprime(p): primes.append(p) def subStringDivisibility(n): for d in range(1, 8): if int(n[d:d+3]) % primes[d-1] != 0: return False return True count = 0 sum = 0 for perm in perms: if subStringDivisibility(perm): count += 1 sum += int(perm) print "the sum of all 0 to 9 pandigital numbers with this property", sum print "count", count