def getprimes(n=None, below=None): primes = [2] num = 3 if below is None: while len(primes) < n: if isprime(num): primes.append(num) num += 2 elif n is None: while num < below: if isprime(num): primes.append(num) num += 2 return primes
def largestprimefactor(n): if type(n) is not int and type(n) is not long: raise TypeError allfactors = factors(n) # Now we have a list of factor pairs (non-prime) # Let's find which of these are prime numbers pfactors = [] for factorpair in allfactors: if isprime(factorpair[0]): pfactors.append(factorpair[0]) if isprime(factorpair[1]): pfactors.append(factorpair[1]) # print(pfactors) return max(pfactors)
def digitreplace(n, checkprime=False): # This function will return all possible digit replacements # excluding replacing all digits # Note that a preceeding zero would cause the number to be omitted number = list(str(n)) length = len(number) indices = list(range(length)) replacement_digits = "0123456789" new_numbers = [] for num_digits_to_replace in range(1, length): # 1 to length-1 digits for replacement_indices in combinations(indices, num_digits_to_replace): this_pattern = [] for digit in replacement_digits: new_number = deepcopy(number) for replacement_index in replacement_indices: new_number[replacement_index] = digit # Now we have a new number # We want to omit numbers which start with a zero if new_number[0] != "0": # Convert to an int new_number = int("".join(new_number)) else: continue if checkprime is False or isprime(new_number): this_pattern.append(new_number) if len(this_pattern) > 0: new_numbers.append(this_pattern) return new_numbers
def iscircularprime(n): s = str(n) for i in range(len(s)): if isprime(int(s)) is False: return False s = rotate(s) return True
def primeFactors(n, conv=False): prime_factors = {} for pair in factors(n): for num in pair: if isprime(num): prime_factors[num] = 1 prod = 1 for prime_factor in prime_factors: prod = prod * prime_factor while prod != n: quotient = n / prod # i.e. what we still need to represent with the prime factors for prime_factor in prime_factors: if quotient % prime_factor == 0: prod = prod * prime_factor prime_factors[prime_factor] += 1 if conv is False: return prime_factors else: return primefactorconverter(prime_factors)
def findValidSets(input_set, member_length): # Takes in an iterable of iterables (preferably set of frozensets) # Gives back only those which have the prime pair set property num_permutations = len( list(permutations([0]*member_length, 2))) # A bit gross print("Searching through " + str(len(input_set)) + " possible combinations...") valid_sets = set() for primes in input_set: num_prime_pairs = 0 # This will give us the pairs both ways round for x in permutations(primes, 2): if isprime(concatenate(x)): num_prime_pairs += 1 else: break if num_prime_pairs == num_permutations: # All the pairs we checked were prime valid_sets.add(frozenset(primes)) return valid_sets
#!/usr/bin/python3 # Getting all primes with < 10 digits takes way too much time # Get all pandigital numbers < 10 digits then check for primeness # Certainly quicker to generate pandigital numbers than to iterate and find them from itertools import permutations from lib import isprime digits = "123456789" if __name__ == "__main__": primepandigitals = [] for n in range(2, 10): for pandigital in permutations(digits[:n], n): num = "" for digit in pandigital: num += digit if isprime(int(num)): primepandigitals.append(int(num)) print("Found " + str(len(primepandigitals)) + " pandigital prime numbers") print(max(primepandigitals))
def truncateleft(n): s = str(n) return int(s[1:]) def truncateright(n): s = str(n) return int(s[:-1]) if __name__ == "__main__": truncatable_primes = [] n = 11 # Single digit primes don't count while len(truncatable_primes) < 11: if isprime(n): left = n right = n for i in range(len(str(n)) - 1): left = truncateleft(left) right = truncateright(right) if isprime(left) and isprime(right): if len(str(left)) == 1: # That's the last time we will be truncating truncatable_primes.append(n) print(n) else: break n += 2 print(sum(truncatable_primes))
#!/usr/bin/python3 from lib import isprime # Let's just run through all the odd natural numbers starting at 3 primes = [2] testnum = 3 while len(primes) != 10001: if isprime(testnum): primes.append(testnum) testnum += 2 print(primes[-1])
primes.append(num) num += 2 elif n is None: while num < below: if isprime(num): primes.append(num) num += 2 return primes if __name__ == "__main__": # First get a list of all prime numbers below 1,000,000 primes = getprimes(below=1000000) print("Got " + str(len(primes)) + " primes") # We will store sums like (sum, num_terms) best = (0, 0) for i, startpoint in enumerate(primes[:-1]): best_for_this_start = (0, 0) length = 1 summation = startpoint for prime in primes[i+1:]: summation += prime length += 1 if summation > 1000000: break if isprime(summation): best_for_this_start = (summation, length) if best_for_this_start[1] > best[1]: best = deepcopy(best_for_this_start) print(best)
#!/usr/bin/python3 from lib import isprime # let's see if we can just check all odd numbers below 2,000,000 s = 2 # Sum starts at 2 because we're missing 2 out of the range for n in range(3, 2000000, 2): if isprime(n): s += n print(s) # Takes about 30sec or so. Fit for purpose.
#!/usr/bin/python3 from lib import isprime if __name__ == "__main__": spiral_side_length = 1 num_diagonal_primes = 0 num_diagonals = 1 prime_ratio = 1.0 # So as not to immidiately exit the loop current_num = 1 while prime_ratio > 0.1: num_diagonals += 4 spiral_side_length += 2 increment_this_spiral = spiral_side_length - 1 # Now look at all four corners for i in range(4): current_num += increment_this_spiral if isprime(current_num): num_diagonal_primes += 1 # Re-calculate the prime ratio prime_ratio = float(num_diagonal_primes) / float(num_diagonals) print(spiral_side_length)
#!/usr/bin/python3 # for n^2 + an + b to give consecutive primes starting at n = 0, b must always be prime # So we need a list of prime numbers below or up to 1,000. # Prime numbers are all positive so we can take that into account from lib import isprime primes = [] for i in range(2, 1001): if isprime(i): primes.append(i) longest_sequence = (0, 0, 0) # (a, b, n) for a in range(-999, 999): for b in primes: n = 0 ans = (n * n) + (a * n) + b while ans > 0 and isprime(ans): n += 1 ans = (n * n) + (a * n) + b if n > longest_sequence[2]: longest_sequence = (a, b, n) print(longest_sequence) a, b, n = longest_sequence print(a * b)
def rotate(s): rotated = "" for i in range(len(s)): rotated += s[i - 1] return rotated def iscircularprime(n): s = str(n) for i in range(len(s)): if isprime(int(s)) is False: return False s = rotate(s) return True if __name__ == "__main__": primes = [2] for i in range(3, 1000000, 2): if isprime(i) is True: primes.append(i) print("found " + str(len(primes)) + " primes") circular_primes = [] for prime in primes: if iscircularprime(prime) is True: circular_primes.append(prime) # print(circular_primes) print(len(circular_primes))
import sys import lib primes_encountered = set() i = 1 while True: i += 1 isprime = lib.isprime(i) if isprime: primes_encountered.add(i) if not isprime and i % 2 == 1: found_hit = False for pr in primes_encountered: diff = i - pr if diff % 2 == 0: possible_sq = diff / 2 if possible_sq in list(x**2 for x in xrange(1, possible_sq+1)): found_hit = True break if not found_hit: print i break
# Start assuming the first number is in the equispaced sequence # We need to find at least three numbers to make it a sequence # If no seqeunce is found, then assume the second number is the # start of an equispaced sequence if len(nums) < 3: return [] nums.sort() for i, start in enumerate(nums[:-2]): gaps = [] for potential_sequence_member in nums[i + 1:]: gaps.append(potential_sequence_member - start) for gap in gaps: if gap * 2 in gaps: # We have found a sequence with gap return [start, start + gap, start + (2 * gap)] return [] if __name__ == "__main__": for i in range(1000, 10000): primeperms = [] for perm in permutations(str(i)): if perm[0] == "0": continue # We need 4 digit numbers n = int(perm[0] + perm[1] + perm[2] + perm[3]) if n not in primeperms and isprime(n): primeperms.append(n) seqeunce = findequispaced(primeperms) if len(seqeunce) > 0: print(seqeunce)