%%     C. Bishop  Homework 0, Summing digits of $\pi$, MAT 331, Fall 2018 

%% Part 1:  What is the sum of the first 10,000 digits of pi?
% We use vpa to compute N=10000 digits of pi and convert this 
% to a character string y with char. When then let x hold the 
% numerical value of the kth digit, being careful to omit the
% second character, which is the decimal point. Then t is defined 
% as the sum of all the digits.

   N=10000;
   y=char(vpa(pi,N));
   x(1)=str2num(y(1));
   y=char(vpa(pi,N));
   for k=2:N
   x(k)=str2num(y(k+1));
   end
   t=sum(x)
%%
% This creates a single character string y that is 10,001 characters long
% (there is an extra character for the decimal point). We  then convert
% this to a string of integers x, remembering to skip the decimal place.
% The answer to the first part  is  t= 44890 .  

%% Part 2: If the digits of $\pi$ are uniformly random in { 0,..., 9}, what would we expect the sum to be? How far apart are the actual and expected sums?
% If the digits were uniformly random in \{0,1,\dots,9\} then
% the average size of a digit would be a = (0+1+\dots+9)/10 = 4.5,
% and the sum of 10,000 such digits would be 45,000. 
% The difference between this and the actual sum is 45000-44890=110.

%% Part 3: For 1  <= k  <= N , plot the difference between the expected and the actual sum of the first k digits of pi.  Do you see any pattern?
% To compute the sum of the first k digits of \pi we can either use 
% a loop as follows, 

   c(1)=x(1);
   for k=2:N
      c(k)=c(k-1)+x(k); 
   end 

%%
% or a built-in MATLAB command that does the  same thing:

   d=cumsum(x);
%%
% We can check that these both give the same list of numbers,
% by subtracting them and taking the maximum of the absolute value:

   error = max(abs(c-d))

%%
% To plot the difference between c  and 4.5 k we use:

   figure;          %  creates an empty figure window
   hold on;         %  tells MATLAB keep all the following features
   grid on;         %  creates a grid
   title('The difference between acutal and expected  sums')  
   f=c-4.5*[1:N];   % defines difference between actual sum and expected sum
   plot(f);         % plots f, default color is blue
   xlabel('k');     % puts label on horizontal axis
   ylabel('f(k)');  % puts label on vertical axis


% No pattern is apparent. Indeed, the behavior looks pretty random.


%% Part 4: Draw a histogram of how many times each digit is used.  Which digit is used the most and which is used the least?  How many times are they used?
%  From part (1) the vector x contains the first 10,000 digits of $\pi$.
%  The command hist(x,n) will plot a histogram of x, i.e., divide the
%  range of x into n equal intervals and plot a chart showing how many
%  values of x lie in each bin.  Since x only takes the 
%  10 integer values 0,..,9, if we set n=10, then each digit will be
%  placed in its own bin. 

  figure;
  hold on;
  grid on;
  title('Histogram of the first 10,000 digits of \pi');
  hist(x,10);

%%
% The command h = hist(x,n) does not draw the histogram, but 
% computes the number of values in each bin and puts these values
% in the n-vector h. The last command creates a 2-column vector
% such that the first column lists the digits 0-9 and the second 
% lists h, the number of times that digit occurs.  

  h=hist(x,10);
  [0:9;h]'

%%
% From the values of h, we see that the most used digit is 5 (it 
% occurs 1046 times) and the least used digit is 8 (used 948 times).

