M  =  { {1, 2, 3},  {4, 5, 6},  {0, 8, 9}}

"Putting stuff in quotes allows you to write comments. The above is \
how you define a 3x3 matrix. Below is how to view it as a matrix. To compile \
a line, you need to press SHIFT-ENTER. Do not compile the instruction \
lines."

MatrixForm[M]

M2  =  M . M

"The above multiplies two matrices. We'll now calculate some basic \
quantities."

InvM  =  Inverse[M]

c  =  Eigenvalues[M]

"On some versions of mathematica, when you compile the above it gives \
Root[blah]. This is because the answer is 
not exact. To get an exact answer, we use the Numerical Command, N. We first \
give an example. If we type Pi, we get the exact
answer (a symbol). If we type N[Pi], it gives us a few digits. If we need \
more digits, do N[Pi,numdigitsneed]"

Pi

N[Pi]

N[Pi,  20]

c  =  N[ Eigenvalues[M],  10 ]

"Notice we now have numerical values of the eigenvalues (to 10 \
digits). These values are stored, and we can pull off entries
one at a time."

c[[1]]

N[ c[[1]] ]

"Note how we were able to store the eigenvalues in a vector, and then \
read off the components. Sometimes we need single brackets, sometimes we need \
double brackets.
Using N[ ... ] gives numerical values."

MM  =  Table[Random[],  {m, 4},  {n,  4}]

MatrixForm[MM]

"The above generates a random 4x4 matrix with entries chosen from the \
uniform distribution."

"We now load in a package, the statistical package on Continuous \
Distributions. It has a lot of nice distributions,
for example, the Gaussian distribution. we will save the Gaussian \
distribution to the name gdist."

<< Statistics`ContinuousDistributions`

gdist  =  NormalDistribution[0, 1]

NN  =  
      MatrixForm[  Table[
          Random[gdist],  {m,  4},  {n, 4} ]  ]

"This allows us to load in a statistics package and generate matrices \
with entries from a normal distribution. Watch what happens to the entries as \
we change the standard deviation."

gdist2  =  NormalDistribution[0, 10]

NN  =  
      MatrixForm[  Table[
          Random[gdist2],  {m, 4},  {n, 4} ]  ]

Det[NN]

"Notice we do NOT get the determinant. The problem is that NN is NOT \
a matrix, but a DISPLAYED matrix."

NNN  =  Table[Random[gdist2],  {m, 4},  {n, 4} ]

Det[NNN]

"To define a function, one needs to do an underscore after each \
variable, and := instead of ="

f[x_, y_, z_]  :=  Sin[x  +  y] * z^2

f[1, 2, 3]

"To get numerical values, one can use 1.0 instead of 1"

f[1.0, 2, 3]

g[n_]  :=  Prime[n]

g[2];   g[3]  
g[4]

"you can do multiple statements on one line if you separate with a \
semi-colon. Only the output of the last is displayed. Here, g[n] is the nth \
prime number."

G[x_]  :=  Sum[ n,  {n, 1, Floor[x]}]

Plot[ G[x],  {x, 1, 10}]

"You can also plot several functions at once."

Plot[ {G[x],   .5*x * (x + 1)  +  0* x*Sin[6 x]},  {x,  
        1,  25} ]

"Mathematica also supports IF and FOR statements"

Mod[12, 7]

"The above is the mod function: 12 mod 7 is 5"

h[x_]  :=  
      If[ Mod[ Floor[x],  2]  ==  1,  5,  -5]

Plot[ h[x],  {x, -5, 5}]

"If statements work by IF[Condition, Then This, Else This]. You can \
have nested IF statements"

\(h[x_]\  := \ 
      If[\ Mod[\ Floor[x], \ 3]\  == \ 0, \ 
        0, \ 
		\ \ \ \ \ \ If[\ 
          Mod[\ Floor[x], \ 3]\  == \ 1, \ 1, \ 
          2]
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\)

Plot[ h[x],  {x, 0, 9}]

Start =  57614;  Num  =  480000;  
    theModulus  =  210;
For[i  =  0,  
        i   ≤   theModulus  +  2,  i++,  
        NumMod[i ]  =  0  ];
For[k = Start,  
        k ≤  
          Start + Num,  k++, 
  NumMod[  Mod[
              Prime[k],  
              theModulus ] ]++ ];
temp  =  
      Table[ {k,  NumMod[k]},  {k, 1,  
          theModulus  -  1}]
ListPlot[ temp ]
F[x_]  :=  
        NumMod[ Mod[Floor[x],  theModulus] ];
Plot[ 
      F[x],  {x, 1, theModulus -  .000001}]
Plot[ {F[x],  (Num*1.0   +  Sqrt[1.0*Num]) / 
          EulerPhi[
            theModulus], 
        (Num*1.0   -  Sqrt[1.0*Num]) / EulerPhi[theModulus]},  {x, 1, 
        theModulus -  .000001}]

EulerPhi[210]

"The EulerPhi function gives the number of numbers relatively prime \
to n."

"Note above that graphs / plots without a ; at the end are displayed.\
"

G[4]

IntegerQ[Sqrt[21222079685]]

145678*145678

temp2  =  
        Table[ If[
            NumMod[k]  >  0,  {k,  NumMod[k]},  {k,  
              Num / EulerPhi[theModulus]}],   {k, 1,  
            theModulus  -  1}];

F[x_]  :=  
        NumMod[ Mod[Floor[x],  theModulus] ];
Plot[ {F[x],  (Num*1.0   +  Sqrt[1.0*Num]) / 
          EulerPhi[
            theModulus], 
        (Num*1.0   -  Sqrt[1.0*Num]) / EulerPhi[theModulus]},  {x, 1, 
        theModulus -  .000001}]

plot1  =  ListPlot[ temp2]

temphigh  =  
        Table[ {k,  (Num*1.0   +  Sqrt[1.0*Num]) / 
              EulerPhi[theModulus]},  {k, 1,  
            theModulus  -  1}];

templow  =   Table[ {k,  (Num*1.0   -  Sqrt[1.0*Num]) / 
              EulerPhi[theModulus]},  {k, 1,  
            theModulus  -  1}];

plot2  =  ListPlot[ temphigh ];  
    plot3  =  ListPlot[ templow]

"We now include a quick program to calculate eigenvalues of Toeplitz \
Matrices. This code can easily be 
modified for other investigations."

"First we make sure we have loaded in the packages we need."

<< Graphics`Graphics`

<< Statistics`DataManipulation`
<< Statistics`NormalDistribution`

ndist  =  NormalDistribution[0, 1];

"In the program below, we calculate num matrices, each is n x n. we \
choose the entries randomly from the standard normal
distribution subject to being a real symmetric toeplitz matrix with zeros on \
the main diagonal. we normalize the eigenvalues by
sqrt[n]. we also have some if statements that print based on where we are -- \
the point is to know how much longer the program
will have to run. we then create a histogram."

\(\(TopMatnodiag[n_, \ num_]\  := \ 
        Module[\ {aa, \ b}, \ 
\ \ \ \ \ \ \ \ Clear[
            temp]; 
\ \ \ \ \ \ \ \ temp\  = \ {}; 
\ \ \ \ \ \ \ \ For[aa\  = \ 1, \ 
            aa\  ≤ \ 
              num, \ \(aa++\), 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Clear[a]; 
			\ \ \ \ \ \ For[
                i\  = \ 1, \ i\  ≤ \ n, \ \(i++\), \ 
                a[i, i]\  = \ 0]; \ \ \  (*\ 
                zero\ *) 
			\ \ \ \ \ \ For[
                k\  = \ 2, \ 
                k\  ≤ \ 
                  n, \ \(k++\), 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ran1\  = \ 
                  Random[ndist]\ ; 
				\ \ \ \ \ \ \ For[i = 1, \ i\  ≤ \ 
                    n\  - \ k\  + \ 
                      1, \ \(i++\), 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\ \(a[\ i, \ k\  - 1\  + \ i]\  = \ \ \(a[
                            k\ \  - \ 1\  + \ i, \ \ i]\  = \ 
                          ran1\);\)\ \ \ }\ \ 
				\ \ \ \ \ \ \ \ \ \ \ \ ];
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\ ; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ mat\  = \ 
                Array[\ a, \ {n, 
                    n}]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tempev\  = \ \ N[
                  Eigenvalues[mat]\ /\ 
                    Sqrt[1.0*
                        n]]\ ; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ If[\ Mod[aa, \ 20]\  == \ 0, \ 
                Print["We're at aa = ", \ 
                  aa]]; 
			\ \ \ \ \ \ For[b\  = \ 1, \ 
                b ≤ \ 
                  n, \ \(b++\), \ \(temp\  = \ 
                    Append[\ temp, \ 
                      tempev[\([b]\)]\ ]\ ;\)\ \ ];
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }
\ \ \ \ \ \ \ \ \ \ \ \ \ ]; 
\ 
          Histogram[\ temp\ ]; \ 
 (*\ 
            the\ second\ line\ scales\ the\ histogram\ so\ that\ we\ have\ percents\ and\ not\ total\ numbers\ *) 
Histogram[temp, \ 
            HistogramCategories\  -> \ bincat, \ 
            HistogramScale\  -> \ 
              1];
\ \ ];\)\)

TopMatnodiag[10, 100]

\("Below is some code for investigating 3x+1 and its generalizations"\)

RemoveTwo[x_]  :=  
      Module[{i},  i  =  x;  
        While[  Mod[i, 2]  ==  0,  i  =  i / 2];  
        Return[i]]

F3[x_]  :=  
        RemoveTwo[ 3*RemoveTwo[x]  +  1];
F5[x_]  :=  RemoveTwo[ 5*RemoveTwo[x]  +  1];

"We now give a code to check the first digits of the 3x+1 \
problem"

ben3[start_,  num_]  :=  
        Module[ {d, m, j, x}, 
For[m = 1,  
            m ≤  9,  m++,  
            numdig[m]  =  0]; 
x  =  
            start; 
count  =  0; 
For[
            j  =  1,  
            j  ≤  
              num,  j++, 
{
  
numdig[   Mod[  Floor[
                      x*1.0 / 10^Floor[Log [10,  x]] ],  
                    10 ] ]++; 
count  =  
                count + 1; 
x  =  
                F3[x]; 
If[x  ==  1,  
                j  =  num  +  
                    5];
}]; 
bF[
              y_]  :=  numdig[Floor[y  +   .5]] /
              count; 
ben[y_]  :=  
            Log[10,  
              1  +  (1.0/y)]; 
Plot[ {bF[y],  
              ben[y]},  {y,  .5, 
              9.5}]; 
Print["Count (the number of terms \
before we hit one) = ",  
            count]; 
Print["The number is approximately \
10^",  Floor[Log[10, 1.0 start]]];
];

ben3[
      4543534664576457754636456464754385784375987684937639847649837698374457457745, 1000]

"The following compares the terms of 5x+1 to a geometric brownian \
motion and the first digits of the
terms to Benford's law."

\(glog[x_, \ end_]\  := \ 
      Module[\ {numat, \ tempplot, \ numdig, \ lognumdig, \ m, y, \ 
          temp}, 
Clear[tempplot]; \ 
        Clear[temp]; 
For[m\  = \ 1, \ 
          m\  ≤ \ 9, \ \(m++\), \ {\ lognumdig[m]\  = \ 0; \ 
            numdig[m]\  = \ 
              0;\ }\ ]; 
tempplot\  = \ {\ {0, 
              0}\ }; 
numat\  = \ 
          RemoveTwo[x]; \ 
\ \ \ \ For[j = \ 1, \ 
            j\  ≤ \ 
              end, \ \ \(j++\), \ 
\ \ \ \ \ \ {\ \ 
If[\ 
                Mod[j, \ 1000]\  == \ 
                  0, \ \(Print["j = ", \ j, \ " x = ", \ 
                    numat, \ " and goes to ", \ 
                    F[numat]];\)\ ]; 
AppendTo[\ 
                tempplot, \ {j, \ 
                  Log[E, numat]}\ ]; 
AppendTo[\ 
                tempplot, \ \ {j, \ 
                  Log[E, \ 1.25]\ *\ j\  + \ 
                    Log[E, x]}]; 
\(numdig[\ \ \ Mod[\ \ Floor[numat*1.0\ /\ 10^Floor[Log\ [10, \ numat]]\ ], \ 
                    10\ ]\ ]++\); 
temp\  = \ 
                Exp[Log[10, \ numat]\  - \ 
                    j*Log[10, \ 
                        1.25]]; 
\(lognumdig[\ \ \ Mod[\ \ Floor[temp*1.0\ /\ 10^Floor[Log\ [10, \ temp]]\ ], \ 
                    10\ ]\ ]++\); 
numat\  = \ 
                5\ *\ numat + \ 1; \ \  (*\ 
                doing\ the\ 5  x + 
                  1\ problem\ *) 
\ }\ 
]\ 
ListPlot[\ tempplot, \ 
            PlotJoined\  -> False]\ ; 
Clear[
          tempplot]; \ 
        tempplot\  = \ {\ {0, 0}\ }; 
For[\ m\  = \ 1, \ 
          m\  ≤ \ 9, \ \(m++\), \ 
          AppendTo[
            tempplot, \ {m, \ 
              1.0*numdig[m]\ /\ end}]]; 
ListPlot[\ 
          tempplot]; 
Clear[
          tempplot]; 

bF[y_]\  := \ 
          numdig[Floor[y\  + \  .5]]\ /\ end; 
ben[
            y_]\  := \ Log[10, \ 
            1\  + \ \((1.0/y)\)]; 
Plot[\ {bF[y], \ 
            ben[y]}, \ {y,  .5, 
            9.5}]; 

\ 
        tempplot\  = \ {\ {0, 0}\ }; 
For[\ m\  = \ 1, \ 
          m\  ≤ \ 9, \ \(m++\), \ 
          AppendTo[
            tempplot, \ {m, \ 
              1.0*lognumdig[m]\ /\ end}]]; 
 (*\ \(ListPlot[
              tempplot];\)\ *) 

logbF[
            y_]\  := \ lognumdig[Floor[y\  + \  .5]]\ /\ 
            end; 
Plot[\ {logbF[y], \ ben[y]}, \ {y,  .5, 
            9.5}];
\ ]\)

glog[421, 1500]