def factor(n): if m_r(n): return [n] factorList = [] while n != 1: if m_r(n): return (factorList + [n]) d = pollard_rho(n) while n % d == 0: factorList.append(d) n /= d return factorList
def main(): sumz = 0 for i in xrange(LOWER_LIM, UPPER_LIM): if m_r(i): result = f(i, math.sqrt(i)) sumz += result print i, result, '\t', time.time() - START print sumz % MOD
def primeProof(n): for digit in xrange(1, int(log(n, 10)) + 2): relevantDig = n % 10**digit - (n % 10**(digit - 1)) m = int(n - relevantDig) digitChange = 10**(digit - 1) for i in xrange(0, 10): if m_r(m + i * digitChange): return False return True
def primeProof(n): for digit in xrange(1,int(log(n,10))+2): relevantDig = n % 10**digit - (n % 10** (digit-1)) m = int(n - relevantDig) digitChange = 10**(digit-1) for i in xrange(0, 10): if m_r(m + i* digitChange): return False return True
def D(a,b,k): sumz = 0 for p in xrange(a,a+b): if m_r(p): inv = [0] + [ multInverse(x,p) for x in xrange(1, p)] tempSum = 0 rollingNcr = 1 for i in xrange(p-1,k-2,-1): index = p - i tempSum += inv[i] * rollingNcr rollingNcr = (rollingNcr * (k + index - 1) * inv[index]) % p sumz += tempSum % p print p, tempSum % p return sumz
def D(a, b, k): sumz = 0 for p in xrange(a, a + b): if m_r(p): inv = [0] + [multInverse(x, p) for x in xrange(1, p)] tempSum = 0 rollingNcr = 1 for i in xrange(p - 1, k - 2, -1): index = p - i tempSum += inv[i] * rollingNcr rollingNcr = (rollingNcr * (k + index - 1) * inv[index]) % p sumz += tempSum % p print p, tempSum % p return sumz
import time from primes import m_r START = time.time() num = 10**12 start_val = 2 * 10**9 end_val = 2 * 10**9 + 2000 power = 10**4 running_sum = 0 for p in xrange(start_val, end_val): # ignore non primes if m_r(p): nModp = num % p running_sum += (-1 * sum( [ (nModp + 1 - x) * pow(x, power, p) \ for x in xrange((num+1)%p, p+1) ])) % p print p, running_sum print running_sum print "Time Taken:", time.time() - START """ Congratulations, the answer you gave to problem 487 is correct. You are the 358th person to have solved this problem. Answer : 106650212746 Time Taken : 16.3226361275 Quick summary on this problem. The problem asks us to find: sum_{2*10^9 <= p <= 2*10^9, p prime} (
START = time.time() LIM = 10**12 validHammingNumbers = set() validPrimes = set() for i in xrange(0,int(math.log(LIM, 2)) +1): for j in xrange(0,int(math.log(LIM / 2**i, 3)) + 1): for k in xrange(0,18): if 2**i * 3**j * 5**k > LIM: break validHammingNumbers.add(2**i * 3**j * 5**k) # Figure out which primes p have the property totient(p) = hammingNumber for hammingNumber in validHammingNumbers: if m_r(hammingNumber +1): validPrimes.add(hammingNumber +1) validPrimes = validPrimes.difference(set([2,3,5])) # these are included in hamming numbers already validPrimes = sorted(validPrimes)[::-1] validHammingNumbers.remove(1) # unnecessary, and causes slowdown validHammingNumbers = sorted(validHammingNumbers)[::-1] # compute every single product of primes ignoring 2,3,5 which results in totient(product) = hammingNumber newSet = set([1]) for prime in validPrimes: oldSet = newSet.union(set()) for oldNumber in oldSet: if oldNumber * prime <= LIM: newSet.add(prime * oldNumber)
s, t = extended_gcd(b, r) return (t, s - q * t) ext_gcd = extended_gcd ############################################################# ##################### Getting 100,000 primes ################ ############################################################# i = 10**14 count = 0 prime_lst = [] while count < 10**5: i += 1 if m_r(i): count += 1 prime_lst.append(i) print "Time Taken:", time.time() - start ############################################################# ################## Finding the nth fibo num ################ ############################################################# pToTest = [3, 7, 13, 67, 107, 630803] #Factorization of 1234567891011 fibo_lsts = [] for prime in pToTest: fib = [0, 1, 1, 2] while fib[-1] != 1 or fib[-2] != 0:
def get_primes(start, end): primes = [] for i in xrange(start, end): if m_r(i): primes.append(i) return primes
import time from primes import m_r START = time.time() num = 10**12 start_val = 2*10**9 end_val = 2*10**9 + 2000 power = 10**4 running_sum = 0 for p in xrange(start_val, end_val): # ignore non primes if m_r(p): nModp = num % p running_sum += (-1 * sum( [ (nModp + 1 - x) * pow(x, power, p) \ for x in xrange((num+1)%p, p+1) ])) % p print p, running_sum print running_sum print "Time Taken:", time.time() - START """ Congratulations, the answer you gave to problem 487 is correct. You are the 358th person to have solved this problem. Answer : 106650212746 Time Taken : 16.3226361275 Quick summary on this problem.
q, r = a/b, a%b s, t = extended_gcd(b, r) return (t, s - q * t) ext_gcd = extended_gcd ############################################################# ##################### Getting 100,000 primes ################ ############################################################# i = 10**14 count = 0 prime_lst = [] while count < 10**5: i +=1 if m_r(i): count +=1 prime_lst.append(i) print "Time Taken:", time.time() - start ############################################################# ################## Finding the nth fibo num ################ ############################################################# pToTest = [3,7,13,67,107,630803] #Factorization of 1234567891011 fibo_lsts = [] for prime in pToTest: fib = [0,1,1,2] while fib[-1] != 1 or fib[-2] != 0:
pos = n - row_start if pos != 0: if row % 2 == 0: neighbors = [n - row + 2, n - row, n + row] else: neighbors = [n - row + 1, n + row - 1, n + row + 1] else: neighbors = [n + row, n + row + 1, n - row + 1, n - row + 2] return neighbors sumz = 0 for p in pos: i = p * (p - 1) / 2 + 1 while i < p * (p + 1) / 2 - 1: if m_r(i): neigh = neighbors(i, p) neigh_sum = sum([m_r(x) for x in neigh]) if neigh_sum >= 2: sumz += i else: for num in neigh: row = p - 1 if num < i else p + 1 if m_r(num): neigh_sum = sum([m_r(x) for x in neighbors(num, row)]) if neigh_sum >= 2: sumz += i break i += 1 print sumz print sumz
import time, math from primes import m_r, findNumLessThan START = time.time() SIZE = 10**11 RELEVANT_SIZE = SIZE / 21125 # 21125 = 5^3 x 13^2 oneMod4Primes = filter(lambda x: m_r(x), xrange(5, RELEVANT_SIZE + 1, 4)) threeMod4Primes = [2] + filter(lambda x: m_r(x), xrange( 3, RELEVANT_SIZE + 1, 4)) large3Mod4PrimesIndex = findNumLessThan(threeMod4Primes, RELEVANT_SIZE**.5) threeMod4Products = set([1] + threeMod4Primes[large3Mod4PrimesIndex + 1:]) for p in threeMod4Primes[large3Mod4PrimesIndex:0:-1] + [2]: for power in xrange(1, int(math.log(RELEVANT_SIZE, p) + 1)): threeMod4Products = threeMod4Products.union( \ filter((lambda x: x < RELEVANT_SIZE), \ [ x*p**power for x in threeMod4Products])) threeMod4Products = sorted(threeMod4Products) def compute(power1, power2, power3=0): sumz = 0 for prime1 in oneMod4Primes: if prime1**power1 * 5**power2 > SIZE: break for prime2 in oneMod4Primes: #print prime1, prime2, prime1**power1*prime2**power2 if prime1 == prime2:
-> (x^2 - xy + y^2) | y^3 For now, assume: x^2 - xy + y^2 = y^3 Then: x = (y + y * sqrt(4y - 3)) / 2 """ count = 0 for i in xrange(1, int(math.sqrt(LIM)), 2): for v in xrange(1, 100): y = v * (i**2 + 3) / 4 x = y * (1 + i) / (2 * v) if x < y: continue if (x**4 - y**4) % (x**3 + y**3) != 0: # print x, y, (x**4 - y**4) % (x**3 + y**3), "ERROR" continue number = (x**4 - y**4) / (x**3 + y**3) if number > LIM: break if m_r(number): print number, x, y, v, x**4 - y**4, x**3 + y**3 count += number print "answer:", count print "Time taken:", time.time() - START
import time from primes import get_primes, divisors, m_r START = time.time() SIZE = 10**6 * 5 LIM = 10**5 primes = get_primes(SIZE) div308 = divisors(308) results = [] for p in primes: if len(results) > LIM: break if not any([m_r(div * p + 1) for div in div308]): results.append(p) print results[LIM - 2] * 308, len(results) for repetition in xrange(10): resultSet = set(results) for index, a in enumerate(results): if a * results[0] > results[LIM]: break for bIndex in xrange(index + 1): b = results[bIndex] p = a * b if a * b > results[LIM]: break if not any([m_r(div * p + 1) for div in div308]): if len(filter(lambda x: x not in resultSet, divisors(p)[1:-1])) == 0:
from primes import m_r, primes import time START = time.time() SIZE = 10**6 MAX_PRIMES_LIM = 600 # sum of [2,3,5,7, ...] first 546 primes = 997,661 maxNumPrimes = 0 for startingPrime in xrange(0, MAX_PRIMES_LIM): if sum(primes[startingPrime:startingPrime + maxNumPrimes]) > SIZE: break lst = primes[:MAX_PRIMES_LIM] k = startingPrime + 1 while lst[k-1] < SIZE: lst[k] += lst[k-1] k += 1 for i in xrange(k-1,0,-1): if m_r(lst[i]): if maxNumPrimes < i: print lst[i], i maxNumPrimes = i break print "Time taken:",time.time() -START
primeSets = [set() for i in xrange(6)] baseDict = {1 : 0, 2 : 0, 3 : 0, 5 : 0, 7 : 0, 11 : 0, 13 : 0} TOTIENT_VAL = math.factorial(13) INDEX = 150000 -1 possiblePrimes = set() possibleTotients = set([1]) for prime in primeFactors.keys(): newSet = set() for power in xrange(1, primeFactors[prime]+1): newSet.update(set([ prime**power * num for num in possibleTotients])) possibleTotients.update(newSet) for num in possibleTotients: if m_r(num+1): possiblePrimes.add(num+1) # Arrange products into sets based on their largest prime factor for num in list(possiblePrimes): for pIndex in xrange(len(primes)): if (num - 1) % primes[pIndex] == 0: primeSets[pIndex].add(num) break def factorAndAddToDict(num, valueDict): factors = factor(num) for num in factors: valueDict[num] += 1 def checkPrimePowers(valDict, func):
row_start = row*(row-1)/2 + 1 pos = n - row_start if pos != 0: if row % 2 == 0: neighbors = [ n-row+2,n-row,n+row] else: neighbors = [n-row+1,n+row-1,n+row+1] else: neighbors = [n+row,n+row+1,n-row+1,n-row+2] return neighbors sumz = 0 for p in pos: i = p*(p-1)/2+1 while i < p*(p+1)/2 - 1: if m_r(i): neigh = neighbors(i,p) neigh_sum = sum([m_r(x) for x in neigh]) if neigh_sum >= 2: sumz += i else: for num in neigh: row = p-1 if num < i else p+1 if m_r(num): neigh_sum = sum([m_r(x) for x in neighbors(num,row)]) if neigh_sum >= 2: sumz +=i break i+=1 print sumz print sumz
import time, math from primes import m_r, findNumLessThan START = time.time() SIZE = 10**11 RELEVANT_SIZE = SIZE / 21125 # 21125 = 5^3 x 13^2 oneMod4Primes = filter(lambda x: m_r(x), xrange(5,RELEVANT_SIZE+1,4)) threeMod4Primes = [2] + filter(lambda x: m_r(x), xrange(3,RELEVANT_SIZE+1,4)) large3Mod4PrimesIndex = findNumLessThan(threeMod4Primes, RELEVANT_SIZE**.5) threeMod4Products = set([1] + threeMod4Primes[large3Mod4PrimesIndex+1:]) for p in threeMod4Primes[large3Mod4PrimesIndex:0:-1] + [2]: for power in xrange(1,int(math.log(RELEVANT_SIZE,p)+1)): threeMod4Products = threeMod4Products.union( \ filter((lambda x: x < RELEVANT_SIZE), \ [ x*p**power for x in threeMod4Products])) threeMod4Products = sorted(threeMod4Products) def compute(power1,power2, power3=0): sumz = 0 for prime1 in oneMod4Primes: if prime1**power1 * 5**power2 > SIZE: break for prime2 in oneMod4Primes: #print prime1, prime2, prime1**power1*prime2**power2 if prime1 == prime2: continue if prime1**power1 * prime2**power2 > SIZE:
import time START = time.time() from primes import m_r rings = [1] + [3 * x * (x - 1) + 2 for x in xrange(1, 100000)] sol = [0, 1] for i in xrange(2, len(rings) - 2): val = m_r(rings[i+1] - rings[i] +1) + \ m_r(rings[i+2] -rings[i] -1) + \ m_r(rings[i+1] - rings[i] -1) if val == 3: sol += [rings[i]] for i in xrange(2, len(rings) - 2): a = rings[i] - 1 val = m_r(a - rings[i-1]) + \ m_r(a -rings[i-2]) + \ m_r(rings[i+1] - 2 - a) if val == 3: sol += [a] sol.sort() print len(sol) if len(sol) > 2000: print sol[1999] print sol[:10] print "Time Taken:", time.time() - START """ 13:57 ~/Desktop/python_projects/proj_euler $ python prob128.py 2635
-> (x^2 - xy + y^2) | y^3 For now, assume: x^2 - xy + y^2 = y^3 Then: x = (y + y * sqrt(4y - 3)) / 2 """ count = 0 for i in xrange(1, int(math.sqrt(LIM)), 2): for v in xrange(1,100): y = v * (i**2 + 3 ) / 4 x = y * (1 + i) / (2*v) if x < y: continue if (x**4 - y**4) % (x**3 + y**3) != 0: # print x, y, (x**4 - y**4) % (x**3 + y**3), "ERROR" continue number = (x**4 - y**4) / (x**3 + y**3) if number > LIM: break if m_r(number): print number, x,y, v, x**4 - y**4, x**3 + y**3 count += number print "answer:", count print "Time taken:", time.time() - START
old_set = new_set new_set = set() for item in old_set: admiss_set.add(item) admiss_set.remove(1) print len(admiss_set), "is how many admissible numbers we need to deal with," print "Time Taken:", time.time() - start ######################################### Above this is finding admissible numbers fort_num = set() # the pseudo-fortunate numbers for num in admiss_set: n = num+2 while not m_r(n): n += 1 fort_num.add(n-num) print sum(fort_num), "is our final answer" print "Time Taken:", time.time() - start """ Congratulations, the answer you gave to problem 293 is correct. You are the 1292nd person to have solved this problem. 22:17 ~/Desktop/python_projects/proj_euler $ python prob293.py 6656 is how many admissible numbers we need to deal with, Time Taken: 0.0592079162598 2209 is our final answer
old_set = new_set new_set = set() for item in old_set: admiss_set.add(item) admiss_set.remove(1) print len(admiss_set), "is how many admissible numbers we need to deal with," print "Time Taken:", time.time() - start ######################################### Above this is finding admissible numbers fort_num = set() # the pseudo-fortunate numbers for num in admiss_set: n = num + 2 while not m_r(n): n += 1 fort_num.add(n - num) print sum(fort_num), "is our final answer" print "Time Taken:", time.time() - start """ Congratulations, the answer you gave to problem 293 is correct. You are the 1292nd person to have solved this problem. 22:17 ~/Desktop/python_projects/proj_euler $ python prob293.py 6656 is how many admissible numbers we need to deal with, Time Taken: 0.0592079162598 2209 is our final answer Time Taken: 0.359772920609
import time, math from primes import factor, m_r START = time.time() SIZE = 10**11 LIM = SIZE / 12101 MOD = 2017 vals = dict() for n in sorted(range(1722, int(SIZE**(1/2.)), MOD) + range(294, int(SIZE**(1/2.)), MOD)): if n**2 > SIZE: break if m_r(n): vals[n] = 2 for n in sorted(range(1788, int(SIZE**(1/3.)), MOD) + range(229, int(SIZE**(1/3.)), MOD)): if n**3 > SIZE: break if m_r(n): vals[n] = 3 ansNum = 0 for n in xrange(MOD-1, SIZE, MOD): if m_r(n): if n > LIM: # don't bother storing large numbers, they won't run into the duplicate problem for j in xrange(1,SIZE/n+1): ansNum += j * n else: vals[n] = 1 print "Time Taken:", time.time() - START
def get_primes(start,end): primes = [] for i in xrange(start,end): if m_r(i): primes.append(i) return primes