def main(): prime = Prime() prime_list = [] prime_under_one_million = [] for p in prime.get_prime(): if p < 1000: prime_list.append(p) prime_under_one_million.append(p) elif p < 1000000: prime_under_one_million.append(p) else: break negative_prime_list = [0 - v for v in sorted(prime_list, reverse=True)] negative_prime_list.extend(prime_list) # b is try to be big, then n&b co efficient will be big # a is try to be small, then current_max_combination = 0 for a in negative_prime_list: for b in negative_prime_list: combi_cnt = 0 for n in count(): if n * n + a * n + b not in prime_under_one_million: break else: combi_cnt += 1 if combi_cnt > current_max_combination: print 'find an greater combination: a={}, b={} has {} combinations'.format( a, b, combi_cnt) current_max_combination = max(current_max_combination, combi_cnt)
def test_get_prime(self): self.assertEqual(Prime.get_prime(0), 2) self.assertEqual(Prime.get_prime(1), 3) self.assertEqual(Prime.get_prime(2), 5) self.assertEqual(Prime.get_prime(3), 7) self.assertEqual(Prime.get_prime(4), 11) self.assertEqual(Prime.get_prime(5), 13) self.assertEqual(Prime.get_prime(6), 17) self.assertEqual(Prime.get_prime(7), 19)
def main(): prime_ins = Prime() prime_hash = dict() circular_prime_list = list() for i in prime_ins.get_prime(): if i < TARGET: prime_hash[i] = True else: break for prime in prime_hash: circular_list = [] for i in range(1, len(str(prime))): circular_list.append(int(''.join(str(prime)[i:] + str(prime)[0:i]))) for num in circular_list: if not num in prime_hash: break else: print "circular prime found: {}".format(prime) circular_prime_list.append(prime) print "count of all circular prime is {}".format(len(circular_prime_list))
from prime import Prime """ The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. """ prime_list = Prime() prime_sum = 0 for i in prime_list.get_prime(): if i < 2000000: prime_sum += i else: print prime_sum break
Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, 35=243 42=16, 43=64, 44=256, 45=1024 52=25, 53=125, 54=625, 55=3125 If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms: 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100? """ prime_list = list() prime_ins = Prime() for prime in prime_ins.get_prime(): if prime > 100: break else: prime_list.append(prime) def is_prime(n=None): for i in range(2, n): if n % i == 0: return False else: return True def find_ingredient(n=None):