Problem 953. Pi Estimate 1
Solution Stats
Problem Comments
-
11 Comments
please update the link to the document, cant find it anymore
The link is broken, but you can solve this problem with the Leibniz formula for π.
The principle is described here: https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
@Jordan Wilkerson change the number format and round to significant digits shown in test code
@Paul Derwin
Oh right, duh. I should've thought of that haha. That worked! I deleted my last comment since it basically was a solution after all. Thanks for your help!
Leibniz formula, precision 1e-6.
please update the link.
The description has been updated per the link provided in a prior comment.
function [estimate] = pi_est1(nMax)
estimate=0;
for i=1:(nMax-1)
m=2*i+1;
if mod(i,2)~=0
estimate=(1/m)+estimate;
else
estimate=-(1/m)+estimate;
end
end
estimate=(1-estimate)*4;
estimate=round(estimate,6);
end
I believe the task statement should be modified by specifying that the estimate should be rounded. Otherwise, the test suite can be edited accordingly.
The problem description has been updated to specify that rounding is required.
Solution Comments
-
1 Comment
My code worked for tests 1 and 3 perfectly well, but kept getting 3.140592000000000 instead of 3.140593000000000 for test 2.
-
1 Comment
stupid
-
2 Comments
I tried my answer on Matlab and It is correct but when I wrote it here it shows me wrong.
try round to 6 digit
Problem Recent Solvers517
Suggested Problems
-
13284 Solvers
-
6461 Solvers
-
Program an exclusive OR operation with logical operators
665 Solvers
-
723 Solvers
-
8511 Solvers
More from this Author4
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!