示例#1
0
#So the sum of the terms in the prime factorisation of 10C3 is 14.

#Find the sum of the terms in the prime factorisation of 20000000C15000000.
#Answer:
#7526965179680

from time import time
t = time()
#from mathplus import get_primes_by_sieve
from gen_primes import get_primes

N, M = 20000000, 15000000
M = min(M, N - M)

#primes, sieves = get_primes_by_sieve(N)
primes = get_primes(50000000)
#print time()-t


def factfact(n):
    #ret = {}
    ret = 0
    for p in primes:
        if p > n: break
        k, m = 0, n
        while m >= p:
            m //= p
            k += m
        #ret[p] = k
        ret += p * k
    return ret
示例#2
0
#How many composite integers, n < 108, have precisely two, not necessarily distinct, prime factors?

#Answer:
#17427258

from time import time
t = time()
from mathplus import isqrt, get_primes_by_sieve
from gen_primes import get_primes

#M = 30
M = 10**8

sqrtM = isqrt(M)
primes = get_primes(M // 2, odd_only=False)
#print time()-t
sieves = [0] * (sqrtM + 1)
pnext = 2
pnext_index = 0
p = 0
for i in range(sqrtM + 1):
    if i == pnext:
        pnext_index += 1
        pnext = primes[pnext_index]
        p += 1
    sieves[i] = p
#print time()-t
ss = sieves[sqrtM]
s = ss * (ss + 1) // 2
s += sum(sieves[M // p] for p in primes[ss:])
示例#3
0
#There are ten composites below thirty containing precisely two, not necessarily distinct, prime factors: 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.

#How many composite integers, n < 108, have precisely two, not necessarily distinct, prime factors?

#Answer:
	#17427258

from time import time; t=time()
from mathplus import isqrt, get_primes_by_sieve
from gen_primes import get_primes

#M = 30
M = 10**8

sqrtM = isqrt(M)
primes = get_primes(M//2, odd_only=False)
#print time()-t
sieves = [0]*(sqrtM+1)
pnext = 2
pnext_index = 0
p = 0
for i in range(sqrtM+1):
    if i == pnext:
        pnext_index += 1
        pnext = primes[pnext_index]
        p += 1
    sieves[i] = p
#print time()-t
ss = sieves[sqrtM]
s = ss*(ss+1)//2
s += sum(sieves[M//p] for p in primes[ss:])