A positive integer, $n$, is factorised into prime factors. We define $f(n)$ to be the product when each prime factor is replaced with $2$. In addition we define $f(1)=1$.
For example, $90 = 2\times 3\times 3\times 5$, then replacing the primes, $2\times 2\times 2\times 2 = 16$, hence $f(90) = 16$.
Let $\displaystyle S(N)=\sum_{n=1}^{N} f(n)$. You are given $S(10^8)=9613563919$.
Find $S(10^{14})$.
To efficiently calculate the sum $\displaystyle S(N) = \sum_{n=1}^{N} f(n)$ for large values of $N$, you can use the principle of inclusion-exclusion along with prime factorization techniques.
Here’s how you can approach this problem:
1- Generate a list of prime numbers up to $\sqrt{N}$ using the Sieve of Eratosthenes algorithm.
2- Use a helper function to calculate the prime factorization of a given number. This function should return the list of prime factors along with their multiplicities.
3- Implement a function to calculate $f(n)$ using the prime factorization. For each prime factor $p$ with multiplicity $k$ in the factorization, replace $p^k$ with $2^k$.
4- Implement the inclusion-exclusion algorithm to calculate the sum $\displaystyle S(N)$:
a- Initialize the sum to zero.
b- For each subset of primes from the list generated in step 1:
1. Calculate the product of $f(n)$ for all numbers that have only the primes in the current subset in their prime factorization.
2. If the number of primes in the subset is odd, subtract the product from the sum; otherwise, add it.
5- The calculated sum will be the value of $\displaystyle S(N)$.
Here’s a Python code that implements the above steps:
,,,,,,,
def sieve_of_eratosthenes(limit):
primes = []
sieve = [True] * (limit + 1)
for num in range(2, int(limit ** 0.5) + 1):
if sieve[num]:
primes.append(num)
for multiple in range(num * num, limit + 1, num):
sieve[multiple] = False
return primes
def prime_factorization(n, primes):
factors = []
for prime in primes:
if prime * prime > n:
break
count = 0
while n % prime == 0:
n //= prime
count += 1
if count > 0:
factors.append((prime, count))
if n > 1:
factors.append((n, 1))
return factors
def f(n):
prime_factors = prime_factorization(n, primes)
result = 1
for prime, count in prime_factors:
result *= 2 ** count
return result
def calculate_S(N):
primes = sieve_of_eratosthenes(int(N ** 0.5))
total_sum = 0
for subset_size in range(1, len(primes) + 1):
for subset in itertools.combinations(primes, subset_size):
subset_product = 1
for prime in subset:
subset_product *= prime
if subset_size % 2 == 1:
total_sum -= f(N // subset_product)
else:
total_sum += f(N // subset_product)
return total_sum
def main():
N = 10 ** 14
result = calculate_S(N)
print(result)
if __name__ == “__main__”:
main()
More Answers:
Factors of Two in Binomial CoefficientsTotal Inversion Count of Divided Sequences
$3$-Like Numbers