def get_prime_v2(primePosition): """ Sieve the numbers. >>> get_prime_v2(6) 13 """ maxCandidate = 64 minPrime = 3 primes = [2] nonPrimes = set() while primePosition > len(primes): maxCandidate *= 2 for factor in range(2, 1 + ceil(sqrt(maxCandidate))): if Prime.is_prime(factor): nonPrime = factor * 2 else: nonPrime = factor while nonPrime <= maxCandidate: nonPrimes.add(nonPrime) nonPrime += factor primes.extend([n for n in range(minPrime, maxCandidate+1, 2) if n not in nonPrimes]) minPrime = maxCandidate + 1 return primes[primePosition - 1]
def larget_prime_factor_v1(n): """ >>> larget_prime_factor_v1(6) 3 >>> larget_prime_factor_v1(8) 2 >>> larget_prime_factor_v1(13195) 29 """ return max(Prime.prime_factors(n))
def least_common_multiple_v1(maxDivisor): """ >>> least_common_multiple_v1(10) 2520 >>> least_common_multiple_v1(20) 232792560 """ primeFactorsCounter = Counter() for divisor in range(1, maxDivisor+1): # Get the union of the currently-used prime factors and the prime factors of divisor. primeFactorsCounter |= Counter(Prime.prime_factors(divisor)) return product(primeFactorsCounter.elements())
def get_prime_v1(primePosition): """ >>> get_prime_v1(6) 13 """ n = 3 primeNumbersSeen = 1 # 2 is prime while True: if Prime.is_prime(n): primeNumbersSeen += 1 if primeNumbersSeen == primePosition: return n n += 2