To use Mathematica, you type in a command (which you should think of as a question)
and Mathematica prints out the answer. The thing that makes Mathematica powerful is the
kinds of questions you can ask and the kinds of answers it can supply.
Let's begin with simple arithmetic. If you type in an arithmetic expression,
followed by the Enter key Ð exactly which key Mathematica takes as ``Enter'' will vary depending on your system. There may be a key explicitly labeled Enter, or, quite possibly, some other key combination will have to be used. A good possibility on many computer is to hit the Shift and Return keys are the same time Ðò€ Mathematica will print back the value of the Mathematical expression. For example, if you'd like to add 2 and 2 click somewhere in the box below (as with the previous chapter, all Mathematica input cells in this Tutorial have boxes put around them to give you a visual aid. We will drop this convention starting in the next chapter).
2 + 2
The question you have just asked is ``what is two plus two,'' and the answer
that Mathematica prints back is ``four.'' Using Mathematica is that simple.
When you type a command to Mathematica, don't forget the Enter key ---it is a crucial
piece of punctuation. Mathematica will not answer your command until it knows you
are through asking it. Also, you have the option of ending your Mathematica code with a semicolon. A semicolon tells Mathematica to execute the given line of input, but not to print out it's resul. For example, if you type
a= 2 + 2;
and hit the enter key nothing will be printed out. However, you can see that the above line of input was execute by typing
a
and the enter key, which returns the value assigned to a, 4.
You don't have to type a command on a single line, which is good because eventually we will have need to type some pretty long lines. Instead, you can use the ``backslash'' character "\" which Mathematica recognizes to mean ``continue this line with what follows.'' For example, you could have typed:
a=2\
+\
2
Of course, there's not much point to using more than one line for such a simple
command, but this will be a handy feature when you start getting more ambitious
with Mathematica.
Naturally, Mathematica knows about more than addition. Among other things, it knows
the following five binary arithmetic operations:
+ = addition
- = subtraction
* = multiplication
/ = division
^ = exponentiation
You can do all kinds of arithmetic calculations using these operators. Here
are a few. Try to predict what the answer will be before trying each one out
in Mathematica. Let's begin with one involving both addition and multiplication:
5+4*3
Did the answer surprise you? Were you expecting an answer of 27 instead of 17?
Mathematica does multiplications and divisions before it does additions and
subtractions, an idea that should be familiar to you from algebra. Using
another idea from algebra, how do you suppose you can force Mathematica to do the
addition first?
Click here for the answer
Here's an example using division:
6/21
Does anything about Mathematica's answer surprise you?
Click here for the answer
Here's an example of taking a number to a power:
2^4
that raises 2 to the 4th power. As another example,
2^1000
we'll raise 2 to the 1000th power. This is another example of Mathematica's
reluctance to give approximate answers---it gives every single digit of the
answer! (Note also that Mathematica itself uses the \ character to denote line continuation).
Mathematica also knows about the factorial operator. Recall that if n is a positive
integer, then n! is the product of the first n integers. Perhaps it won't
surprise you at this point that
100!
produces every last digit of 100 factorial.