def E49(): prime_perm = [ ] for i in range(1487, 10000): if is_prime(i) == False: continue plist = list(itertools.permutations(str(i))) for each_num in plist: num = "".join(map(str, each_num)) if is_prime(num) == True: prime_perm.append(num) len_p = len(prime_perm) for j in range(0, len_p-1): for k in range(j+1, len_p): if prime_perm[k] == prime_perm[j]: continue diff = int(prime_perm[k]) - int(prime_perm[j]) next_num = int(prime_perm[k]) + diff if (str(next_num) in prime_perm) == True : print prime_perm[j], prime_perm[k], next_num del prime_perm[:] return 0
def CheckRight(num): devide = 1 if (is_prime(num) != True): return -1 for i in range(0, len(str(num)) - 1): num = num / 10 if (is_prime(num) != True): return -1 return 0
def is_prime_set(vals): subsets = itertools.combinations(vals, 2) failed_set = set([]) for a, b in subsets: a = str(a) b = str(b) str1 = a + b str2 = b + a if not is_prime(int(str1)) or not is_prime(int(str2)): return False return True
def E70(): res = 0 min_res = 100 min_i = 100 p_set = [] for i in range (3, MAXNUM, 2): if is_prime(i) == True: continue p_set = factor(i) if len(p_set) != 2: continue if int(p_set[0][1])!=1 or int(p_set[1][1])!=1: continue # [(421, 1), (571, 1)] <- both are 1 cnt = i - (int(p_set[0][0])+int(p_set[1][0])-1) if is_perm(i, cnt) == False: continue res = float(i)/float(cnt) if min_res >= res: min_res = res min_i = i #print min_res, min_i, cnt, (1, p_set[0][0], p_set[1][0], i) print "result", min_res, min_i return 0
def eight_prime_family(prime, rd): c=0 for digit in '0123456789': n = int(string.replace(prime, rd, digit)) if (n>100000 and is_prime(n)): c=c+1 return c==8
def main(): best = int(1000000) for i in range(11, 1000, 2): if i % 5 == 0: i += 2 patterns = deepcopy(patterns5) if i > 100: patterns = deepcopy(patterns6) for j in range(len(patterns)): pattern = fill_pattern(patterns[j], i) for k in range(3): if pattern[0] == -1 and k == 0: continue candidate = generate_number(k, pattern) # print(i, j, k, patterns[j], pattern, candidate, is_prime(candidate), get_family_size(k, pattern)) if is_prime(candidate) and get_family_size( k, pattern) >= 8 and candidate < best: best = candidate break print(best)
def E70(): res = 0 min_res = 100 min_i = 100 p_set = [] for i in range(3, MAXNUM, 2): if is_prime(i) == True: continue p_set = factor(i) if len(p_set) != 2: continue if int(p_set[0][1]) != 1 or int(p_set[1][1]) != 1: continue # [(421, 1), (571, 1)] <- both are 1 cnt = i - (int(p_set[0][0]) + int(p_set[1][0]) - 1) if is_perm(i, cnt) == False: continue res = float(i) / float(cnt) if min_res >= res: min_res = res min_i = i #print min_res, min_i, cnt, (1, p_set[0][0], p_set[1][0], i) print "result", min_res, min_i return 0
def get_family_size(repeated_num, pattern_fam): family_size = 0 for i in range(repeated_num, 10): if is_prime(generate_number(i, pattern_fam)): family_size += 1 return int(family_size)
def test_make_pairs(): print(make_pairs([1, 3, 5])) print(make_pairs([7, 9])) print(make_pairs([75, 23, 18])) test = make_pairs([3, 7, 109, 673]) for elem in iter(test): print(elem, is_prime(elem))
def P(n): all_sum = 0 i = 0 while i <= n: if (is_prime(i)): all_sum += i i += 1 return all_sum
def CheckLeft(num): devide = 1 for i in range(0, len(str(num))): devide *= 10 res = num % devide if (is_prime(res) != True): return -1 return 0
def main(limit): num = 1 vals = [] while num < limit: val = 2*num**2-1 vals.append(val) if not is_prime(val): print val, factor(val) num += 1 return vals
def Get_P(n): i = 2 prime_th = 0 while (1): if (prime_th == n): return (i - 1) break if (is_prime(i)): prime_th += 1 i += 1
def Get_P(n) : i = 2 prime_th = 0 while (1): if (prime_th == n): return (i-1) break if (is_prime(i)): prime_th += 1 i += 1
def project_euler_27(): """Finds the product of a and b for that produces the most primes for consecutive values of n, starting with n = 0. For n^2 + an + b where |a| < 1000 and |b| < 1000.""" best = (0, -1001, -1001) for a in range(-999, 1000): for b in range(-999, 1000): n = 0 while is_prime(abs(n*n + a*n + b)): n += 1 if n > best[0]: best = (n, a, b) print(best[1]*best[2])
def pFact(num): pfactors = [] while True: j = 2 while j < num+1: if num%j==0 and is_prime(j): num = num/j pfactors.append(j) break j+=1 if num==1: break return pfactors
def main(): answers = list() for index_a in range(0, len(primes)): if primes[index_a] * 5 >= result: break if pairs[index_a] is None: pairs[index_a] = make_pairs([index_a]) for index_b in range(index_a, len(primes)): if all([ is_prime(n) for n in iter(make_pairs(answers + [primes[index_b]])) ]): answers.append(primes[index_b]) for index_c in range(index_b, len(primes)): if all([ is_prime(n) for n in iter( make_pairs(answers + [primes[index_c]])) ]): answers.append(primes[index_c]) for index_d in range(index_c, len(primes)): if all([ is_prime(n) for n in iter( make_pairs(answers + [primes[index_d]])) ]): answers.append(primes[index_d]) for index_e in range(index_d, len(primes)): if all([ is_prime(n) for n in iter( make_pairs(answers + [primes[index_e]])) ]): answers.append(primes[index_e]) return answers return "Failed", answers
def sol_1(): def formula(i, j, k): return k**2 + i * k + j max_primes, resI, resJ = 0, 0, 0 for i in xrange(-1000, 1000): for j in xrange(i, 1000): k = 0 while is_prime(formula(i, j, k)): k += 1 if k - 1 > max_primes: max_primes = k - 1 resI, resJ = i, j print resI * resJ
def project_euler_26(): """Calculates the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part. Decimal fraction parts only repeat if d is relatively prime to 10, so we filter out non-primes from consideration. Next, the number of digits is equal to the multiplicative order of 10 mod d.""" best, index = 0, 0 for x in [i for i in range(10,1000) if is_prime(i)]: y = mult_order_mod(x) if y > best: index = x best = y print(index)
def main (): """ """ # primes = [i for i in range(10**3,10**4) if is_prime(i) and '0' not in str(i)] #print primes for p in primes: perms = sorted(list(set([int(''.join(perm)) for perm in permutations(str(p)) if is_prime(int(''.join(perm)))]))) if len(perms) < 3:continue diffDiff = [b-a for a,b in pairwise([y-x for x,y in pairwise(perms)])] if 0 in diffDiff: print p,perms print p,diffDiff print pass
def FindPrimeAndTwiceSquare(n): max_twice = math.sqrt(n) print n, max_twice for i in range(1, int(max_twice)): if (2 * (i **2)) > n: break prime = n - (2 * (i ** 2)) print prime, n if (is_prime(prime)) == True: print i, "=", prime, "+", "2 x ", i, "^2" return True #else : # print "not....",i, "=", prime, "+", "2 x ", i, "^2" return False
def FindPrimeAndTwiceSquare(n): max_twice = math.sqrt(n) print n, max_twice for i in range(1, int(max_twice)): if (2 * (i**2)) > n: break prime = n - (2 * (i**2)) print prime, n if (is_prime(prime)) == True: print i, "=", prime, "+", "2 x ", i, "^2" return True #else : # print "not....",i, "=", prime, "+", "2 x ", i, "^2" return False
def main (): """ """ maxN = 10**6+1 maxConsecTerms = 0 maxPrime = 0 # make a list of primes primes = [i for i in range(2,maxN) if is_prime(i)] for j,p in enumerate(primes): print j/780. # make a list of the candidate primes cand = [c for c in primes[:j]] # starting at each prime in the candidate list... consecTerms=0 for ind,c in enumerate(cand): candSum = 0 i = ind while candSum < p and i < len(cand): candSum += cand[i] i += 1 # aggregate terms until they equal or exceed the target prime #print p,c,candSum consecTerms = i-ind if p == candSum: if consecTerms > maxConsecTerms: maxPrime = p maxConsecTerms = consecTerms #print p,i-ind #,sum(cand[ind:i]),cand[ind:i] break continue print '****',maxPrime,maxConsecTerms print 'And the answer is:',maxPrime,'with a total of',maxConsecTerms,'consecutive terms.' pass
def CirPrime(num): buf = str(num) len_buf = len(buf) devide = 1 for i in range (0, len_buf-1): devide *= 10 new_num = num for i in range(0, len_buf): #print new_num if (is_prime(new_num)) : d1 = new_num / devide d2 = new_num % devide new_num = d2 * 10 + d1 else : return -1 return 0
def E69(): res = 0 max_res = 0 max_i = 0 for i in range (2, MAXNUM): if i % 2 == 1: continue; if is_prime(i) == True: continue cnt = 1 for j in range (1, i): if gcd(i, j) == 1: cnt+=1 if cnt != 0: res = float(i)/float(cnt) if max_res < res: max_res = res max_i = i print max_res, max_i return 0
def E69(): res = 0 max_res = 0 max_i = 0 for i in range(2, MAXNUM): if i % 2 == 1: continue if is_prime(i) == True: continue cnt = 1 for j in range(1, i): if gcd(i, j) == 1: cnt += 1 if cnt != 0: res = float(i) / float(cnt) if max_res < res: max_res = res max_i = i print max_res, max_i return 0
chart[(i, j)] = result return result else: result = sum_of_primes(i - 1, j) - primes[i - 1] chart[(i, j)] = result return result (highest, num_of_terms) = (41, 6) starting_prime_index = 0 for i in xrange(0, 4): #index of the starting prime m = num_of_terms for j in xrange(i + m, 550): #index of the ending prime the_sum = sum_of_primes(i, j) if the_sum > 1000000: break #increment here to test if is_prime(the_sum): highest = the_sum num_of_terms = j - i starting_prime_index = i print highest, "is the sum" print num_of_terms, "terms in the sum" print "The sum starts at the prime", primes[starting_prime_index] print time.clock() - t_0, " seconds to complete" ##k=1000000 ##while not is_prime(k): ## k -= 1 ## ##print k, "has index", primes.index(k) ##
chart[(i,j)] = result return result else: result = sum_of_primes(i-1,j) - primes[i-1] chart[(i,j)] = result return result (highest, num_of_terms) = (41,6) starting_prime_index = 0 for i in xrange(0,4): #index of the starting prime m = num_of_terms for j in xrange(i+m,550): #index of the ending prime the_sum = sum_of_primes(i,j) if the_sum > 1000000: break #increment here to test if is_prime(the_sum): highest = the_sum num_of_terms = j-i starting_prime_index = i print highest, "is the sum" print num_of_terms, "terms in the sum" print "The sum starts at the prime", primes[starting_prime_index] print time.clock() - t_0, " seconds to complete" ##k=1000000 ##while not is_prime(k): ## k -= 1 ##
#27. Come up with a formula n^2 + an + b for |a|,|b|<=1000 which #generates the most number of consecutive primes starting from n=0. ## Here's the output: ##-59231 is the product of a and b ##70 is the maximum consecutive number of primes ##8.589944 seconds to complete from Euler import is_prime import time import cProfile t_0 = time.clock() max_consecutive = 0 for a in xrange(-1000,1001): for b in xrange(-1000,1001): for n in xrange(80): if not is_prime(n**2+a*n+b): break elif n > max_consecutive: max_consecutive = n+1 if max_consecutive==70: print a*b, " is the product of a and b" print max_consecutive, " is the maximum consecutive number of primes" print time.clock()-t_0, " seconds to complete"
from Euler import is_prime import math def FindPrimeAndTwiceSquare(n): max_twice = math.sqrt(n) print n, max_twice for i in range(1, int(max_twice)): if (2 * (i **2)) > n: break prime = n - (2 * (i ** 2)) print prime, n if (is_prime(prime)) == True: print i, "=", prime, "+", "2 x ", i, "^2" return True #else : # print "not....",i, "=", prime, "+", "2 x ", i, "^2" return False MAX_LIMIT = 1000000 count = 0 for i in range(3, MAX_LIMIT, 2): if (is_prime(i) == True): continue if (FindPrimeAndTwiceSquare(i) == False): print "(DONE) result = [",i,"]" exit(0)
#!/usr/bin/python -tt import sys sys.path.append('/Users/admin/Desktop/python-work/Project_Euler/primes') from Euler import prime_sieve, is_prime nmax = 0; primes = prime_sieve(1000) for a in range(-999,999,2): for b in primes: n = 1 while is_prime(n**2 + a*n + b): n += 1 if n>nmax: nmax, p = n, a*b print "Answer to PE27 = ",p def main(): print 'hello' # Standard boilerplate to call the main() function. if __name__ == '__main__': main()
from Euler import prime_sieve, is_prime max = 1000000 primes = prime_sieve(max) prime_sum = [0] sum = 0 count = 0 while (sum<terms and is_prime(n)): (terms, max_prime) = (j-i, n) print "Project Euler 50 Solution = ", max_prime, " with ", terms, " terms"
def right_prime(n): if len(str(n)) ==1:return is_prime(n) return is_prime(n) and right_prime(int(str(n)[1:]))
from Euler import is_prime num_diag_primes = 3 step_length = 2 corner = 9 while float(num_diag_primes) / (2 * step_length + 1) > 0.1: step_length += 2 for i in range(0, 3): corner += step_length if is_prime(corner): num_diag_primes += 1 corner += step_length print(step_length + 1) # SOLVED : 26241
def genPrimes(maxN): primes = [] for n in range(2, maxN + 1): if is_prime(n): primes.append(n) return primes
def left_prime(n): if len(str(n)) ==1:return is_prime(n) return is_prime(n) and left_prime(int(str(n)[:-1]))
BUF[a4] = 1 for a5 in range(1, 8): if (BUF[a5] == 1): continue BUF[a5] = 1 for a6 in range(1, 8): if (BUF[a6] == 1): continue BUF[a6] = 1 for a7 in range(1, 8): if (BUF[a7] == 1): continue count += 1 buf = "%d%d%d%d%d%d%d" % (a1, a2, a3, a4, a5, a6, a7) if (is_prime(int(buf)) == True): print "prime = ", buf BUF[a7] = 0 BUF[a6] = 0 BUF[a5] = 0 BUF[a4] = 0 BUF[a3] = 0 BUF[a2] = 0 ''' for a1 in range(1, 10): BUF = [0,0,0,0,0,0,0,0,0,0] BUF[a1] = 1 for a2 in range(1, 10): if (BUF[a2] == 1): continue BUF[a2] = 1
#!/usr/bin/python from Euler import is_prime from time import clock # start a process timer clock() # setup a counter max = 0 # get a list of primes z = [n for n in range(10**6+1) if is_prime(n)] lenght = len(z) for a in range(0, lenght-1): # t = 0 sum = 0 wyrazy = 0 # for i in range(a, lenght-1): sum = sum + z[i] if sum > 10**6: break wyrazy += 1 if is_prime(sum) and sum != z[i] and wyrazy > max: r = sum max = wyrazy print(r)
#!/usr/bin/python from Euler import is_prime from sys import stdout print "{ ", for n in range(2,10**6): if is_prime(n) : stdout.write("'%s': 1, " % (str(n))) print "}"
def test(n): for factor in divisors(n): if not is_prime(factor + (n / factor)): return False return True
''' What is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%? ''' from Euler import is_prime, itertools # consider a layer N # for example, N = 3 # ....... ....... # .NNNNN. S .C...C. # .N...N. S ..XXXX. # .N...N. => S => ..XXXX. # .N...N. S ..XXXX. # .NNNNN. S .C..... # ....... ....... count = 0 for N in itertools.count(start=2): S = 2*N-1 X = (S-1)*(S-2) i = X+1 count += sum(is_prime(i+j*(S-1)) for j in range(3)) total = 4*N-3 if count/total < 0.1: break ans = S print(ans)
def project_euler_3(): '''Prints the largest prime factor of 600851475143. See the is_prime method in Euler.py for more details.''' print(max(i for i in range(1, int(600851475143**.5) +1) if 600851475143 % i == 0 and is_prime(i)))
<i>n</i>² + <i>an</i> + <i>b</i>, where |<i>a</i>| < 1000 and |<i>b</i>| < 1000<br /><br /> <div class="info" style="text-align:left;">where |<i>n</i>| is the modulus/absolute value of <i>n</i><br />e.g. |11| = 11 and |−4| = 4</div> </blockquote> <p>Find the product of the coefficients, <i>a</i> and <i>b</i>, for the quadratic expression that produces the maximum number of primes for consecutive values of <i>n</i>, starting with <i>n</i> = 0.</p> </div><br /> <br /></div> """ from Euler import is_prime max_cnt = 0 max_a = 0 max_b = 0 for a in range(-1000, 1001): for b in range(-1000, 1001): n = 0 while (True): result = (n * n) + (a * n) + b if (is_prime(result)): n += 1 else: if (max_cnt < n): max_cnt = n max_a = a max_b = b break print max_cnt, max_a, max_b print "result=", (max_a * max_b)
#!/usr/bin/python -Wall # -*- coding: utf-8 -*- """ <div id="content"> <div style="text-align:center;" class="print"><img src="images/print_page_logo.png" alt="projecteuler.net" style="border:none;" /></div> <h2>Largest prime factor</h2><div id="problem_info" class="info"><h3>Problem 3</h3><span>Published on Friday, 2nd November 2001, 06:00 pm; Solved by 281868; Difficulty rating: 5%</span></div> <div class="problem_content" role="problem"> <p>The prime factors of 13195 are 5, 7, 13 and 29.</p> <p>What is the largest prime factor of the number 600851475143 ?</p> <!-- Note: This problem has been changed recently, please check that you are using the right number. --> </div><br /> <br /></div> """ from Euler import is_prime #limit = 13195 limit = 600851475143 i = 3 max_num = 0 for i in range(i, 10000): if (is_prime(i)): if (limit % i == 0): temp = limit // i max_num = i print "max =", max_num
import math def FindPrimeAndTwiceSquare(n): max_twice = math.sqrt(n) print n, max_twice for i in range(1, int(max_twice)): if (2 * (i**2)) > n: break prime = n - (2 * (i**2)) print prime, n if (is_prime(prime)) == True: print i, "=", prime, "+", "2 x ", i, "^2" return True #else : # print "not....",i, "=", prime, "+", "2 x ", i, "^2" return False MAX_LIMIT = 1000000 count = 0 for i in range(3, MAX_LIMIT, 2): if (is_prime(i) == True): continue if (FindPrimeAndTwiceSquare(i) == False): print "(DONE) result = [", i, "]" exit(0)
continue BUF[a4] = 1 for a5 in range(1, 8): if (BUF[a5] == 1): continue BUF[a5] = 1 for a6 in range(1, 8): if (BUF[a6] == 1): continue BUF[a6] = 1 for a7 in range(1, 8): if (BUF[a7] == 1): continue count += 1 buf = "%d%d%d%d%d%d%d" % (a1,a2,a3,a4,a5,a6,a7) if (is_prime(int(buf)) == True): print "prime = ", buf BUF[a7] = 0 BUF[a6] = 0 BUF[a5] = 0 BUF[a4] = 0 BUF[a3] = 0 BUF[a2] = 0 ''' for a1 in range(1, 10): BUF = [0,0,0,0,0,0,0,0,0,0] BUF[a1] = 1 for a2 in range(1, 10): if (BUF[a2] == 1):
def r_p(p): for i in trunc_r(p): if not is_prime(i): return False return True
from Euler import prime_sieve, is_prime max = 1000000 primes = prime_sieve(max) prime_sum = [0] sum = 0 count = 0 while (sum < terms and is_prime(n)): (terms, max_prime) = (j - i, n) print "Project Euler 50 Solution = ", max_prime, " with ", terms, " terms"
def is_valid(my_list): return all((is_prime(str(p[0]) + str(p[1]))) for p in perms(my_list, 2))
"""fact = lambda n: reduce (lambda x,y:x*y, range(1,n+1)) def mod10(n): r = 0 for i in range(1,n+1): f = fact(i) r = (r + pow(i,f,10)) % 10 return r while True: a = raw_input() if a == '#':break print mod10(int(a))""" from Euler import is_prime if is_prime(23): print 'hi' else: print 'no'
def genPrimes(maxN): primes = [] for n in range(2, maxN+1): if is_prime(n): primes.append(n) return primes
""" <div id="content"> <div style="text-align:center;" class="print"><img src="images/print_page_logo.png" alt="projecteuler.net" style="border:none;" /></div> <h2>Largest prime factor</h2><div id="problem_info" class="info"><h3>Problem 3</h3><span>Published on Friday, 2nd November 2001, 06:00 pm; Solved by 281868; Difficulty rating: 5%</span></div> <div class="problem_content" role="problem"> <p>The prime factors of 13195 are 5, 7, 13 and 29.</p> <p>What is the largest prime factor of the number 600851475143 ?</p> <!-- Note: This problem has been changed recently, please check that you are using the right number. --> </div><br /> <br /></div> """ from Euler import is_prime #limit = 13195 limit = 600851475143 i = 3 max_num = 0 for i in range (i, 10000): if (is_prime(i)): if (limit % i == 0): temp = limit // i max_num = i print "max =", max_num
<div class="info" style="text-align:left;">where |<i>n</i>| is the modulus/absolute value of <i>n</i><br />e.g. |11| = 11 and |−4| = 4</div> </blockquote> <p>Find the product of the coefficients, <i>a</i> and <i>b</i>, for the quadratic expression that produces the maximum number of primes for consecutive values of <i>n</i>, starting with <i>n</i> = 0.</p> </div><br /> <br /></div> """ from Euler import is_prime max_cnt = 0 max_a = 0 max_b = 0 for a in range (-1000, 1001): for b in range (-1000, 1001): n = 0 while (True): result = (n*n) + (a*n) + b if (is_prime(result)): n += 1 else: if (max_cnt < n): max_cnt = n max_a = a max_b = b break print max_cnt, max_a, max_b print "result=", (max_a * max_b)