Exemplo n.º 1
0
def numberofdivisors(n):
    if n == 1:
        return 1
    pf = prime_factorization(n)
    lookup = Counter(pf)
    numDivisors = 1
    for val in lookup.values():
        numDivisors *= (val + 1)
    return numDivisors
Exemplo n.º 2
0
def number_of_divisors(n):
    """ Calculate the number of divisors of n.

    Every integer n can be written as a product of prime factors e.g.
    n = p^a * q^b * r^c
    and the number of divisors = (a + 1)(b + 1)(c + 1)...
    """
    divisors = 1
    prime_factors = prime_factorization(n)
    for v in prime_factors.itervalues():
        divisors *= (v + 1)

    return divisors
Exemplo n.º 3
0
def divisors(n):
    """Finds all of the divisors for a given number, including 1 and itself.

    Returns a list containing all the divisors for a number"""
    if n == 1:
        yield 1
        return
    factors = list(prime_factorization(n))
    nfactors = len(factors)
    f = [0] * nfactors
    while True:
        yield reduce(lambda a, b: a*b, [factors[x][0]**f[x] for x in range(nfactors)], 1)
        i = 0
        while True:
            f[i] += 1
            if f[i] <= factors[i][1]:
                break
            f[i] = 0
            i += 1
            if i >= nfactors:
                return
Exemplo n.º 4
0
def divisors(n):
    """Finds all of the divisors for a given number, including 1 and itself.

    Returns a list containing all the divisors for a number"""
    if n == 1:
        yield 1
        return
    factors = list(prime_factorization(n))
    nfactors = len(factors)
    f = [0] * nfactors
    while True:
        yield reduce(lambda a, b: a * b,
                     [factors[x][0]**f[x] for x in range(nfactors)], 1)
        i = 0
        while True:
            f[i] += 1
            if f[i] <= factors[i][1]:
                break
            f[i] = 0
            i += 1
            if i >= nfactors:
                return
Exemplo n.º 5
0
"""The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?"""

import primes

print(primes.prime_factorization(600851475143)[-1])
Exemplo n.º 6
0
"""2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?"""
from collections import defaultdict, Counter

import primes

d = defaultdict(int)

for i in range(2, 21):
    pf = primes.prime_factorization(i)
    for k, v in Counter(pf).items():
        d[k] = max(d[k], v)

lcm = 1
for k, v in d.items():
    lcm *= pow(k, v)
print(lcm)