示例#1
0
def sum_primes_below(n):
    """Compute the sum of primes below the given threshold.

    >>> sum_primes_below(10)
    17
    >>> sum_primes_below(2000000)
    142913828922
    """

    return sum(A3.prime_generator(n))
示例#2
0
def find_nth_prime(n):
    """Compute the nth prime number using an unbounded prime generator.

    >>> find_nth_prime(6)
    13
    >>> find_nth_prime(101)
    547
    >>> find_nth_prime(10001)
    104743
    """

    primes = A3.prime_generator()

    for x in range(n):
        prime = primes.next()

    return prime
示例#3
0
def computed_with_prime_powers(n):
    """A faster method observes that for any prime number, a simple power of
    that prime number will be the smallest number with the that factorization
    count. For example, 16 is the largest power of 2 less than or equal to 20.

    >>> computed_with_prime_powers(20)
    232792560
    >>> computed_with_prime_powers(40)
    5342931457063200
    >>> computed_with_prime_powers(80)
    32433859254793982911622772305630400L
    >>> computed_with_prime_powers(160)
    117671955487901874837890815641362681946988303003141220897970719568000L
    """

    product = 1
    for prime in A3.prime_generator(n):
        count = int(math.log(n, prime))
        product *= prime ** count

    return product