def smallest_multiple_fastest(n): prod = 1 for prime in generate_primes(int(n**0.5)): prod *= prime**int(log(n, prime)) for prime in generate_primes(int(1 + n**0.5), n): prod *= prime return prod
def get_circular_prime(range_to): count = 1 #2! prime_list = primesieve.generate_primes(range_to) #[2, 3, 5, 7, 11, 13, 17, 19....] for prime in prime_list: if is_circular_prime(prime) == True : count += 1 return count
def run(n): pl = ps.generate_primes(n) ret = 0 for q in pl: if is_unique(q): ret += q return ret
def get_circular_prime(range_to): count = 1 #2! prime_list = primesieve.generate_primes( range_to) #[2, 3, 5, 7, 11, 13, 17, 19....] for prime in prime_list: if is_circular_prime(prime) == True: count += 1 return count
def get_biggest_decimal_pandigital(n): max_pan = 0 permu_obj = itertools.permutations(range(1, n + 1)) for pan in permu_obj: each_pan = int(''.join(map(str, pan))) if len(generate_primes(each_pan, each_pan)) > 0 and each_pan > max_pan: max_pan = each_pan return max_pan
def get_biggest_decimal_pandigital(n): max_pan = 0 permu_obj = itertools.permutations(range(1, n+1)) for pan in permu_obj: each_pan = int(''.join(map(str,pan))) if len(generate_primes(each_pan, each_pan)) > 0 and each_pan > max_pan: max_pan = each_pan return max_pan
def getPrimesOfMagnitudePS(mag): """ Get all primes of mag orders of magnitude using PrimeSieve """ lwr = 10**(mag-1) upr = 10**mag return ps.generate_primes(lwr,upr)
def list_generate_primes_array(a, b): logger.info("starting generating primes from {} to {}".format(a, b)) logger.info("\tusing primesieve list prime generation") a = primesieve.generate_primes(a, b) logger.info("\tprimes generated") a = np.array(a, dtype=np.uint64) logger.info("\toutput array created") logger.info("done generating primes") return a
def get_still_prime_sum(range_from, range_to): prime_list = primesieve.generate_primes(range_from, range_to) result_cnt = 0 result_sum = 0 for prime in prime_list: if is_still_prime(prime): result_sum += prime result_cnt += 1 if result_cnt == 11 : break return result_sum
def get_still_prime_sum(range_from, range_to): prime_list = primesieve.generate_primes(range_from, range_to) result_cnt = 0 result_sum = 0 for prime in prime_list: if is_still_prime(prime): result_sum += prime result_cnt += 1 if result_cnt == 11: break return result_sum
def search_cumulative_primes(max_range): """ Search for the first cumulative """ log.info("Start looking for cumulative primes: %d" % max_range) res = None num_of_primes = 0 primes = primesieve.generate_primes(max_range) primesum = generate_primes_sum(primes) # check for every cumulative sum, every possible sum of primes for i in range(len(primesum)): j = i - (num_of_primes + 1) while j > 0: prime_candidate = primesum[i] - primesum[j] if prime_candidate > max_range: break if binary_search(primes, prime_candidate): num_of_primes = i - j res = prime_candidate j = j - 1 return res
import primesieve from allfunction import * def shengcheng(n): if len(str(n)) == 1: return [n] count = 0 a = primesieve.generate_primes(1000000) isprimelist = [False for _ in range(1000001)] for i in a: isprimelist[i] = True for p in a: #b=num2list(p) l = len(str(p)) h = 10**(l - 1) flag = True t = p for i in range(1, l): t = t // 10 + t % 10 * h if not isprimelist[t]: flag = False break if flag: print(p) count += 1 '''
6 1,5 2 3 2 3 4 7 1,2,3,4,5,6 6 1.1666... 8 1,3,5,7 4 2 2 4 6 9 1,2,4,5,7,8 6 1.5 3 6 10 1,3,7,9 4 2.5 2 4 5 6 8 It can be seen that n=6 produces a maximum n/φ(n) for n ≤ 10. Find the value of n ≤ 1,000,000 for which n/φ(n) is a maximum. ''' from pprint import pprint from primesieve import generate_primes limit = 1000000 primes = generate_primes(limit) inverses = { key: set() for key in range(2, limit+1) } def check_prime(n): return len(generate_primes(n, n)) for p in primes: #print('\nADDING PRIME', p, '\n') #if check_prime(p): for n in range(p*2, limit+1, p): #print('adding', p, 'to n', n) for inverse in range(p, n-1, p): #print(inverse) inverses[n].add(inverse)
# -*- coding: utf-8 -*- """ Created on Sun May 14 00:59:16 2017 @author: Steven """ import timeit from primesieve import generate_primes start = timeit.default_timer() print(sum(generate_primes(2000000))) stop = timeit.default_timer() print(stop - start, "seconds")
def test_generate_primes(): assert primesieve.generate_primes(10) == [2, 3, 5, 7]
Find the sum of all the primes below two million. ''' if __name__ == '__main__': print len([ 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 ]) print list(primesbelow(108)) # print generate_primes(2*10**6) import timeit limit = 2 * 10**6 print sum(primesbelow(limit)) print sum(generate_primes(limit)) times = 10**4 t1 = timeit.timeit("primesbelow(2*10**6)", setup="from __main__ import primesbelow", number=times) / float(times) t2 = timeit.timeit("generate_primes(2*10**6)", setup="from __main__ import generate_primes", number=times) / float(times) print "primesbelow", t1 * 1000, 'ms' print "generate_primes", t2 * 1000, 'ms' print "My solution used: ", 1000 * t1, "ms" print "The C++ solution used: ", 1000 * t2, "ms"
Prime pair sets Problem 60 The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. ''' from primesieve import generate_primes full_primes = generate_primes(100000000) p_set = set(full_primes) primes = generate_primes(674, 10000000) four = [3, 7, 109, 673] for n in primes: print(n) okay = True for m in four: str_n = str(n) str_m = str(m) a = int(str_n + str_m) b = int(str_m + str_n) if not ((a in p_set) and (b in p_set)): print(n, m, 'is not okay!!!')
def smallest_multiple_fast(n): prod = 1 for prime in generate_primes(n): prod *= prime**int(log(n, prime)) return prod
n = 1 i = 1 length = 1 solved = False corners = [1] prime_tally = 0 total_tally = 1 while not solved: for j in range(4): corner = n + (2 * i) #print(corner) corners.append(corner) if generate_primes(corner, corner): #if corner in primes: prime_tally += 1 total_tally += 1 n += (2 * i) length = (2 * i) + 1 ratio = prime_tally / total_tally #print('Length:', length) #print(prime_tally, '/', total_tally, '=', ratio, '\n') if ratio < 0.1: solved = True i += 1 print('Length:', length) print(prime_tally, '/', total_tally, '=', ratio, '\n') '''
def check(n): if not n in d: test = generate_primes(n, n) d[n] = len(test) return d[n]
primes = [] bin_mask = 1 limit = 1 quota = 8 def find_first(s): prime_set = set(primes) for n in range(10): to_check = s.replace('*', str(n)) if int(to_check) in prime_set: print("It's", to_check) exit() while True: print('Checking up to', limit*10) primes = generate_primes(limit, limit*10) for i in range(1, bin_mask*2-1): hits = {} mask = str("{0:b}".format(i)).zfill(len(str(limit))) for p in primes: s = '' digit = None for i, d in enumerate(str(p)): fail = False if mask[i] == '1': if d == digit or digit == None: s += '*' digit = d else: fail = True break
def prime_check(n): if not n in primes_d: test = generate_primes(n, n) primes_d[n] = len(test) return primes_d[n]
def test_generate_primes(): assert primesieve.generate_primes(10) == [2,3,5,7]
def check(n): generate_primes(n, n) return d[n]
def check_prime(n): return len(generate_primes(n, n))
reader = csv.reader(recipe) keys = [] for row in reader: type = row[0] if type in ['file']: _, start, stop, path = row start = int(start or 0) stop = int(stop or -1) if stop < 0: stop = None key = FileSource(path, start, stop) if type in ['www']: _, start, stop, url = row start = int(start or 0) stop = int(stop or -1) if stop < 0: stop = None key = WebSource(url, start, stop) if type in ['passphrase']: _, phrase = row key = passphrase(phrase) keys.append(key) return (keys) primes = filter(lambda x: x % 4 == 3, primesieve.generate_primes(10**6))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Feb 27 08:47:05 2017 @author: Pranavtadepalli """ import primesieve primes = primesieve.generate_primes(2000000) print(sum(primes))
def test_generate_primes(): assert primesieve.generate_primes(10) == [2,3,5,7] assert primesieve.generate_primes(10, 20) == [11,13,17,19]