def isHarshadPrevPrime(splitNum): total = sum(splitNum) if total == 0: return True num = getNum(splitNum) if (num % total == 0): if (EulerSupport.miller_rabin(int(num / total))): return True return False
def isAbundantNumber(n): result = EulerSupport.FindProperDivisors(n) total = sum(result) if (total > n): return (total, 1) elif (total == n): return (total, 0) else: return (total, -1)
def isPrimeHarshad(num): s_num = str(num) list_of_ints = [int(i) for i in s_num] total = sum(list_of_ints) if total == 0: return True if (num % total == 0): if EulerSupport.miller_rabin(int(num / total)): #print("Test: " + str(num)) return recursiveHarshad(list_of_ints[:-1]) return False
def buildDivList(n): total = 0 #return a list of tuples containing (prime, #ofPrime) of intenger n pList = EulerSupport.countPrimeComponents(n) #print(pList) #Made nonlocal instead of passing result cause it made so sense to constantly pass a consistent variable result = [] def recursiveCount(vals,total): nonlocal result #This would only happen if an empty array were passed if len(vals) > 0: prime, count = vals[0] else: return [1] for i in range(0, count+1): tempTotal = total tempTotal *= pow(prime,i) if (len(vals) == 1): result.append(tempTotal) else: recursiveCount(vals[1:],tempTotal) return recursiveCount(pList,1) #Trim off the last number even though itself is a divisor not valid for this problem #Technically last number will always be equal to n result = result[:-1] firstTotal = sum(result) #The ones that divisors sum to itself are not valid... if n == firstTotal: #total += firstTotal print("Onsie: %d"%firstTotal) #If its less than then we've already covered it elif firstTotal > n: pList = EulerSupport.countPrimeComponents(firstTotal) result = [] recursiveCount(pList,1) result = result[:-1] if sum(result) == n: total += n total +=firstTotal print ("Twosie: %d %d"%(n,firstTotal)) return(total)
def findSumFast(n): print ("Fast Way For N: %d"%n) print ("Start: " + time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) total = 0 num = [0]*20 # We default to 100 because 0-99 are not valid answers num[-3] = 1 #So we don't have to constantly compute length TOP = len(num)-1 i = TOP - 2 #while num[0] < 10: #Constantly Converting the Array to an int is a 100% time increase. #Could write code find the max array value and compares more constantly #Or if you use powers of 10 exclusively like below #while num[-15] < 10: while getNum(num) < n: #Clean Up step if we exceed the bounds of where we are set to 0 and go back up a spot if num[i] > 9: num[i] = 0 i -=1 num[i]+=1 #If our 1-Right deliminated value a Harshad and Reduces to a prime #Then go to the next highest value and check if prime #Else no good and increment elif i == TOP - 1: if isHarshadPrevPrime(num[:i+1]): i+=1 else: num[i] += 1 #If we are at the top check to see if our total number is a prime, else increment elif i == TOP: if (EulerSupport.miller_rabin(getNum(num))): val = getNum(num) total += val num[TOP]+=1 else: num[i]+=1 #Every number preceding the TOP must be a Harshad, except 2nd to top thats Harshad Prime elif i < TOP - 1: if isHarshad(num[:i+1]): i += 1 else: num[i]+=1 print ("Total %d"%total) print ("End: " + time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())+ "\n") return total
def BruteForceSum(n): print ("Brute Force For N: %d"%n) print ("Start: " + time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) if n % 10 != 0: print("Must end in a 0") return(0) n = int(n / 10) total = 0 for x in range(1,n,1): if (isPrimeHarshad(x)): #print(x) for c in "1234567890": val = int(str(x)+c) if (EulerSupport.miller_rabin(val)): #if (True): total+=val #print(val) print ("Total %d"%total) print ("End: " + time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())+"\n") return total
def problem50(n): primes = EulerSupport.getPrimes(n) # print (primes) maxLen = 0 maxPrime = 0 firstPrime = 0 for i in range(0, len(primes)): total = 0 tempTotal = 0 next = i primeLen = 0 while tempTotal < n and next < len(primes): tempTotal = tempTotal + primes[next] next = next + 1 if tempTotal in primes: primeLen = next - i total = tempTotal if primeLen > maxLen: maxLen = primeLen maxPrime = total firstPrime = primes[i] print("Len: %d, Start: %d, max: %d" % (maxLen, firstPrime, maxPrime)) print("Len: %d, Start: %d, max: %d" % (maxLen, firstPrime, maxPrime))
def problem50(n): primes = EulerSupport.getPrimes(n) #print (primes) maxLen = 0 maxPrime = 0 firstPrime = 0 for i in range(0, len(primes)): total = 0 tempTotal = 0 next = i primeLen = 0 while (tempTotal < n and next < len(primes)): tempTotal = tempTotal + primes[next] next = next + 1 if tempTotal in primes: primeLen = next - i total = tempTotal if primeLen > maxLen: maxLen = primeLen maxPrime = total firstPrime = primes[i] print("Len: %d, Start: %d, max: %d" % (maxLen, firstPrime, maxPrime)) print("Len: %d, Start: %d, max: %d" % (maxLen, firstPrime, maxPrime))
#Grid is a 2d array of elements def findLargest(grid): length = len(grid[0]) height = len(grid) #Make sure all rows are equal in size for row in grid: if len(row) != length: print("Rows are not equal length") exit() #Cycle through each item, since random grid must brute force solution #Alg is 0(n^2) greatestProd = 0 for y in range(0,height): for x in range(0,length): if (x <= length-4): greatestProd = max(grid[y][x] * grid[y][x+1] * grid[y][x+2] * grid[y][x+3],greatestProd) if (y <= height - 4): greatestProd = max(grid[y][x] * grid[y+1][x] * grid[y+2][x] * grid[y+3][x],greatestProd) if (y <= height - 4 and x < length-4): greatestProd = max(grid[y][x] * grid[y+1][x+1] * grid[y+2][x+2] * grid[y+3][x+3],greatestProd) if (y <= height - 4 and x >= 3): greatestProd = max(grid[y][x] * grid[y+1][x-1] * grid[y+2][x-2] * grid[y+3][x-3], greatestProd) print("Result: " + str(greatestProd)) if __name__ == "__main__": print("Problem 11") print("What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 2020 grid?") filePath = "./InputFiles/gridP11.txt" grid = EulerSupport.parseUnicodeSepFile(filePath, True) findLargest(grid)
def program(n): print("Result: %d" % n) for i in range(1, n + 1): result = EulerSupport.countPrimeComponents(i)
result = EulerSupport.countPrimeComponents(i) @EulerSupport.printTiming def countStuff(): x = 0 for i in range(1, 10**8): x += 1 print(x) def findSChain(n): None def findAntiChain(listN): None if __name__ == "__main__": print("Problem 386") #EulerSupport.printTiming(program(10**1)) #EulerSupport.printTiming(program(10**2)) #EulerSupport.printTiming(program(10**3)) #EulerSupport.printTiming(program(10**4)) #EulerSupport.printTiming(program(10**5)) #EulerSupport.printTiming(program(10**6)) #EulerSupport.printTiming(program(10**7)) #EulerSupport.printTiming(program(10**8)) EulerSupport.printTiming(countStuff())
#Alg is 0(n^2) greatestProd = 0 for y in range(0, height): for x in range(0, length): if (x <= length - 4): greatestProd = max( grid[y][x] * grid[y][x + 1] * grid[y][x + 2] * grid[y][x + 3], greatestProd) if (y <= height - 4): greatestProd = max( grid[y][x] * grid[y + 1][x] * grid[y + 2][x] * grid[y + 3][x], greatestProd) if (y <= height - 4 and x < length - 4): greatestProd = max( grid[y][x] * grid[y + 1][x + 1] * grid[y + 2][x + 2] * grid[y + 3][x + 3], greatestProd) if (y <= height - 4 and x >= 3): greatestProd = max( grid[y][x] * grid[y + 1][x - 1] * grid[y + 2][x - 2] * grid[y + 3][x - 3], greatestProd) print("Result: " + str(greatestProd)) if __name__ == "__main__": print("Problem 11") print( "What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 2020 grid?" ) filePath = "./InputFiles/gridP11.txt" grid = EulerSupport.parseUnicodeSepFile(filePath, True) findLargest(grid)
def FindLargestPrimeFactor(n): print (max(EulerSupport.listPrimeComponents(n)))
@EulerSupport.printTiming #This function works by cylcing through each least significant digit and adding that number #Then taking the mod 10 of that number and making it the result and then carry the sum over to #the next column of digits def addSplit(nums): word = "" length = len(nums[0][0]) print("Length: " + str(length)) carry = 0 for pos in range(length - 1, -1, -1): #49..0 for num in nums: carry += int(num[0][pos]) dangle = carry % 10 carry -= dangle carry = int(carry / 10) word = str(dangle) + word word = str(carry) + word print(word[:10]) return (word[:10]) if __name__ == "__main__": print("Problem 13") filePath = "./InputFiles/numbersP13.txt" nums = EulerSupport.parseUnicodeSepFile(filePath, False) #This way solved it more cleverly and would be useful in a larger system EulerSupport.printTiming(addSplit(nums)) #This is the way that just summed all the numbers using python EulerSupport.printTiming(cheatway(nums))
import sys import time sys.path.append("../SupportFunctions/") import EulerSupport print (time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) result = EulerSupport.getPrimes(103) print(len(result)) print(result[-3:]) print (time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) result = EulerSupport.getPrimes(1000) print(len(result)) print(result[-3:]) print (time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) print(EulerSupport.isPrime(17)) print(EulerSupport.miller_rabin(19)) print (time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) print (1066340417491710595814572169) print(EulerSupport.miller_rabin( 10663404174917)) print (time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) print(EulerSupport.miller_rabin( 1066340417491710595814572171)) print (time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) print(EulerSupport.miller_rabin( 19134702400093278081449423917)) print (time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()))
import sys import time sys.path.append("../SupportFunctions/") import EulerSupport print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) result = EulerSupport.getPrimes(103) print(len(result)) print(result[-3:]) print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) result = EulerSupport.getPrimes(1000) print(len(result)) print(result[-3:]) print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) print(EulerSupport.isPrime(17)) print(EulerSupport.miller_rabin(19)) print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) print(1066340417491710595814572169) print(EulerSupport.miller_rabin(10663404174917)) print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) print(EulerSupport.miller_rabin(1066340417491710595814572171)) print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())) print(EulerSupport.miller_rabin(19134702400093278081449423917)) print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()))
def program(n): print("Result: %d"%n) for i in range (1,n+1): result = EulerSupport.countPrimeComponents(i) @EulerSupport.printTiming def countStuff(): x=0 for i in range (1,10**8): x+=1 print(x) def findSChain(n): None def findAntiChain(listN): None if __name__ == "__main__": print("Problem 386") #EulerSupport.printTiming(program(10**1)) #EulerSupport.printTiming(program(10**2)) #EulerSupport.printTiming(program(10**3)) #EulerSupport.printTiming(program(10**4)) #EulerSupport.printTiming(program(10**5)) #EulerSupport.printTiming(program(10**6)) #EulerSupport.printTiming(program(10**7)) #EulerSupport.printTiming(program(10**8)) EulerSupport.printTiming(countStuff())
def program(n): print("Result: %d"%n) for i in range (1,n+1): result = EulerSupport.countPrimeComponents(i)
def FindLargestPrimeFactor(n): print(max(EulerSupport.listPrimeComponents(n)))
@EulerSupport.printTiming #This function works by cylcing through each least significant digit and adding that number #Then taking the mod 10 of that number and making it the result and then carry the sum over to #the next column of digits def addSplit(nums): word = "" length = len(nums[0][0]) print("Length: " + str(length)) carry = 0 for pos in range(length-1,-1,-1): #49..0 for num in nums: carry += int(num[0][pos]) dangle = carry % 10 carry -= dangle carry = int(carry / 10) word = str(dangle) + word word = str(carry) + word print(word[:10]) return(word[:10]) if __name__ == "__main__": print("Problem 13") filePath = "./InputFiles/numbersP13.txt" nums = EulerSupport.parseUnicodeSepFile(filePath, False) #This way solved it more cleverly and would be useful in a larger system EulerSupport.printTiming(addSplit(nums)) #This is the way that just summed all the numbers using python EulerSupport.printTiming(cheatway(nums))