def check_co_primes(my_input: List[BusType]): first_bus = None for bus in my_input: assert is_prime(bus.interval), bus.interval if first_bus is not None: assert is_co_prime(first_bus.interval, bus.interval), (first_bus.interval, bus.interval) first_bus = bus
def test_create_private_key_returns_a_prime_number(self): p = 100 self.client.p = p probe_size = 100 flag = all([ is_prime(self.client.create_private_key()) for _ in range(probe_size) ]) self.assertTrue(flag)
def update_primes(prime_diagonals, diagonals): try: biggest_prime = max(prime_diagonals) except ValueError: print("Empty prime diagonals, using 0 as biggest prime") biggest_prime = 0 candidates = filter(lambda d: d > biggest_prime, diagonals) for c in candidates: if is_prime(c): prime_diagonals.add(c) return prime_diagonals
def prime_family(n, replace_digit): result = [n] str_digit = str(replace_digit) str_n = str(n) other_digits = string.digits.replace(str_digit,'') for d in other_digits: temp = int(str_n.replace(str_digit, d)) if len(str(temp)) == len(str_n): if is_prime(temp): result.append(temp) result.sort() return result
def test_is_prime_works_for_a_couple_of_numbers(self): self.assertFalse(is_prime(256)) self.assertFalse(is_prime(133)) self.assertTrue(is_prime(5)) self.assertTrue(is_prime(7)) self.assertTrue(is_prime(11)) self.assertTrue(is_prime(13))
def not_right(num): for n in twice_squares(num): prime = num - n if is_prime(prime): return True return False
#here's a more pythony way of doing things from itertools import count, dropwhile from Utils import is_prime def twice_squares(limit): for i in range(limit): yield 2*(i**2) def not_right(num): for n in twice_squares(num): prime = num - n if is_prime(prime): return True return False composite_odds = filter(lambda x: not is_prime(x), count(35, 2)) print(next(dropwhile(not_right, composite_odds))) #5777 #mine: works fairly fast # from itertools import count, takewhile # from Utils import is_prime, primes_sieve # #first come up with iterator to generate all odd composite numbers # composite_odds = filter(lambda x: not is_prime(x), count(35, 2)) # # i = 0 # # for c in composite_odds: # # print(c) # # i += 1 # # if i == 10:
def test_is_prime_returns_false_for_one_and_zero(self): self.assertFalse(is_prime(1)) self.assertFalse(is_prime(0))
def test_is_prime_returns_false_for_four(self): self.assertFalse(is_prime(4))
from Utils import primes_sieve, is_prime candidates = filter(lambda x: x > 7, primes_sieve(1000000)) prime_set = set() count = total = 0 for n in candidates: if n not in prime_set: prime_set.add(n) digits = str(n) i = 1 while i < len(digits): if not is_prime(int(digits[i:])): break i += 1 if i == len(digits): i = len(digits) - 1 while i > 0: if not is_prime(int(digits[0:i])): break i -= 1 if i == 0: #print(n) count += 1 total += n
upper_range = 1000000 circular_count = 0 #use set for much better performance primes_seen = set() for p in primes_sieve(upper_range): if p not in primes_seen: primes_seen.add(p) p_digits = str(p) if len(p_digits) > 1: is_circular = True for i in range(1, len(p_digits)): possible = int(p_digits[i:]+p_digits[:i]) if possible in primes_seen: continue elif not is_prime(possible): is_circular = False break else: primes_seen.add(possible) if is_circular: #print(p) circular_count += 1 else: #print(p) circular_count += 1 print(circular_count) #55
def test_create_private_key_returns_a_prime_number(self): p = 100 self.client.p = p probe_size = 100 flag = all([is_prime(self.client.create_private_key()) for _ in range(probe_size)]) self.assertTrue(flag)
#this works, but i had to do a lot of guess work on the limit from Utils import primes_sieve, is_prime limit = 3967 primes = list(primes_sieve(limit)) index = 1 max_prime = 0 max_index = 0 max = len(primes)-1 #print(max) for i in range(max): index = 1 sum = primes[i] while index + i < max: #print("i is %i and index is %i" % (i, index)) sum += primes[i + index] if is_prime(sum): if max_index < index: max_index = index max_prime = sum index += 1 print("Max value %i with sequence length of %i" % (max_prime, max_index+1)) #997651
# the consecutive values n = 0 to 79. The product of the coefficients, -79 and 1601, is -126479. # Considering quadratics of the form: # n^2 + an + b, where |a| < 1000 and |b| < 1000 # where |n| is the modulus/absolute value of n # e.g. |11| = 11 and |4| = 4 # Find the product of the coefficients, a and b, for the quadratic expression that produces the # maximum number of primes for consecutive values of n, starting with n = 0. from Utils import is_prime, primes_sieve #a must be odd and b must be prime max_n = 0 max_a = max_b = 0 for a in range(-999, 999, 2): for b in primes_sieve(1000): n = 1 sum = n**2 + a*n + b while is_prime(sum): n += 1 sum = n**2 + a*n + b if n > max_n: max_n = n p = a*b max_a = a max_b = b print(p) #59231
from itertools import permutations, dropwhile from Utils import is_prime from functools import reduce def gen_pandigitals(): #start with nine digits and work our way down for x in range(9,2,-1): perms = permutations(range(x, 0, -1), x) for p in perms: yield int(''.join(str(d) for d in p)) #my original long-winded way # temp = "" # for d in p: # temp += str(d) # yield int(temp) prime_check = lambda i: not is_prime(i) print(list(dropwhile(lambda x: prime_check(x), gen_pandigitals()))[0]) # for n in gen_pandigitals(): # if n % 2 != 0: # if is_prime(n): # print(n) # break #7652413
def test_is_prime_returns_true_for_two_and_three(self): self.assertTrue(is_prime(2)) self.assertTrue(is_prime(3))
def create_private_key(self): candidate = random.randint(0, self.p - 2) while not is_prime(candidate): candidate = random.randint(0, self.p - 2) return candidate