def sumOfConsectutivePrimes(limit=1000000): p.genPrimesESieve(1000000, True) i, j = 1, 2 runningSum, maxPrime = p.primes[i], 0 result, longest = runningSum, 1 for i in xrange(1, len(p.primes)): j = i+1 count = 1 runningSum = p.primes[i] if runningSum >= 40: break curLongest, curMaxPrime = 2, 0 while runningSum+p.primes[j] < limit: count +=1 runningSum += p.primes[j] if p.isPrime(runningSum): curLongest = count curMaxPrime = runningSum j += 1 if curLongest >= longest: result = curMaxPrime longest = curLongest print longest, result return longest, result
def largestPandigitalPrime(): p.genPrimesESieve(7654321, True) index = len(p.primes) while index >= 1: prime = p.primes[index] if isPandigital(prime): return prime index -= 1 return None
def sumOfTruncatablePrimes(n=11): S = [] p.genPrimesESieve(1000000, True) for nth in xrange(5, len(p.primes)+1): prime = p.primes[nth] # via wiki, 83 right-truncatable primes vs 4260 left-truncatable # Test left to right prime building first if truncatablePrime(prime, True) and truncatablePrime(prime, False): S.append(prime) n -= 1 if n == 0: break return sum(S)
def firstOfFourConsecutive(): p.genPrimesESieve(1000000, True) consecutivePrimes, firstPrime = 0, 2 n = 2*3*5*7-1 while consecutivePrimes < 4: n += 1 nPrimes = nOfPrimeFactors(n)#len(DistinctPrimes(n)) if nPrimes == 4: consecutivePrimes += 1 if consecutivePrimes == 1: firstPrime = n else: consecutivePrimes = 0 return firstPrime
def golbachProvedWrong(): p.genPrimesESieve(10000, True) for n in xrange(3, p.primes[len(p.primes)], 2): if n in p.primes.values(): continue contradicted = True root = 0 nthPrime = 1 while n >= p.primes[nthPrime]: if isTwiceSquare(n-p.primes[nthPrime]): contradicted = False break nthPrime +=1 if contradicted: return n return 'Something is wrong here...'
def threePrimePermutations(): p.genPrimesESieve(10000, True) i = 1 nPrimes = len(p.primes) while i < nPrimes: prime = p.primes[i] if prime > 1000 and prime != 1487: sortedPrime = sorted(str(prime)) j = i+1 while j < nPrimes: secondPrime = p.primes[j] if sorted(str(secondPrime)) == sortedPrime: diff = secondPrime - prime testValue = secondPrime + diff if sorted(str(testValue)) == sortedPrime and testValue in p.primes.values(): return str(prime) + str(secondPrime) + str(testValue) j += 1 i += 1 return None
def coefficientProducts(a=1000, b=1000): aMax, bMax, nMax = (0, 0, 0); for a in xrange(-1000, 1001): for b in xrange(-1000, 1001): n = 0 while p.isPrime(abs(n*n + a*n + b)): n += 1 if (n > nMax): aMax = a bMax = b nMax = n return aMax*bMax
def coefficientProducts2(): aMax, bMax, nMax = (0, 0, 0) for a in xrange(-999, 1001, 2): key = 1 while p.primes[key] <= 1000: for sign in (1, -1): n = 0; offset = -1 if p.primes[key]%2 == 0 else 0 # Making a even if b is even while p.isPrime(abs(n*n + (a+offset)*n + sign*p.primes[key])): n += 1 if (n > nMax): aMax = a; bMax = p.primes[key]; nMax = n; key += 1 return aMax*bMax, nMax
def truncatablePrime(n, leftToRight=True): '''Returns if all numbers of length ≤ 1 are also prime. leftToRight flag determines in which direction the new number is built up from. ''' n = str(n) if not leftToRight: n = n[::-1] newNumber = '' for i in xrange(len(n)-1): if leftToRight: newNumber += n[i] else: newNumber = n[i] + newNumber if not p.isPrime(int(newNumber)): return False return True
def main(): p.genPrimesESieve(87400) print 'Product of the coefficients for which produces the ' +\ 'longest sequence of primes.' print coefficientProducts2()