def isTruncatablePrime(n): if n <= 7: return False s = str(n) length = len(s) for i in range(length): if not isPrime(int(s[i:])): return False for i in range(length): if not isPrime(int(s[:length - i])): return False return True
def distinctPrimeFactors(n): div = divisors(n) div.remove(1) toRemove = set() [toRemove.add(d) for d in div if not isPrime(d)] return div - toRemove
#=============================================================================== # Considering quadratics of the form: # # n^2 + an + b, where |a| < 1000 and |b| < 1000 # # Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, # starting with n = 0. #=============================================================================== from Common import isPrime, prod highStreak = 0 coefficients = [] for a in range(-999, 1000): for b in range(-999, 1000): count = 0 n = 0 while isPrime(n ** 2 + a * n + b): count += 1 n += 1 if count > highStreak: highStreak = count coefficients = [a, b] print(prod(coefficients))
#=============================================================================== # What is the 10001st prime number? #=============================================================================== from Common import isPrime count = 0 i = 1 while True: if (isPrime(i)): count += 1 if count == 10001: break i += 1 print(i)
#=============================================================================== # What is the largest prime factor of the number 600851475143 ? #=============================================================================== from Common import isPrime, divisors print(max([i for i in divisors(600851475143) if isPrime(i)]))
def isCircularPrime(n): s = str(n) for i in range(len(s)): if not isPrime(int(s[i:] + s[:i])): return False return True
#=============================================================================== # Which prime, below one-million, can be written as the sum of the most consecutive primes? #=============================================================================== from Common import primeSieve, isPrime bound = 1000000 primes = list(primeSieve(bound)) length = len(primes) long = primes[0] prime = 0 for i in range(length): total = primes[i] j = i + 1 while total < bound and j < length: total += primes[j] if isPrime(total) and j - i + 1 > long: long = j - i + 1 prime = total j += 1 print(prime)