Esempio n. 1
0
def p3(N=600851475143):
    """P3: What is the largest prime factor of the number 600851475143?
    """
    # First attempt was to use a prime sieve first and then filter
    # the factors, but my naive prime sieve (Eratosthenes)
    # implementation ran into performance issues. This version uses
    # the Pollard-Rho-Brent algorithm -- very, very efficient.
    return max(nttools.pfactor(N))
Esempio n. 2
0
def p5(N=20):
    """What is the smallest positive number that is evenly divisible
    by all of the numbers from 1 to 20?
    """
    # Factors of smallest number evenly divisible by every number
    # from 1 to N given as follows:  from all numbers between 2 and
    # N, take the maximum multiplicity (n_i) of each factor (i).
    fcounters = [Counter(nttools.pfactor(N=n)) for n in range(2, N+1)]
    #could also have used pfactorGen(n)
    factors = Counter()
    for fc in fcounters:
        for k, v in fc.items():
            factors[k] = v if v > factors[k] else factors[k]
    return reduce(op.mul, [k**v for k, v in factors.items()], 1)