예제 #1
0
from sys import argv

from euler.lib import sieve


primes = list(sieve(1000))

factor_counts = {}
def num_factors(num):
    count = factor_counts.get(num)
    if not count:
        count = 1
        for prime in primes:
            if prime**2 > num:
                count *= 2
                break
            exp = 1
            while not num % prime:
                exp += 1
                num /= prime
            count *= exp
            if num == 1:
                break

    factor_counts[num] = count
    return count


if __name__ == '__main__':
    n = 2
    while True:
예제 #2
0
from itertools import ifilter
from sys import argv

from euler.lib import gcd, pollard_rho, sieve


def max_prime_factor(num, primes):
    primes = ifilter(lambda x: x <= num / 2, primes)
    for x in primes:
        if not num % x:
            return x
    return num


if __name__ == "__main__":
    n = int(argv[1])
    factors = pollard_rho(n, lambda x: (x ** 2 + 1) % n)
    if factors:
        primes = list(sieve(max(factors) / 2, ascending=False))
        print max(map(lambda x: max_prime_factor(x, primes), factors))