Example #1
0
def smallest_divisible(n):
    """
    Finds the smallest positive number that is evenly divisible by all the
    numbers from 1 to n.
    """
    factors_list = []
    for i in range(1, n):
        ls = prime_factorization(i)
        for item in factors_list:
            if item in ls:
                ls.remove(item)
        factors_list.extend(ls)
    product = 1
    for x in factors_list:
        product *= x
    
    return product
     1: 1
     3: 1,3
     6: 1,2,3,6
    10: 1,2,5,10
    15: 1,3,5,15
    21: 1,3,7,21
    28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred
divisors?
'''

from prime import prime_sieve, prime_factorization
from operator import mul

primes = prime_sieve(int(500 ** .5) + 1)

n = 0
while True:
    n += 1
    triangle = n * (n + 1) / 2

    factors = prime_factorization(triangle, primes)
    if factors:
        divisors = reduce(mul, (c + 1 for (factor, c) in factors))
        if divisors > 500:
            print triangle
            break
Example #3
0
def number_of_divisors(n):
    return reduce(operator.mul,
                  [v + 1 for v in prime_factorization(n).values()], 1)
     1: 1
     3: 1,3
     6: 1,2,3,6
    10: 1,2,5,10
    15: 1,3,5,15
    21: 1,3,7,21
    28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred
divisors?
'''

from prime import prime_sieve, prime_factorization
from operator import mul

primes = prime_sieve(int(500**.5) + 1)

n = 0
while True:
    n += 1
    triangle = n * (n + 1) / 2

    factors = prime_factorization(triangle, primes)
    if factors:
        divisors = reduce(mul, (c + 1 for (factor, c) in factors))
        if divisors > 500:
            print triangle
            break
    14 = 2 × 7
    15 = 3 × 5

The first three consecutive numbers to have three distinct prime factors are:

    644 = 2² × 7 × 23
    645 = 3 × 5 × 43
    646 = 2 × 17 × 19.

Find the first four consecutive integers to have four distinct prime factors. What is the first of these numbers?
'''

from prime import prime_sieve, prime_factorization

primes = prime_sieve(10**4)

n = 1000
c = []

while True:
    n = n + 1
    if len(prime_factorization(n)) == 4:
        c.append(n)
    else:
        del c[:]

    if len(c) == 4:
        print c
        break
Example #6
0
def distinct_primes(x, n):
    return len(prime_factorization(x).keys()) == n
Example #7
0
def lcm(a,b): return (a*b) / gcd(a,b)
cin, cout = stdin.readline, stdout.write
START = time()
MOD = 10**9 + 7
LIMIT = 20000

def times(my_counter, time):
    new_counter = Counter(my_counter)
    for prime in new_counter:
        new_counter[prime]*=time
    return new_counter

f_f = [0 for i in xrange(LIMIT+1)]
f_f[0] = Counter()
for i in xrange(1, LIMIT+1):
    f_f[i] = prime_factorization(i) + f_f[i-1]

denominator = [0]*(LIMIT+1)
denominator[0] = Counter()
for i in xrange(1, LIMIT+1):
    denominator[i] = denominator[i-1] +  times(f_f[i], 2)

cout('Done preprocessing\n')
def d_of_n(num):
    numerator = times(f_f[num], num+1)
    my_number = numerator - denominator[num]
    d_of_n_ = 1
    for prime, count in my_number.items():
        num = (pow(prime, count+1, MOD) - 1 +MOD)%MOD
        prime_pow_sum =  (num* pow(prime-1, MOD-2, MOD))%MOD
        d_of_n_ = (prime_pow_sum * d_of_n_)%MOD
Example #8
0
def distinct_primes(x, n):
    return len(prime_factorization(x).keys()) == n
    14 = 2 × 7
    15 = 3 × 5

The first three consecutive numbers to have three distinct prime factors are:

    644 = 2² × 7 × 23
    645 = 3 × 5 × 43
    646 = 2 × 17 × 19.

Find the first four consecutive integers to have four distinct prime factors. What is the first of these numbers?
'''

from prime import prime_sieve, prime_factorization

primes = prime_sieve(10 ** 4)

n = 1000
c = []

while True:
    n = n + 1
    if len(prime_factorization(n)) == 4:
        c.append(n)
    else:
        del c[:]

    if len(c) == 4:
        print c
        break

Example #10
0
def number_of_divisors(n):
    return reduce(operator.mul, [v + 1 for v in prime_factorization(n).values()], 1)