Using a for loop to count the digits of pi
Show older comments
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.
- 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 π. - 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
Walter Roberson
on 4 Oct 2023
I recommend that you semi-colon to the end of the sum() lines, as you do not need the intermediate values displayed.
Which part do you think is incorrect? You can use histcounts to count frequencies of vector entities.
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
histcount50 = histcounts(pi_digits(1:50))
histcount100 = histcounts(pi_digits)
isequal(f50, histcount50)
isequal(f100, histcount100)
Sam Chak
on 4 Oct 2023
@Martin (OP): "In this problem we are going to see if the digits of π do indeed appear to be random."
Sounds interesting. I believe this is still an open problem in Mathematics and Algorithmics. I heard many of my math friends say its decimal expansion goes on forever without repeating, but no one has actually tested it 'forever'.
Do you think the first 100 digits of π are sufficient to demonstrate the randomness?
Martin
on 4 Oct 2023
Martin
on 4 Oct 2023
William Rose
on 4 Oct 2023
Edited: William Rose
on 4 Oct 2023
[edit: I attached the text file of a million decimal digits (plus the initial "3.") from here, in case you are having trouble downloading it.]
if you're interested, you can download pi to a million decimal digits here. The site already reports the histogram of digits, so you can check your algorithm. Have fun!
Walter Roberson
on 4 Oct 2023
I was not able to follow the ftp link, at least not with Firefox. I will try with a command line tool.
Martin
on 4 Oct 2023
Walter Roberson
on 4 Oct 2023
Nope, cannot resolve the site name wuarchive.wustl.edu
I think wuarchive-ftpd was defunct.
Can find some archives here:
Sam Chak
on 4 Oct 2023
Can also find something interesing here:
Up to 22.4 Trillion Digits of π!
Dyuman Joshi
on 4 Oct 2023
@Sam, but the discussion here is for pi, not the factorial of pi :P
Sam Chak
on 5 Oct 2023
@Dyuman Joshi, I am "irrational", pun intended.

Answers (2)
Daniel
on 6 Oct 2023
1 vote
MA207?
need to divide f50 by 50 and f100 by 100.
apparently it wants the answer to be in decimal.
2 Comments
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.
Hi @Martin
Please try comparing the histogram with yours.
num2str(pi, 1000);
digits(100); % show 100 digits of π
numPi = vpa(pi)
c = char(numPi);
% Find the decimal point:
pos = strfind(c, '.')
% 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
Hi @Martin
This code converts the number π into an array of integers and counts the frequency of the digits. The code also verifies the results obtained by you. Please review the question to clarify whether you want to count the frequency of the digits in the array of integers or only the decimal digits. Additionally, if I use digits(100), the last digit will be rounded up.
num2str(pi, 1000);
digits(102); % show 102 digits of π
numPi = vpa(pi)
c = char(numPi);
c(2) = [] % remove decimal point
% Count the digit from array of integers: 3, 1, 4, ...
d = arrayfun(@str2num, c(1:end-2)) % exclude the 101st and 102nd position in the sequence
histcount100 = histcounts(d)
% Plot the histogram
histogram(d, 10);
xt = linspace(0.5, 8.5, 10);
xticks(xt);
xticklabels({'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'})
xlabel('Digit')
ylabel('Frequency')
title('Distribution of the digits of \pi');
Martin
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')
nnz(T(1:100)=='6')
nnz(T(1:100)=='9')
Categories
Find more on Creating and Concatenating Matrices 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!



