
\documentclass[12pt]{amsart}
%\usepackage{amssymb,amsthm,verbatim,amsfonts,amscd,graphicx}
\usepackage{amssymb,amsthm,verbatim,amsfonts,amscd,psfig}


\def\integers{{\Bbb Z}}
\def\complex{{\Bbb C}}
\def\reals{{\Bbb R}}


\addtolength{\textwidth}{1.0in}
%\addtolength{\leftmargin}{-.5in}
\hoffset -.5in


\begin{document}


\begin{center}
{\bf  
  MAT 331 Fall 2017, Homework  0 \\
   Summing the digits of $\pi$\\
}
\end{center}


\vskip.2in 

{\bf QUESTION:}
\begin{enumerate}
\item 
What is the sum of the first $N= 10,000$ digits of $\pi$?
For example, the sum of the first three digits is 
$3+1+4 = 8$.
\vskip.2in 


\item 
If the digits of $\pi$ are uniformly random in $\{0,1, \dots,9\}$
what  do we expect the  sum to be? How far apart are the 
actual and expected sums?
\vskip.2in 


\item 
For $1 \leq k \leq N$, plot  the difference  between the expected
and the actual sum of the first $k$ digits of $\pi$.  Do you 
see any pattern?

\vskip.2in 
\item 
Draw a histogram of how many times each digit is used.
 Which digit is used the most and which is used the least?
\end{enumerate}

\vskip.5in

{\bf SOLUTION:}

(1) 
The first step is to form a $N$-long vector whose entries are 
single digits integers correspodning to the digits of $\pi$. 
The code I used is 

\begin{verbatim}
   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)
\end{verbatim} 

This creates a single character string \verb+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 \verb+x+,
 remembering to skip the decimal place.
The answer to the first part  is \verb+ t= 44890 +.


(2) 
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$.


(3) 
To compute the sum of the first $k$ digits of $\pi$ we 
can either use a loop 
\begin{verbatim}
   c(1)=x(1);
   for k=2:N
      c(k)=c(k-1)+x(k); 
   end 
\end{verbatim} 

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

\begin{verbatim}
   c=cumsum(x);
\end{verbatim} 

We want to plot the difference between this and $4.5 k$:

\begin{verbatim}
   figure;
   hold on;
   grid on;
   title('The difference between acutal and expected  sums')
   d=c-4.5*[1:N]
   plot(d);
\end{verbatim}

After the figure appeared on the screen I used the ``File''
and ``Save As''buttons  to save the figure as \verb+pi_sum.pdf+ 
in PDF format. The resulting picture is shown in 
Figure \ref{pi_sum}.

\begin{figure}[htb]
\centerline{
\psfig{figure=pi_sum.eps,width=4in}
%\includegraphics[width=3in]{pi_sum.pdf} 
}
\caption{ \label{pi_sum}
     This plot the difference between the sum of the 
     first $k$ digits of $\pi$ and the expected size of 
     the sum, which is $(4.5)k$. At least to the naked eye, 
     the difference seems random and no pattern in apparent. 
}
\end{figure}

(4) 
We plot the histgram using 
\verb+ hist(x,10) +  which produces the figure shown 
on the left of Figure \ref{pi_hist}. The right side 
of the figure is an enlargement of the top of the 
histogram  (made using the magnifing glass button on the 
top of the figure window). The most common digit is
and the least common digit is 8.

\begin{figure}[htb]
\centerline{
\psfig{figure=pi_hist1.eps,width=3in}
\hspace{.2in}
\psfig{figure=pi_hist2.eps,width=3in}
}
\caption{ \label{pi_hist}
      On the left is the histogram of the first 10,000 
    digits of $\pi$. On the right is an enlargement of
    the top of the histogram that shows that $5$ is the 
    most common digit and $8$ is the least common.
}
\end{figure}

% How to include a figure in PDf format
\begin{comment} %##################### 
\begin{figure}[htb]
\centerline{
\includegraphics[width=3in]{pi_sum.eps}
}
\caption{ \label{pi_sum}
     This plot the difference between the sum of the 
     first $k$ digits of $\pi$ and the expected size of 
     the sum, which is $(4.5)k$. At least to the naked eye, 
     the difference seems random and no pattern in apparent. 
}
\end{figure}
\end{comment} %#########################


    



\end{document}


