14 = 2 × 7 15 = 3 × 5 The first three consecutive numbers to have three distinct prime factors are: 644 = 2**2 × 7 × 23 645 = 3 × 5 × 43 646 = 2 × 17 × 19. Find the first four consecutive integers to have four distinct prime factors. What is the first of these numbers? """ import common primes = common.getPrimes(10**5) def getPrimeFactorsFast(number): global primes primeFactors = [] for i in primes: if number % i == 0 and i not in primeFactors: primeFactors.append(i) if i>number/2: break return primeFactors def numCanditatesAround(num,isPrev=False,lim = 4): nc = 0 while True and nc<=lim: if isPrev:
#-*-coding:utf-8 -*- """ The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Find the sum of the only eleven primes that are both truncatable from left to right and right to left. NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. """ import common primes = set(common.getPrimes(10**6)) #don't seek if prime has even digit or 5 optiPrimes= set([x for x in primes if not any(d in str(x) for d in ["0","2","4","5","6","8"])]) tprimes = [] for p in optiPrimes: s = str(p) isTruncatable = True for i in range(1,len(s)): if not int(s[:i]) in primes or not int(s[i:]) in primes: isTruncatable = False break if isTruncatable:
#-*- coding: utf-8 -*- """ 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? """ import common allDivisors=[] primes= common.getPrimes(21) for i in range(1,21): pdivs = common.getPrimeDivisors(i,primes) for p in pdivs: if not p in allDivisors or allDivisors.count(p) < pdivs.count(p): allDivisors.append(p) mult = 1 for i in allDivisors: mult *= i print "result : " + str(mult)
""" The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? """ import math import common num = 600851475143 primes = common.getPrimes(math.sqrt(num)) primeFactors = [p for p in primes if num % p == 0] print "result : " + str(max(primeFactors))
#!/usr/bin/env python from common import getPrimes from time import time #get all primes below 1,000,000 primes = getPrimes(1000000) maxCount = 0 maxPrime = -1 start = time() #prime to look at for p in primes: #index variable i = 0 #count of how many primes used count = 0 #sum of the primes so far total = 0 #Start index value start = 0 tooHigh = p/2 #keep in loop until the prime is half of the one being looked at while primes[i] <= tooHigh: #add currentPrime to total & increment count
#!/usr/bin/env python from common import getPrimes from math import sqrt # get primes under 100,000 maxNum = 100000 primes = getPrimes(maxNum) primes.insert(0, 1) # index of the maxPrime maxPrime = 0 # starting number num = 9 while num < maxNum: # up the maxPrime while primes[maxPrime] < num: maxPrime += 1 # if the number is prime if num == primes[maxPrime]: num += 2 continue found = False # go through all primes looking for a number that fits for i in range(maxPrime - 1): if str(sqrt((num - primes[i]) / 2)).split(".")[1] == "0": found = True break # if no number found
#-*- coding:utf-8 -*- """ The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953. Which prime, below one-million, can be written as the sum of the most consecutive primes? """ import common import math primes = common.getPrimes(10**6) primeset = set(common.getPrimes(10**6)) result, resCount = 0, 0 for i in range(0,len(primes)): num = 0 count = 0 for j in primes[i:]: count+=1 num +=j if num < 10**6 and num in primeset and resCount<count: resCount = count result = num if num>10**6: