def consecutivePrimes(a, b): n = 0 while (True): quadradicResult = quadratic(n, a, b) if isPrime(quadradicResult) == False: return n n += 1
def getFactors(input): factors = [] for i in range(1, int(sqrt(input)+1)): if input % i == 0: factors.append(i) factors.append(int(input/i)) return list(filter(lambda x: x != 1 and isPrime(x), factors))
def getRelativePrimes(n): if isPrime(n): rps = list(range(n)) rps.pop(0) return rps factorsOfN = getFactorsWithout1(n) factorsMap[n] = factorsOfN rp = [] for i in range(1, n): factorsOfI = getFactorsWithout1(i) if not hasCommon(factorsOfI, factorsOfN): rp.append(i) return rp
def getFamily(replacedDigits, baseNumber, maxNonPrimes): bnAsStr = list(str(baseNumber)) nonPrimeCount = 0 family = [] if bnAsStr[replacedDigits[0]] != bnAsStr[replacedDigits[1]]: return family for i in range(0, 10): for dig in replacedDigits: bnAsStr[dig] = str(i) if not isPrime(int(''.join(bnAsStr))): nonPrimeCount += 1 if nonPrimeCount > maxNonPrimes: return [] else: family.append(int(''.join(bnAsStr))) return family
print('project euler problem 3') from utils.mathHelpers import isPrime from math import sqrt lim = 600851475143 sqlimit = sqrt(lim) factors = [] for i in range(1, int(sqlimit)): if lim % i == 0 and isPrime(i): factors.append(i) print(factors)
if (srcs.count(ch) != tgts.count(ch)): return False return True print('isPerm', isPerm(123, 312)) print('isPerm2', isPerm(1487, 4817)) print('isPerm3', isPerm(4817, 8147)) print('isPerm4', isPerm(213, 512)) print ('isPerm5', isPerm(6733, 6673)) for starting in range(100, 10000): if (not isPrime(starting)): continue for incr in range(0, 3335): for i in range(1, 3): curr = starting + (i * incr) if (not isPrime(curr)): break if (not isPerm(curr, starting)): break if (i == 2): print('curr is ', curr) print('starting is ', starting) print('incr is ', incr)
family = [] if bnAsStr[replacedDigits[0]] != bnAsStr[replacedDigits[1]]: return family for i in range(0, 10): for dig in replacedDigits: bnAsStr[dig] = str(i) if not isPrime(int(''.join(bnAsStr))): nonPrimeCount += 1 if nonPrimeCount > maxNonPrimes: return [] else: family.append(int(''.join(bnAsStr))) return family print("isPrime", not isPrime(86507)) # brute force: # step thru each permutation of swapped indexes, index 1, index 2... index 1 and 2, index 1 and 3 ... # fix some digits swap some digits. then for each # fixed number and idx pair, try 0-9 (10 ops), for each item that matches, add 1 to the count # permutations of abcde - ab ac ad ae def getPossibleReplacements(max): replacementIndexes = [] for i in range(0, max): for j in range(i, max): for k in range(j, max): newList = [i, j, k] replacementIndexes.append(newList) return replacementIndexes
print('project euler problem 41') from utils.mathHelpers import isPandigital, isPrime from euler32 import getAllPerms, stringifyAndJoin allPerms = [stringifyAndJoin(i) for i in getAllPerms([1, 2, 3, 4, 5, 6, 7])] for cand in range(0, len(allPerms)): print('is prime', isPrime(allPerms[cand])) filtered = [j for j in allPerms if isPrime(j) == True] print('filtered is ', filtered)
print('project euler problem 27') from utils.mathHelpers import isPrime print('isPrime(7) is {}'.format(isPrime(7))) print('isPrime(8) is {}'.format(isPrime(8))) print('isPrime(10) is {}'.format(isPrime(10))) print('isPrime(11) is {}'.format(isPrime(11))) def quadratic(n, a, b): return n**2 + n * a + b def consecutivePrimes(a, b): n = 0 while (True): quadradicResult = quadratic(n, a, b) if isPrime(quadradicResult) == False: return n n += 1 longestStreak = 0 longesta = -1000 longestb = -1000 for a in range(-1000, 1001): for b in range(-1000, 1001): cur = consecutivePrimes(a, b) if (cur > longestStreak): longestStreak = cur
print('project euler problem 37') import math from utils.mathHelpers import isPrime def getIterations(asStr): iterations = set() for idx in range(0, len(asStr)): # compare first and last subStr = asStr[idx:len(asStr)] subStr2 = asStr[0:idx+1] iterations.add(int(subStr)) iterations.add(int(subStr2)) return iterations passed = [] for idx in range(10, 1000000): iter = getIterations(str(idx)) passes = True for item in iter: if (not(isPrime(item))): passes = False break if (passes): passed.append(idx) print('passed is ', passed) print('sum is ', sum(passed))
from utils.mathHelpers import isPrime print('project euler problem 50') primesToOneM = [] for i in range(1, 1000001): if (isPrime(i)): primesToOneM.append(i) print(primesToOneM) print('len is ', len(primesToOneM)) print(sum(primesToOneM[3:546])) longestSplit = 0 lower = 0 upper = 0 for i in range(0, 1000): print('i is now ', i) for j in range(1, 1000): if (j - i < longestSplit): continue curSum = sum(primesToOneM[i:j]) print('curSum is ', curSum) if curSum in primesToOneM: split = j - i if (split > longestSplit): lower = i upper = j longestSplit = split
def getRatioOfPrimesOnDiagonal(diagonals): primeDiags = list(filter(lambda x: isPrime(x), list(diagonals))) return len(primeDiags) / len(list(diagonals))
from math import sqrt from utils.mathHelpers import isPrime print('project euler problem 46') print(isPrime(33)) composites = [] primes = [] doubleOfSquares = [] for i in range(4, 4000): nextOdd = (2 * i) + 1 if (not isPrime(nextOdd)): composites.append(nextOdd) for i in range(0, 4000): nextOdd = (2 * i) + 1 if (isPrime(nextOdd)): primes.append(nextOdd) for i in range(0, 100): nextItem = 2 * (i**2) doubleOfSquares.append(nextItem) print("composites is ", composites) print("primes is ", primes) print("double of squares", doubleOfSquares) def isBuildable(input): result = False for i in range(0, len(primes)):
newString = '' for idx in range(0, len(asStr) - 1): newString += asStr[idx + 1] newString += asStr[0] return newString def getRotations(input): asStr = str(input) rotations = [] rotations.append(int(asStr)) for _ in range(0, len(asStr) - 1): asStr = getNextRotation(asStr) rotations.append(int(asStr)) return rotations primes = [] for cur in range(0, 1000000): rots = getRotations(cur) prime = True for i in range(0, len(rots)): if isPrime(rots[i]) != True: prime = False if (prime): primes.append(cur) print("prime for {} is {}".format(cur, prime)) print("primes is ", len(primes))