Discovering Number Theory

Jeffrey J. Holt
University of Virginia

John W. Jones
Arizona State University

Copyright © 2001 by W. H. Freeman and Company

Introduction to Using Mathematica

Initializations

\(Off[General::"spell"]; 
Off[General::"spell1"]; \)

Mathematica Basics
Click twice on the outer bracket at the right to open the cell.

If you're reading this cell now, then you've mastered the second hardest thing about Mathematica -- figuring out how to open a closed collection of cells.

The hardest thing about Mathematica is figuring out how to have Mathematica execute a computation.  For example, to have Mathematica evaluate 17 + 34, we type

17 + 34

To perform a computation, click the mouse on the command you want to execute, hold down the Shift key, and press the normal carriage return key, labeled Enter or Return.  (Hitting only the Enter key in the numeric keypad portion of your keyboard will also work, although we do not openly advocate hitting computers.)  Try it out on the above command.  The first time that you execute a command in a Mathematica session, you will be asked "Do you want to automatically evaluate all of the initialization cells in this notebook?"  Your response to this question should always be "Yes".

If everything works the way it should, then you will get the answer 51. If you don't, ask for help.

Try having Mathematica evaluate the following:

3 + 4

13 - 47

12 23

12*23

284/16

2^143

Notice that 12 23 and 12*23 have the same effect.  Multiplication can be indicated either with the symbol * or by leaving a space between the two quantities that are to be multiplied together.

Now that you have made some modifications to this notebook, you should save your work.  Doing so is easy:  Click on Save in the File menu.  There is also a keyboard "shortcut" that you can use.  On a Windows machine, hold down the Ctrl key and type s.  On a Macintosh, hold down the "cloverleaf" key adjacent to the Space bar and type s.  (In the future, we shall use the word control to refer to the Ctrl key on a Windows machine and the "cloverleaf" key on a Macintosh.  If you are using something other than a Windows machine or a Macintosh, check with your instructor to determine your "control" key.)  You should save frequently to avoid losing your work if the machine malfunctions.

Everything in a Mathematica notebook is contained in "cells".  The scope of a given cell is indicated by the square bracket at the right.  There are a variety of different kinds of cells.  This one is called a "text" cell, and (you guessed it) is used for text.  There are other kinds of cells, such as those shown below:

This is an input cell .  (Do not execute . )

This is a text cell.

This is a subsection cell.

You can change the type of cell by clicking on the cell bracket (it will become shaded) and then by clicking on the Format menu and selecting Style.  The different types of cells allow great freedom in customizing notebooks.

To create a new cell, move the cursor until it is between cells (the cursor will change from vertical to horizontal) and then click.  A horizontal line should appear.  Then start typing; you will get an input cell, which can be changed to a different type of cell using the Style menu within the Format menu.

Create a cell below this one to compute 123!. (The "!" is not at the end of the sentence for emphasis, it's there to indicate the factorial operation.  Factorials are entered just as shown).

You can obtain decimal approximations using the command "N".  For instance, when you executed the command to compute 284/16, Mathematica reduced the fraction but returned a fraction as output.  To get a decimal approximation, run the command below:

N[284/16]

You can get more decimal places.  For instance, here are 100 digits of π:

N[Pi, 100]

To close this collection of cells, click twice on the outer cell bracket.

Adept Algebra with Mathematica
Click twice on the outer bracket at the right to open the cell.

Here is (((x  +  2 y))^(25 ))multiplied out:

Clear[x, y]Expand[(x + 2  y)^25]

If you don't hang the command Expand with the square brackets [ ] around the product, Mathematica will not multiply out the terms because it was not told to do so.  Try it.

(x + 2  y)^25

Get it?  Try changing the numbers above and rerunning.

Mathematica can also factor polynomials:

Factor[1 + x^15]

As mentioned in the preceding section, you don't have to use an asterisk between two variables when you multiply them, but you can if you want to.

\(a = 7; 
b = 4; 
a\ b\)a*b

However, remember that if you don't use an asterisk then you must put a space between the variable names.  To Mathematica, "ab" is not a product but a two-letter variable name.

a bab

Note that Mathematica remembers the values of a and b.

Exercise 1

The following calculations suggest a pattern.  What is the general formula suggested by these calculations?

Clear[x]Expand[(1 - x) (1 + x)]Expand[(1 - x) (1 + x + x^2)]Expand[(1 - x) (1 + x + x^2 + x^3)]Expand[(1 - x) (1 + x + x^2 + x^3 + x^4)]

Save your work!

To close this collection of cells, click twice on the outer cell bracket.

Defining Functions and Constants
Click twice on the outer bracket at the right to open the cell.

Defining functions in Mathematica is easy.  Here's an example:

f[x_] := Cos[x];

By executing the above command (which you should do now if you haven't already), you tell Mathematica that the function f is defined to be Cos[x].  There are three important points that you need to remember when defining functions:

To evaluate a function, you just "plug in" a value.  For instance, if we want to know f[7π/12], we just type it in:

f[7  Pi/12]

Note:  For this command to evaluate properly, you must first have executed the definition for f above.  All definitions must be executed prior to their use during each Mathematica session.

Here's f[1]:

f[1]

Remember that Mathematica gives exact answers unless told to do otherwise.  Here's a decimal approximation to f[1]:

N[f[1]]

Defining constants is even easier than defining functions.  Suppose that you wish to set the constant a to be equal to 1.4142135624, which happens to be the first ten decimal places of the square root of 2.  All you do is the following:

a = 1.4142135624

To close this collection of cells, click twice on the outer cell bracket.

Two Useful Commands
Click twice on the outer b....., well, you know the drill!

Two commands that you will use constantly are

Here is an example of how to use these commands.

N[3432902008176640000/121645100408832000]

Some other things about copy-and-paste:

Getting Help in Mathematica

Mathematica is a big program with lots of parts.  However, it's not necessary to learn everything about Mathematica in order to use it.  Feel free to experiment -- look at the menus, try out different things, play with the program.  Don't be afraid of breaking anything.  You can't break the machine just by pushing keys!

You can get help from the Mathematica program itself.  If you want to find out what a certain function does, you can type ?"FunctionName".  For example, to find out what the functions Divisors and FactorInteger do, you would type:

?Divisors

?FactorInteger

You get more information by using two question marks:

?? Divisors

?? FactorInteger

Selecting "Help . . ." in the "Help" menu provides a means of searching for commands.

As you go along, you may find that something doesn't work the way you expect it to, or that there is something you would like Mathematica to do, but you don't know how to make it work. In those cases, you should seek help from the following sources:

Exercise 2

(a) What does the Mathematica function Floor do?
	(b) What does the Mathematica function Ceiling do?

Save your work!