Problem 44309. Pi Digit Probability
Solution Stats
Problem Comments
-
19 Comments
testsuite not working, please change "assert(isequal(pidigit(N,n)-y_correct<0.0001))" to "assert(abs(pidigit(N,n)-y_correct)<0.0001)"
Thanks, I have made the change
Still missing a ")" in your assert statement
Thanks, I have add ')'
In Solution 1301292 I am finding the same thing as Yona reported (see comment below). "Digits" should mean all digits, including the leading 3. That is the definition required to pass Test 1. But to pass Test 2 and Test 5, it is required to ignore the leading 3. Or else some people have misread the problem statement, which says that the number of digits to inspect is not N, but rather N–1. (Because we're trying to predict the N'th digit.) I count only 15 occurrences of "6" in the first 200 digits, when the leading "3" is included (as it should be).
I can not find a solution without using the symbolic math toolbox ;(
David, Maurício, Yona and mhartma3 check your solutions. They should pass all the test suit now.
Thanks, Mehmet OZC. I suppose that numerically my Solution 1301292 should now pass, but it seems it is failing the new assertion that uses regex to parse the M-file. I guess it is looking for hard-coded solutions that might characteristically contain any of the numbers [101,201,202,203,1001], which are important parameters in your Test Suite. However, inspection of pi reveals that the sequence "101" appears at the 852nd decimal place, "201" appears at the 244th decimal place, etc. So the new assertion might be creating unexpected side-effects.
@David it is somewhat funny but that solution is not failing because of those partial matches in the pi sequence (the regexp command used will disregard partial matches) but rather because of the explicit 1001 value in your commented line (reading "First 1001 digits of pi")
Ah, thanks for the clarification, Alfonso. So it was an unintended side-effect, but not the one I had supposed. That also explains why it didn't disrupt most other solutions.
It is a shame vpa does not work, this could lead to some more interesting challenges
......
You have to provide the number pi as a string as input. This is horrible, however, this is needed, because the symbolic toolbox is not working. Therefore, here is the number:
initPI = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989';
I think the answers to the second and the last tests are wrong, they should be 0.08 and .105 respectively
The question is boring
Pretty sure that there is an error in the test bench or the internet has the PI digits wrong on some sites
- result for the last case must be 0.106.
- if vpa is not accepted this is a silly question.
% I used Machin's Formula: pi = 4[4arctan(1/5) - arctan(1/239)] because it was used for speed. But it will not go no more than 16 digits. How can I go beyond 16 digits?
function [PI] = pidigit(N,n)
A = 0;
F = 10^300;
% Machin's Formula: pi = 4[4arctan(1/5) - arctan(1/239)]
x1 = 1/5;
x2 = 1/239;
for K = 0:1000000000
A1 = 4*((-1)^K*x1^(2*K+1)/(2*K+1));
A2 = (-1)^K*x2^(2*K+1)/(2*K+1);
A = A + 4*(A1 - A2)*F;
end
E = abs(pi*F-A)/(pi*F);
PI = sprintf('%.f',A) -'0';
sum(PI(1:N-1) == n)
sum(PI(1:N-1) == n)/(N-1)
end
can't use vpa, so thats annoying. just find online the first bunch of digits of pi, paste them into a character variable, and work from there. I found the digits here :https://www.angio.net/pi/digits/10000.txt
Solution Comments
-
1 Comment
I used a .txt file for the first 1 million digits of pi hosted in a repository I made. Then applied the following commands:
url = "https://raw.githubusercontent.com/GerardoDomra/pi-digits/main/pi-digits.txt?_sm_au_=iVVqvQZNWHMHssHP7G1BvKtGW0FVq";
piApprox = webread(url, weboptions('ContentType','text'));
piDigits = split(erase(string(piApprox),'.'),'');
piDigits = piDigits(2:end - 1);
The variable piDigits is a char array that contains every digit as an element. Then you can just turn each digit into a number using the function str2double( ).
-
2 Comments
What is the error with code as a solution?
m = 'n';
Pi='3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275900994657640789512694683983525957098258226205224894077267194782684826014769909026401363944374553050682034962524517493996514314298091906592509372216964615157098583874105978859597729754989301617539284681382686838689427741559918559252459539594310499725246808459872736446958486538367362226260991246080512438843904512441365497627807977156914359977001296160894416948685558484063534220722258284886481584560285060168427394522674676788952521385225499546667278239864565961163548862305774564980355936345681743241125150760694794510965960940252288797108931456691368672287489405601015033086179286809208747609178249385890097149096759852613655497818931297848216829989487226588048575640142704775551323796414515237462343645428584447952658678210511413547357395231134271661021359695362314429524849371871101457654035902799344037420073105785390621983874478084784896833214457138687519435064302184531910484810053706146806749192781911979399520614196634287544406437451237181921799983910159195618146751426912397489409071864942319615679452080951465502252316038819301420937621378559566389377870830390697920773467221825625996615014215030680384477345492026054146659252014974428507325186660021324340881907104863317346496514539057962685610055081066587969981635747363840525714591028970641401109712062804390397595156771577004203378699360072305587631763594218731251471205329281918261861258673215791984148488291644706095752706957220917567116722910981690915280173506712748583222871835209353965725121083579151369882091444210067510334671103141267111369908658516398315019701651511685171437657618351556508849099898599823873455283316355076479185358932261854896321329330898570642046752590709154814165498594616371802709819943099244889575712828905923233260972997120844335732654893823911932597463667305836041428138830320382490375898524374417029132765618093773444030707469211201913020330380197621101100449293215160842444859637669838952286847831235526582131449576857262433441893039686426243410773226978028073189154411010446823252716201052652272111660396665573092547110557853763466820653109896526918620564769312570586356620185581007293606598764861179104533488503461136576867532494416680396265797877185560845529654126654085306143444318586769751456614068007002378776591344017127494704205622305389945613140711270004078547332699390814546646458807972708266830634328587856983052358089330657574067954571637752542021149557615814002501262285941302164715509792592309907965473761255176567513575178296664547791745011299614890304639947132962107340437518957359614589019389713111790429782856475032031986915140287080859904801094121472213179476477726224142548545403321571853061422881375850430633217518297986622371721591607716692547487389866549494501146540628433663937900397692656721463853067360965712091807638327166416274888800786925602902284721040317211860820419000422966171196377921337575114959501566049631862947265473642523081770367515906735023507283540567040386743513622224771589150495309844489333096340878076932599397805419341447377441842631298608099888687413260472';
b = Pi(1:N);
x = length(strfind(b, m));
y=round(x/(N-1),4);
Not m = 'n', but:
m = num2str(n);
-
1 Comment
It does not accept VPA and digits function? Something wrong with online debugging?
-
1 Comment
at least the first 20 SOL are fun ;P
-
1 Comment
quite silly as we cannot use the vpa function.
-
1 Comment
how do I solve this without using either the Pi value string or symbolic toolbox?
-
2 Comments
why do I always get this error message:
Undefined function 'vpa' for input arguments of type 'double'.
When running the code in my Matlab version, it works fine
to use vpa function you must have symbolic math toolbox. We cannot use the toolbox on cody.
-
3 Comments
I think the answer to test suite 3 should be 0.0746 rather than 0.0796.
Kyle, test suite is correct. your code includes decimal point. check your solution
Ah, you're correct. Thanks!
-
1 Comment
My code passes all tests but the last... I'm getting 0.1050 as a result instead of 0.1060.
-
1 Comment
can we just do not paste pi and truncate? try something different.
-
4 Comments
Shouldn't the correct answer for test 2 be 0.08? There are be 16 occurrences of the digit 6 in the first 200 digits. 16/200 = 0.08.
Sorry for that, I have corrected the mistake.
I think this correction is not good. the 201th digit is 6 so only 15 until 200 (when the first digit is 3 and not only the decimal part)
same problem in test 5. need to be 105. (see solution 1292755 and 1292769)
I agree with Yona that the "corrected" solution is now wrong. There are only 15 occurrences of the digit 6 in the first 200 digits. 15/200 = 0.0750. Similarly, the solution to test 5 should be 105/1000 = 0.1050.
-
1 Comment
Not a particularly cool solution to be proud of, but a string literal would be neither too elegant...
Problem Recent Solvers471
Suggested Problems
-
637 Solvers
-
684 Solvers
-
4517 Solvers
-
Pernicious Anniversary Problem
786 Solvers
-
Given a square and a circle, please decide whether the square covers more area.
661 Solvers
More from this Author92
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!