def possibilities(limit): for prime in primes(limit=sqrt(limit)): val = prime ** 2 if is_totient_perm(val): return val for prime in primes(limit=limit ** (1.0 / 3)): val = prime ** 3 if is_totient_perm(val): return val
def num_distinct_prime_factors(n): upto = n/2 num_factors = 0 current_factors = set([]) temp = n for p in primes.primes(): if p > upto: break if n % p != 0: continue current_factors.add(p) if n in factors: current_factors = current_factors.union(factors[n]) return len(current_factors), current_factors num_factors += 1 n /= p if len(current_factors) == 0: current_factors.add(n) # n is prime factors[temp] = current_factors return len(current_factors), current_factors
def init_primes(self): # initie le tableau des nombres premiers <987654321 primes_numbers = primes.primes(987654321) self.correct_primes = set([ x for x in primes_numbers if (len(set(str(x))) == len(str(x)) and ('0' not in set(str(x)))) ]) del primes_numbers
def build_prime_set(N): for p in primes.primes(): if p >= N: break g_primes.add(p)
def euler72(upper) : p = primes.primes(upper) d = [-1] * upper for j, pn in enumerate(p) : #print pn, "\r", if pn >= upper: break L = pn - 1 #d[pn] = L a = [pn] for val, k in currentPrimeIterator(pn, upper, pn, L) : d[k] = val a.append(k) for x in a : for i in xrange(j) : px = p[i] for val, k in lowerPrimeIterator(x*px, upper, x, L*x) : if not k in d : d[k] = val print #for i,v in enumerate(d): # if v == -1 : # print i #print sum(v for k,v in d.iteritems()) print sum(d) for i,v in enumerate(d): print i,v #for k,v in d.iteritems() : # print k, v #print visited - len(p), sum((1 if e == 0 else 1) for e in d) print
def euler72(limit) : global count print "*** %d" % limit count = 0 p = primes.primes(limit+1) results = [0] * (limit+1) expandPrimes(results, p, limit) expandComposites(results, p, limit) expandEverythingElse(results, p, limit) print print [i for i,x in enumerate(results) if x == 0 and i >= 2] #for i,x in enumerate(results) : # print i, x return sum(results) def test_euler72() : a = [[5,9], [6,11], [7,17], [8,21], [9, 27], [10, 31], \ [11, 41], [12,45], [13,57], [14,63], [15,71], [16,79], [17,95], [18,101], [19,119], [20,127], \ [21,139], [22,149], [23, 171], [24, 179], [25, 199], [26, 211], [27, 229], [28, 241], [29, 269], \ [30, 277], [31, 307], [32, 323], [33, 343], [34, 359], [35, 383], [36, 395], \ [1000, 304191]] for r in a : result = euler72(r[0]) if result != r[1] : print "%d: expecting %d, got %d" % (r[0], r[1], result) else : print "Success: %d" % r[0] if __name__ == "__main__" : #test_euler72() r = euler72(1000*1000) #r = euler72(1000) print print r
def main(): # I generate a sieve (with erastophene) of the 500500 first primes, actually a bit more beacause the upper bound is # not really accurate sieve = primes.primes(upper_bound_prime(500500)) # the length of sieve is initially 530758 so I restrict its length to 500500 sieve = sieve[:500500] # I want to generate a list which is a copy sieve where I add the powers of the form : "element of sieve"^2 # But when for a element of sieve this item is greater than the last element of the sieve I will do the same with # the formula "element of sieve"^4 until I reach the same limit then with "element"^8 and so on # finally I will take the 500500 smallest items of this list and multiply them # This number is the smallest with 2^500500 divisors # Indeed the sum of the powers of the prime factors of a number with 2^n divisors is n # And since I have choosen the smallest prime powers it is indeed the smallest number with 2^n divisors limit = sieve[-1] i = 0 j = 2 test = True resultat = 1 while test: while sieve[i]**j < limit: sieve.append(sieve[i]**j) i += 1 j *= 2 if i == 0: test = False i = 0 # Now I will take the 500500 smallest elements of sieve t = heapq.nsmallest(500500, sieve) # And finally I make the product modulo 500500507 of all the elements in t for k in t: resultat = (resultat * k) % 500500507 print(resultat) print('temps d execution', time.perf_counter() - start, 'sec')
def main(): start = time.perf_counter() p = primes.primes(5000) i = 0 resultat = 0 while p[i] < 1000: i += 1 p = p[i:len(p)] p1 = [0] * len(p) for j in range(len(p1)): p1[j] = lucas(10**18, 10**9, p[j]) for a1 in range(len(p) - 2): pa = p[a1] pla = p1[a1] for b1 in range(a1 + 1, len(p) - 1): nc1 = [pa, p[b1]] ac1 = [pla, p1[b1]] nab, aab = chinese_remainder(nc1, ac1) for c1 in range(b1 + 1, len(p)): nc2 = [nab, p[c1]] ac2 = [aab, p1[c1]] resultat += chinese_remainder(nc2, ac2)[1] print(resultat-20) print('temps d execution', time.perf_counter() - start, 'sec')
def main(): # I generate a sieve (with erastophene) of the 500500 first primes, actually a bit more beacause the upper bound is # not really accurate sieve = primes.primes(upper_bound_prime(500500)) # the length of sieve is initially 530758 so I restrict its length to 500500 sieve = sieve[:500500] # I want to generate a list which is a copy sieve where I add the powers of the form : "element of sieve"^2 # But when for a element of sieve this item is greater than the last element of the sieve I will do the same with # the formula "element of sieve"^4 until I reach the same limit then with "element"^8 and so on # finally I will take the 500500 smallest items of this list and multiply them # This number is the smallest with 2^500500 divisors # Indeed the sum of the powers of the prime factors of a number with 2^n divisors is n # And since I have choosen the smallest prime powers it is indeed the smallest number with 2^n divisors limit = sieve[-1] i = 0 j = 2 test = True resultat = 1 while test: while sieve[i]**j < limit: sieve.append(sieve[i]**j) i += 1 j *= 2 if i == 0: test = False i = 0 # Now I will take the 500500 smallest elements of sieve t = heapq.nsmallest(500500, sieve) # And finally I make the product modulo 500500507 of all the elements in t for k in t: resultat = (resultat * k) % 500500507 print(resultat) print('temps d\'exécution', time.perf_counter() - start, 'sec')
def euler72(upper): p = primes.primes(upper) d = [-1] * upper for j, pn in enumerate(p): #print pn, "\r", if pn >= upper: break L = pn - 1 #d[pn] = L a = [pn] for val, k in currentPrimeIterator(pn, upper, pn, L): d[k] = val a.append(k) for x in a: for i in xrange(j): px = p[i] for val, k in lowerPrimeIterator(x * px, upper, x, L * x): if not k in d: d[k] = val print #for i,v in enumerate(d): # if v == -1 : # print i #print sum(v for k,v in d.iteritems()) print sum(d) for i, v in enumerate(d): print i, v #for k,v in d.iteritems() : # print k, v #print visited - len(p), sum((1 if e == 0 else 1) for e in d) print
def do_GET(self): log.info("GET Request: %s" % self.path) query = parse_qs(urlparse(self.path).query) status = self._determine_status(query) if status != 200: self.send_error(status, message=self.message, explain=self.explain) return lo = int(query[self.LO][0]) hi = int(query[self.HI][0]) cached = r.get((lo, hi)) if cached is not None: log.info("Found answer in cache") response = cached else: log.info("Calculating and caching answer") response = ','.join(list(map(str, primes(lo, hi)))).encode() r.set((lo, hi), response) log.info("Writing back: %s" % bytes.decode(response)) self.send_response(status) self._headers() self.wfile.write(response)
def euler49(): p = primes() ps = [x for x in p.primesLessThan(10000) if x > 999 ] retVal = [] # print(ps) for item in ps: # item = 1487 s = list(str(item)) perms = [ listToInt(i) for i in permutations(s) if listToInt(i) in ps ] perms.sort() diffs = differences(perms) for key, val in diffs.items(): for k,v in diffs.items(): if key != k: val_set = set(val) intersections = val_set.intersection(v) if len(intersections) > 0: for i in intersections: maxVal = key midVal = key - i minVal = key - 2*i if maxVal in ps and midVal in ps and minVal in ps: if maxVal in perms and midVal in perms and minVal in perms: ret = str(minVal) + str(midVal) + str(maxVal) if ret not in retVal: retVal.append(ret) print(retVal)
def euler5b(list): k = max(list) p = primes.primes(k) a = [0] * len(p) for j in list: # get prime factors f = primes.primeFactors(j) # iterate over p[i] for i in range(0, len(p)): # count of this prime in the prime factors c = len(filter(lambda x: x == p[i], f)) a[i] = max(c, a[i]) # compute the final answer r = 1 for i in range(0, len(p)): r *= p[i] ** a[i] return r
def main(): start = time.perf_counter() p = primes.primes(5000) i = 0 resultat = 0 while p[i] < 1000: i += 1 p = p[i:len(p)] p1 = [0] * len(p) for j in range(len(p1)): p1[j] = lucas(10**18, 10**9, p[j]) for a1 in range(len(p) - 2): pa = p[a1] pla = p1[a1] for b1 in range(a1 + 1, len(p) - 1): nc1 = [pa, p[b1]] ac1 = [pla, p1[b1]] nab, aab = chinese_remainder(nc1, ac1) for c1 in range(b1 + 1, len(p)): nc2 = [nab, p[c1]] ac2 = [aab, p1[c1]] resultat += chinese_remainder(nc2, ac2)[1] print(resultat - 20) print('temps d\'exécution', time.perf_counter() - start, 'sec')
def main(): import time # 启动pyx编译器 import pyximport pyximport.install() # Cython的素数算法实现 import primesCy # Python的素数算法实现 import primes print("Cython:") t1 = time.time() print(primesCy.primes(500)) t2 = time.time() print("Cython time: %s" % (t2 - t1)) print("") print("Python") t1 = time.time() print(primes.primes(500)) t2 = time.time() print("Python time: %s" % (t2 - t1))
def getNumberOfDivisors(num): numDivisors = 1 p = primes() factorization = p.getPrimeFactorization(num) counter = Counter(factorization) for v in counter.values(): numDivisors = numDivisors * (v + 1) return numDivisors
def test_first_primes(): a, b, c, d, e = take(5, primes()) assert a == 2 assert b == 3 assert c == 5 assert d == 7 assert e == 11
def euler7(n): i = 2 while (primes.piLowerBound(i) < n): i *= 2 p = primes.primes(i) return p[n-1]
def euler46() : p = primes.primes(1000*1000) for i in range(1,len(p)) : y = p[1: i+1] for x in range(p[i] + 1, p[i + 1]) : if (x&1) and (not test(x, y)) : #print y return x
def main(): s = 0 for k in p.primes(): if k < 2 * 10 ** 6: s += k else: break return s
def test_faq_cython_compile(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from ensae_teaching_cs.faq.faq_cython import compile_cython_single_script temp = get_temp_folder(__file__, "temp_cython_primes") newfile = os.path.join(temp, "primes.pyx") with open(newfile, "w") as f: f.write(TestFaqCython.primes_pyx) cwd = os.getcwd() if is_travis_or_appveyor() == "travis": # the unit test does not end on travis warnings.warn("test_faq_cython_compile not test on travis") return compile_cython_single_script(newfile, fLOG=fLOG, skip_warn=False) # we checked it worked res = platform.architecture() if sys.platform.startswith("win"): ext = "win_amd64" if res[0] == "64bit" else "win32" name = "primes.cp%d%d-%s.pyd" % ( sys.version_info[0], sys.version_info[1], ext) elif sys.version_info[:2] >= (3, 8): ext = "x86_64-linux-gnu" if res[0] == "64bit" else "x86-linux-gnu" name = "primes.cpython-%d%d-%s.so" % ( sys.version_info[0], sys.version_info[1], ext) else: ext = "x86_64-linux-gnu" if res[0] == "64bit" else "x86-linux-gnu" name = "primes.cpython-%d%dm-%s.so" % ( sys.version_info[0], sys.version_info[1], ext) fname = os.path.join(temp, name) fLOG(fname) if not os.path.exists(fname): raise FileNotFoundError("Not found: '{0}'. Found:\n{1}".format( fname, "\n".join(os.listdir(temp)))) sys.path.append(temp) import primes del sys.path[-1] p = primes.primes(100) fLOG(p) self.assertEqual(p, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541]) fLOG(os.getcwd()) self.assertEqual(cwd, os.getcwd())
def generate_primes(M): for p in primes.primes(): if p > M: return plist.append(p) pset.add(p) results[p] = p - 1
def smallest_prime_nonfactor_(ns): ps = primes(1000) for p in ps: factor = False for n in ns: if n % p == 0: factor = True if not factor: return p return "Not found"
def test_negative_case(self): a = [-40, -2] ans = _( "The prime numbers with a maximum value of {} is {} and you returned {}. \n You should check the conditions to be a prime number." ) for i in range(len(a)): stu_ans = primes.primes(a[i]) corr_ans = corr.primes(a[i]) self.assertEqual(corr_ans, stu_ans, ans.format(a[i], corr_ans, stu_ans))
def test_primes(self): for i, p in zip(range(50), primes()): if i == 0: self.assertEqual(p, 2) if i == 1: self.assertEqual(p, 3) if i == 9: self.assertEqual(p, 29) if i == 49: self.assertEqual(p, 229)
def test_primes(self): a = [2, 70] ans = _( "The prime numbers with a maximum value of {} is {} and you returned {}." ) for i in range(len(a)): stu_ans = primes.primes(a[i]) corr_ans = corr.primes(a[i]) self.assertEqual(corr_ans, stu_ans, ans.format(a[i], corr_ans, stu_ans))
def primefac(n,prints=False,cumul=[]): for p in primes.primes(n): if n%p == 0: if prints: print p cumul.append(p) if n/p == 1: return cumul else: return primefac(n/p, prints, cumul)
def prime_factorization(n): pri = primes(int(n / 2)) if len(pri) == 0: return [n] # base case - if there are no primes less than the number i = 0 for p in pri: if n % p == 0: # if divisible, recursively find the factorization return [p] + prime_factorization(int(n / p)) # if no primes fit the bill, another base case - this number is prime return [n]
def naive_factor(n): f = dict() if n < 0: n = -n f[-1] = 1 root_n = int(sqrt(n)) for p in take_while(lambda p: n > 1 and p <= root_n, primes()): n, i = div_while(n, p) if i > 0: f = dict_sum(f, {p: i}) return f if n == 1 else dict_sum(f, {n: 1})
def primeFac(n): s = int(sqrt(n)) if s == 1: return [n] for x in primes(s): if n % x == 0: return [x] + primeFac(n / x) return [n]
def biggest_prime_factor(n): max = int(math.floor(math.sqrt(n))) ps = primes.primes(max) i = len(ps) - 1 while i: if n % ps[i] == 0: return ps[i] i -= 1 return 1
def prime_factors(n): """Generate prime factors of n""" if n == 1: return for i in primes(): yielded = False while n % i == 0: n /= i if not yielded: yield i yielded = True if n == 1: return
def test_faq_cython_compile(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from ensae_teaching_cs.faq.faq_cython import compile_cython_single_script temp = get_temp_folder(__file__, "temp_cython_primes") newfile = os.path.join(temp, "primes.pyx") with open(newfile, "w") as f: f.write(TestFaqCython.primes_pyx) cwd = os.getcwd() if is_travis_or_appveyor() == "travis": # the unit test does not end on travis warnings.warn("test_faq_cython_compile not test on travis") return compile_cython_single_script(newfile, fLOG=fLOG, skip_warn=False) # we checked it worked res = platform.architecture() if sys.platform.startswith("win"): ext = "win_amd64" if res[0] == "64bit" else "win32" name = "primes.cp%d%d-%s.pyd" % ( sys.version_info[0], sys.version_info[1], ext) else: ext = "x86_64-linux-gnu" if res[0] == "64bit" else "x86-linux-gnu" name = "primes.cpython-%d%dm-%s.so" % ( sys.version_info[0], sys.version_info[1], ext) fname = os.path.join(temp, name) fLOG(fname) if not os.path.exists(fname): raise FileNotFoundError("Not found: '{0}'. Found:\n{1}".format( fname, "\n".join(os.listdir(temp)))) sys.path.append(temp) import primes del sys.path[-1] p = primes.primes(100) fLOG(p) self.assertEqual(p, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541]) fLOG(os.getcwd()) self.assertEqual(cwd, os.getcwd())
def solve(n): ans = n-1 prime_list = primes.primes(int(n/2)) for i in combos(prime_list, n/2): tmp = product(i) tmp = triangle((n-tmp)//tmp) if len(i) % 2 == 0: ans -= tmp else: ans += tmp return ans * 6
def first_with(target): smallest = {} counts = {} for p in primes(): if p > 99: for choice in choices(p): if choice not in smallest: smallest[choice] = p count = counts.setdefault(choice, 0) + 1 counts[choice] = count if count == target: return smallest[choice]
def candidates(count, limit): root = int(limit ** (1.0 / count)) if count == 1: for num in xrange(root - (1 - (root % 2)), 2, -2): if is_prime(num): yield [num] yield [2] else: for prime in primes(start=root, limit=limit): rest = candidates(count - 1, limit // prime) for nums in rest: yield [prime] + nums
def main(): start = time.perf_counter() test = 1 k = 1 pri = primes.primes(1000000) while test: k += 1 if reste(k, pri) > 10**10: test = False print(k) print('temps d\'exécution', time.perf_counter() - start, 'sec')
def test_primes(self): cases = [ ((20, ), [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71 ]), ((1, 6, 2), [3, 7, 13]), ((10, 20, 2), [31, 41, 47, 59, 67]), ((98, 101), [523, 541, 547]), ] for args, expected in cases: result = [x for x in itertools.islice(primes(), *args)] self.assertEqual(result, expected)
def main(): start = time.perf_counter() test = 1 k = 1 pri = primes.primes(1000000) while test: k += 1 if reste(k, pri) > 10**10: test = False print(k) print('temps d execution', time.perf_counter() - start, 'sec')
def test_faq_cython_compile(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") from src.ensae_teaching_cs.faq.faq_cython import compile_cython_single_script temp = get_temp_folder(__file__, "temp_cython_primes") primes = os.path.join(temp, "..", "primes.pyx") shutil.copy(primes, temp) newfile = os.path.join(temp, "primes.pyx") cwd = os.getcwd() if "travis" in sys.executable: # the unit test does not end on travis warnings.warn("test_faq_cython_compile not test on travis") return compile_cython_single_script(newfile, fLOG=fLOG) # we checked it worked if sys.version_info[:2] < (3, 5): assert os.path.exists(os.path.join(temp, "primes.pyd")) else: res = platform.architecture() ext = "win_amd64" if res[0] == "64bit" else "win32" name = "primes.cp%d%d-%s.pyd" % (sys.version_info[0], sys.version_info[1], ext) fname = os.path.join(temp, name) fLOG(fname) assert os.path.exists(fname) sys.path.append(temp) import primes del sys.path[-1] p = primes.primes(100) fLOG(p) assert p == [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541 ] fLOG(os.getcwd()) assert cwd == os.getcwd()
def primePowerCounts(n): """ When given a number n with prime factorisation p1^k1·p2^k2·...·pm^km, iterates over the numbers k1, k2, ..., km. """ p = 2 it = primes() while (p := next(it)) ** 2 <= n: count = 0 while n % p == 0: n //= p count += 1 if count > 0: yield count
def factor_base(n, t): # res = [-1] # if n % 2 == 0: # res += [2] # pi = skip(1, primes()) # skip 2 res = [-1] pi = primes() p = next(pi) while p <= t: # if legendre(n, p) == 1: if legendre(n, p) != -1: res += [p] p = next(pi) return res
def not_working(): for s in ps(): print s for p in primes(): if p > 10**6: break if p > s[-1]: t = list(s) t.append(p) if check(t): print "win" print t print sum(t) break
def test_faq_cython_compile(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from src.ensae_teaching_cs.faq.faq_cython import compile_cython_single_script temp = get_temp_folder(__file__, "temp_cython_primes") primes = os.path.join(temp, "..", "primes.pyx") shutil.copy(primes, temp) newfile = os.path.join(temp, "primes.pyx") cwd = os.getcwd() if "travis" in sys.executable: # the unit test does not end on travis warnings.warn("test_faq_cython_compile not test on travis") return compile_cython_single_script(newfile, fLOG=fLOG) # we checked it worked if sys.version_info[:2] < (3, 5): assert os.path.exists(os.path.join(temp, "primes.pyd")) else: res = platform.architecture() ext = "win_amd64" if res[0] == "64bit" else "win32" name = "primes.cp%d%d-%s.pyd" % ( sys.version_info[0], sys.version_info[1], ext) fname = os.path.join(temp, name) fLOG(fname) assert os.path.exists(fname) sys.path.append(temp) import primes del sys.path[-1] p = primes.primes(100) fLOG(p) assert p == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541] fLOG(os.getcwd()) assert cwd == os.getcwd()
def main(): start = time.perf_counter() p = primes.primes(160001) result = 0 count = 0 i = 3 while count < 40: if pow(10, 10**9, 9 * p[i]) == 1: count += 1 result += p[i] i += 1 print(result) print('temps d execution', time.perf_counter() - start, 'sec')
def main(): p = primes.primes(1000008) compteur = 0 for i in range(2, len(p) - 1): p1 = p[i] p2 = p[i + 1] k = 1 u = str(k) + str(p1) while int(u) % p2 != 0: k += 1 u = str(k) + str(p1) compteur += int(u) print(compteur)
def euler72(limit): global count print "*** %d" % limit count = 0 p = primes.primes(limit + 1) results = [0] * (limit + 1) expandPrimes(results, p, limit) expandComposites(results, p, limit) expandEverythingElse(results, p, limit) print print[i for i, x in enumerate(results) if x == 0 and i >= 2] #for i,x in enumerate(results) : # print i, x return sum(results)
def main(): p = primes.primes(1000008) compteur = 0 for i in range(2, len(p)-1): p1 = p[i] p2 = p[i+1] k = 1 u = str(k)+str(p1) while int(u) % p2 != 0: k += 1 u = str(k)+str(p1) compteur += int(u) print(compteur)
def main(): start = time.perf_counter() p = primes.primes(160001) result = 0 count = 0 i = 3 while count < 40: if pow(10, 10**9, 9 * p[i]) == 1: count += 1 result += p[i] i += 1 print(result) print('temps d\'exécution', time.perf_counter() - start, 'sec')
def p033(): top = 1 bottom = 1 for a in xrange(11, 100): for b in xrange(a+1, 100): if curiousp(a,b): top *= a bottom *= b for p in primes(): if p > top: return bottom while (top % p) == (bottom % p) == 0: top /= p bottom /= p
def main(): start = time.perf_counter() pri = primes.primes(10**8) a = [] resultat = 1 for k in pri: a.append((k, padic_order(10**8, k))) del pri for k in a: terme = 1 + pow(k[0], 2 * k[1], 1000000009) resultat = (resultat * terme) % 1000000009 print(resultat) print('temps d\'exécution', time.perf_counter() - start, 'sec')
def euler47() : limit = 200 * 1000 print "Generating primes" p = primes.primes(limit) x = [0] * limit print "Generating factor counts" for pp in p: for j in xrange(pp, limit, pp) : x[j] += 1 for i in xrange(2, limit) : if all(4 == x[i-j] for j in range(0, 4)) : print i-3 break
def main(): start = time.perf_counter() p = primes.primes(10**5) somme = 0 for i in p: test = True for n in range(1, 17): if pow(10, 10**n, 9*i) == 1: test = False break if test: somme += i print(somme) print('temps d execution', time.perf_counter() - start, 'sec')
def main(): start = time.perf_counter() pri = primes.primes(10**8) a = [] resultat = 1 for k in pri: a.append((k, padic_order(10**8, k))) del pri for k in a: terme = 1 + pow(k[0], 2*k[1], 1000000009) resultat = (resultat * terme) % 1000000009 print(resultat) print('temps d execution', time.perf_counter() - start, 'sec')
def main(): start = time.perf_counter() p = primes.primes(5000) somme = 0 for r in range(2, len(p)): pr = p[r] for q in range(1, r): pq = p[q] temp = pr*pq temp1 = 2*temp - pr - pq for p1 in range(q): somme += p[p1]*temp1 - temp print(somme) print('temps d execution', time.perf_counter() - start, 'sec')
def euler_37(): x = primes.primes(2000000) cand = dict.fromkeys(x[4:], 1) h = [str(p) for p in x[0:100] if 10 < p < 100] for k in remove_candidates(cand, h): cand.pop(k) left = set(p for p in cand if all(int(y) in x for y in truncate_left(p))) right = set(p for p in cand if all(int(y) in x for y in truncate_right(p))) #print left #print right truncatable_prime = left.intersection(right) for i, j in enumerate(sorted(truncatable_prime)): print "%d %d" % (i, j) print sum(truncatable_prime)
def euler47(): limit = 200 * 1000 print "Generating primes" p = primes.primes(limit) x = [0] * limit print "Generating factor counts" for pp in p: for j in xrange(pp, limit, pp): x[j] += 1 for i in xrange(2, limit): if all(4 == x[i - j] for j in range(0, 4)): print i - 3 break
def euler_37() : x = primes.primes(2000000) cand = dict.fromkeys (x[4 : ], 1 ) h = [ str(p) for p in x[0:100] if 10 < p < 100 ] for k in remove_candidates(cand, h) : cand.pop(k) left = set( p for p in cand if all(int(y) in x for y in truncate_left(p)) ) right = set( p for p in cand if all(int(y) in x for y in truncate_right(p)) ) #print left #print right truncatable_prime = left.intersection(right) for i, j in enumerate(sorted(truncatable_prime)) : print "%d %d" % ( i, j ) print sum(truncatable_prime)
def euler72(limit) : # 1 appears in all count = limit - 2 + 1 p = primes.primes(limit + 1) p.append(limit + 50) p_counter = 0 next_prime = p[p_counter] p25 = p[ : 25] #print "Limit: %d Count: %d" % (limit, count) g_fn = gcd.gcd for r in xrange(2, limit) : if r % 1000 == 0 : print r, count, "\r", leftover = limit%r multiples_of_r = (limit - r - leftover) / r x, y = 0, 0 if r == next_prime : p_counter += 1 next_prime = p[p_counter] x, y = (r-1), leftover else : r_temp = r rr = range(r) for pp in p25 : if r_temp % pp == 0 : for ccc in xrange(pp, r, pp) : rr[ccc] = 0 while 1 : qq, rrr = divmod(r_temp, pp) if rrr != 0 : break r_temp = qq if r_temp == 1 or pp > r_temp : break for j in rr : if j : if g_fn(j, r) == 1 : x += 1 if j <= leftover : y += 1 #for j in range(1,r+1) : # if g_fn(j, r) == 1 : # x += 1 # if j <= leftover : # y += 1 #print r, gcd_range, multiples_of_r, leftover print r, (multiples_of_r * x) + y count += (multiples_of_r * x) + y return count