Using a for loop to count the digits of pi

8 views (last 30 days)
Martin
Martin on 4 Oct 2023
Commented: Sam Chak on 6 Oct 2023
If the digits of π were random, then we would expect each of the integers to occur with approximately equal frequency in the decimal representation of π. In this problem we are going to see if the digits of π do indeed appear to be random.
The first line of your script stores the first 100 digits of π in the vector pi_digits.
  1. Create a vector f50 such that f50(i) is equal to the frequency with which the integer i-1 appears among the first 50 digits of π.
  2. Replicate it for f100.
I have the following code:
pi_digits=[3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0 2 8 8 4 1 9 7 1 6 9 3 9 9 3 7 5 1 0 5 8 2 0 9 7 4 9 4 4 5 9 2 3 0 7 8 1 6 4 0 6 2 8 6 2 0 8 9 9 8 6 2 8 0 3 4 8 2 5 3 4 2 1 1 7 0 6 7];
f50=zeros(1,10);
for i=0:9
f50(i+1)=sum(pi_digits(1:50)==i)
end
f100=zeros(1,10);
for i=0:9
f100(i+1)=sum(pi_digits(1:100)==i)
end
the output returns the correct frequency vector, yet I get an error that it is the incorrect value.
  15 Comments
Dyuman Joshi
Dyuman Joshi on 4 Oct 2023
@Sam, but the discussion here is for pi, not the factorial of pi :P

Sign in to comment.

Answers (2)

Daniel
Daniel on 6 Oct 2023
MA207?
need to divide f50 by 50 and f100 by 100.
apparently it wants the answer to be in decimal.
  2 Comments
Walter Roberson
Walter Roberson on 6 Oct 2023
Ah, the original Question does ask about "frequency" rather than about counts, so I can see why they might normalize by the number of entries.
Sam Chak
Sam Chak on 6 Oct 2023
Thank you, @Daniel. I interpreted "frequency" as the number of occurrences within a given 100-digit π number. I totally overlooked that the "frequency" in question is somehow mathematically defined in statistics.

Sign in to comment.


Sam Chak
Sam Chak on 4 Oct 2023
Please try comparing the histogram with yours.
num2str(pi, 1000);
digits(100); % show 100 digits of π
numPi = vpa(pi)
numPi = 
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
c = char(numPi);
% Find the decimal point:
pos = strfind(c, '.')
pos = 2
% Begin to count after the decimal point
d = arrayfun(@str2num, c(pos+1:end));
% Plot the histogram for comparing with your Answer in MATLAB Grader
histogram(d, 10);
xt = linspace(0.5, 8.6, 10);
xticks(xt);
xticklabels({'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'})
xlabel('Decimal Digit')
ylabel('Frequency')
title('Distribution of the digits of \pi');
  3 Comments
Martin
Martin on 4 Oct 2023
Thanks I appreciate it! After further review I am convinced there is an error in the solution on Matlab Grader.
Stephen23
Stephen23 on 4 Oct 2023
Edited: Stephen23 on 4 Oct 2023
Another approach using 1000 digits from https://pi2e.ch/blog/2017/03/10/pi-digits-download/
T = '31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989';
histogram(T(1:100)-'0',0:10)
Confirming a few random digits:
nnz(T(1:100)=='0')
ans = 8
nnz(T(1:100)=='6')
ans = 9
nnz(T(1:100)=='9')
ans = 13

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!