project-euler/231_prime_factorisation_of_...

43 lines
702 B
Python

#!/usr/bin/env python3
from math import ceil
def reducetwo(n):
c = 0
while not n%2:
n //= 2
c += 1
return n, c
def primes(n):
sieve = [0] * (n//2)
for p in range(3, n, 2):
if sieve[p//2] > 0:
continue
for k in range(1, n//p+1, 2):
sieve[(p*k)//2] += p + sieve[k//2] - sieve[(p*k)//2]
return sieve
n = 2*10**7
k = 15*10**6
sieve = primes(n)
print("Primed.")
s = 0
for m in range(n, n-k, -1):
if not m % 2:
m, c = reducetwo(m)
s += 2*c
s += sieve[m//2]
print("Calculated.")
for m in range(2, k+1):
if not m % 2:
m, c = reducetwo(m)
s -= 2*c
s -= sieve[m//2]
print(s)