Ejemplo n.º 1
0
def all_factors(n: int) -> Set[int]:
    from Euler0003.primes import prime_factors
    from Euler0008.product import product
    pfs: List[int] = prime_factors(n)
    return {
        product(pfc)
        for c in range(max(1, len(pfs))) for pfc in combinations(pfs, c)
    }
Ejemplo n.º 2
0
 def factor_count(n: int) -> int:
     nonlocal ps
     pfs = prime_factors(n, ps)
     ps = sorted(list(set(ps).union(set(pfs))))
     counts = Counter(pfs)
     return product([1 + counts[k] for k in counts.keys()])
Ejemplo n.º 3
0
def fact(n: int) -> int:
    from Euler0008.product import product
    return product(range(1, n + 1))
Ejemplo n.º 4
0
def all_factors(n: int) -> Set[int]:
    from Euler0003.primes import prime_factors
    from Euler0008.product import product
    pfs: List[int] = prime_factors(n)
    return {product(pfc) for c in range(max(1, len(pfs)))
            for pfc in combinations(pfs, c)}
Ejemplo n.º 5
0
def grid_product(grid, xys: Iterator[Tuple[int, int]]) -> Optional[int]:
    from Euler0008.product import product
    try:
        return product([grid[x, y] for x, y in xys])
    except IndexError:
        return None
Ejemplo n.º 6
0
from typing import Tuple, List


def is_triplet(x: int, y: int, z: int) -> bool:
    [a, b, c] = sorted([x, y, z])
    return a**2 + b**2 == c**2


def triplets_summing_to(x: int) -> List[Tuple[int, ...]]:
    return [tuple(sorted([a, b, x - (a + b)])) for a in range(1, x + 1) for b
            in range(a, x + 1 - a) if is_triplet(a, b, x - (a + b))]


if __name__ == '__main__':
    import os
    import sys
    sys.path.append(os.path.dirname(os.pardir))
    from Euler0008.product import product
    '''
    A Pythagorean triplet is a set of three natural numbers, a < b < c,
    for which,
    a^2 + b^2 = c^2

    For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.

    There exists exactly one Pythagorean triplet for which a + b + c = 1000.
    Find the product abc.
    '''
    print(product(triplets_summing_to(1000)[0]))
Ejemplo n.º 7
0
def test_factor_product(n: int):
    from Euler0008.product import product
    assert n == product(prime_factors(n))
Ejemplo n.º 8
0
def lcm(support: Sequence[int]) -> int:
    from Euler0003.primes import prime_factors
    from Euler0008.product import product
    factors: List[Counter] = [Counter(prime_factors(x)) for x in support]
    common_factors: Counter = reduce(lambda a, b: a | b, factors)
    return product([k**common_factors[k] for k in common_factors.keys()])
Ejemplo n.º 9
0
 def factor_count(n: int) -> int:
     nonlocal ps
     pfs = prime_factors(n, ps)
     ps = sorted(list(set(ps).union(set(pfs))))
     counts = Counter(pfs)
     return product([1 + counts[k] for k in counts.keys()])