def is_prime_pair(prime_number1, prime_number2): assert prime_number1 < prime_number2 if prime_number1 > 3 and prime_number2 - prime_number1 == 2: # concatenating twin primes always results in a number divisible by 3 return False digits1 = str(prime_number1) digits2 = str(prime_number2) return is_prime(int(digits1 + digits2)) and is_prime( int(digits2 + digits1))
def _ratios() -> Iterable[float]: """ Returns the ratios of prime numbers on the diagonals of the Ulam spiral """ index = 0 primes = 0 while True: primes += 1 if is_prime(_first_spiral_arm(index)) else 0 primes += 1 if is_prime(_second_spiral_arm(index)) else 0 primes += 1 if is_prime(_third_spiral_arm(index)) else 0 primes += 1 if is_prime(_fourth_spiral_arm(index)) else 0 yield primes / (index * 4 + 1) index += 1
def num_consecutive_primes(a: int, b: int): n = 0 while True: if not is_prime(n**2 + a * n + b): break n += 1 return n
def _test_number(number: int) -> bool: """ Check whether the number upholds the conjecture """ for s in _double(_squares()): if s >= number: return False if is_prime(number - s): return True
def max_prime_value_family(self, prime): if prime in self._max_prime_value_families: return len(self._max_prime_value_families[prime]) digits = str(prime) num_digits = len(digits) max_prime_value_family = {prime} indexes_by_digit = defaultdict(set) for i, digit in enumerate(digits): indexes_by_digit[digit].add(i) for replacement_indexes in indexes_by_digit.values(): if (num_digits - 1) in replacement_indexes: replacement_digits = self.VALID_PRIME_ENDING_DIGITS else: min_replacement_digit = 1 if 0 in replacement_indexes else 0 replacement_digits = range(min_replacement_digit, 10) prime_value_family = set() for replacement_digit in replacement_digits: new_digits = list(digits) for index in replacement_indexes: new_digits[index] = str(replacement_digit) new_digits = ''.join(new_digits) new_number = int(new_digits) if is_prime(new_number): prime_value_family.add(new_number) if len(prime_value_family) > len(max_prime_value_family): max_prime_value_family = prime_value_family for p in max_prime_value_family: self._max_prime_value_families[p] = max_prime_value_family return len(self._max_prime_value_families[prime])
def get_next_candidates(self, number, seen_numbers): appended_candidates = (append_digits(number, d) for d in self.SINGLE_DIGIT_PRIMES) prepended_candidates = (prepend_digits(d, number) for d in range(1, 10)) return { candidate for candidate in chain(appended_candidates, prepended_candidates) if candidate not in seen_numbers and is_prime(candidate) }
def is_truncatable_prime(number): if len(str(number)) <= 1: return False if not is_prime(number): return False digits = str(number)[:-1] while len(digits) > 0: if not is_prime(int(digits)): return False digits = digits[:-1] digits = str(number)[1:] while len(digits) > 0: if not is_prime(int(digits)): return False digits = digits[1:] return True
def get_answer(self): side_length = 3 num_primes_along_diagonals = 0 num_along_diagonals = 1 values_along_diagonals_gen = ulams_spiral_diagonal_numbers() next(values_along_diagonals_gen) # skip 1 while True: for _ in range(4): if is_prime(next(values_along_diagonals_gen)): num_primes_along_diagonals += 1 num_along_diagonals += 4 prime_ratio = num_primes_along_diagonals / num_along_diagonals if prime_ratio < 0.1: return side_length side_length += 2
def get_answer(self): largest_pandigital_prime = 0 digits = '1' for x in range(2, 10): digits += str(x) # it's at least a 4-digit so skip 2 and 3. 5,6,8,9 all add up to multiples of 3 so none of them will be prime if not (x == 4 or x == 7): continue for pandigital in permutations(digits): number = int(''.join(pandigital)) if number > largest_pandigital_prime and is_prime(number): largest_pandigital_prime = number assert largest_pandigital_prime != 0 return largest_pandigital_prime
def _quadratic_primes(a: int, b: int) -> int: """ Returns the amount of consecutive primes generated by the quadratic function with these parameters """ for n in count(): if not is_prime(n * n + a * n + b): return n
# Soultion for Project Euler Problem #7 - https://projecteuler.net/problem=7 # (c) 2017 dpetker from util.primes import is_prime n = 13 ctr = 6 while True: n = n + 2 if is_prime(n): ctr += 1 if ctr == 10001: break print('The 10 001st prime number is {}'.format(n))
from PIL import Image from hacker.settings import inputfile, outputfile from util.primes import is_prime filename = inputfile('crypto', 'white_noise', 'whitenoise.png') filename_out1 = outputfile('crypto', 'white_noise', 'whitenoise_out1.png') filename_out_prime = outputfile('crypto', 'white_noise', 'whitenoise_out_prime.png') im = Image.open(filename) # Set the first index in the palette (this outputs an image with a hint) palette = bytearray(256*3) palette[:3] = [255, 255, 255] im.putpalette(palette) im.save(filename_out1, 'PNG') # Set the prime numbers palette = bytearray(256*3) for i in range(256): if is_prime(i + 1): palette[i*3:i*3+3] = [255, 255, 255] im.putpalette(palette) im.save(filename_out_prime, 'PNG') print('The solution is worldofprimenoise')
def test_is_prime(value: int, expected: bool) -> None: assert is_prime(value) == expected
def _is_prime_pair(prime1: int, prime2: int) -> bool: if not is_prime(int(str(prime1) + str(prime2))): return False if not is_prime(int(str(prime2) + str(prime1))): return False return True
def problem_0046() -> int: for i in count(3, 2): if not is_prime(i) and not _test_number(i): return i
top_left = diagonal(1, 4) top_right = diagonal(1, 2) bottom_left = diagonal(1, 6) bottom_right = diagonal(1, 8) p = 0 t = -3 s = -1 while p == 0 or float(p) / t > 0.10: tl = top_left.next() tr = top_right.next() bl = bottom_left.next() br = bottom_right.next() if primes.is_prime(tl): p += 1 if primes.is_prime(tr): p += 1 if primes.is_prime(bl): p += 1 if primes.is_prime(br): p += 1 t += 4 s += 2 if s <= 7: print '[{}] {}/{}={}'.format(s, p, t, float(p)/t) print '[{}] {}/{}={}'.format(s, p, t, float(p)/t)