How can I optimize and improve the accuracy of a summation involving the Mangoldt function in MATLAB?
Show older comments
K = 10^7;
% Initialize the sum
sum_value = 0;
% Loop through k from 1 to K
for k = 1:K
% Calculate the Mangoldt function for k
L_k = mangoldt(k);
% Add the term to the sum
sum_value = sum_value + L_k / k^(3);
end
disp(sum_value);
% Define the smallest_prime_factor function
function p = smallest_prime_factor(n)
for p = 2:sqrt(n)
if mod(n, p) == 0
return; % Return the first divisor
end
end
p = n; % If no divisor is found, n is prime
end
% Define the Mangoldt function
function L = mangoldt(n)
if n == 1
L = 0; % Von Mangoldt function is 0 for n = 1
else
% Find the smallest prime factor of n
p = smallest_prime_factor(n);
% Check if n is a power of p
k = log(n) / log(p);
if abs(k - round(k)) < 1e-10 % Tolerance for floating-point errors
L = log(p); % Logarithm of the prime
else
L = 0; % Not a prime power
end
end
end
7 Comments
Torsten
on 1 Jan 2025
Could you give some explanation about what you are trying to do ?
Walter Roberson
on 1 Jan 2025
It looks like another approach would be
f = factor(n);
if numel(unique(f)) == 1
L = log(f(1));
else
L = 0;
end
Fatima Majeed
on 1 Jan 2025
Edited: Walter Roberson
on 2 Jan 2025
Torsten
on 2 Jan 2025
Estimate
lambda(k) <= k
for all k.
Thus you get
sum_{k=N}^{k=Inf} Lambda(k)/k^3 <= sum_{k=N}^{k=Inf} 1/k^2
Now determine N such that the last sum is smaller than your desired precision.
You will see that N = 10^7 is far too large for reasonable accuracy.
Fatima Majeed
on 2 Jan 2025
The default for higher precision arithmetics is 32 digits, but you can enhance precision of your symbolic computations by using the "digits" function:
You didn't include your new code under
so we can't tell if it makes sense what you are doing right now.
Fatima Majeed
on 6 Jan 2025
Accepted Answer
More Answers (0)
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!