NOTATION IN MATHEMATICAL WRITING AND IN PROGRAMMING

The language of mathematics for expressions, equations, and inequalities is similar to what you see and what you produce for programming, but there are differences.

For example, if you see ab in this book or any other mathematics text, you would interpret it as a times b. To indicate the product of two variables in JavaScript (and most, maybe all, programming languages), you need to write a*b.

In programming, a variable is a construct for holding a value. If you see a+b in JavaScript, it could mean the arithmetic sum of the values stored in the variables a and b, but it also could mean concatenate the character string stored in a with the character string stored in b. This is true for some other programming languages, but not all. Concatenation of strings is not that frequent an occurrence in mathematical works on number theory, but it is used in some of the programming examples, mainly to produce something to display.

Another issue is that single character names may not be the best practice in programming. In the examples for this book, I often do use the names used in the text, but I also sometimes use longer, meaningful names, for my benefit and the benefit of the reader.

An equation in mathematics uses the equal sign = . You will see plenty of equal signs in code, but the meaning is different. For example,

a = b+c;

is an instruction to JavaScript to determine the values of variables b and c, add them (or concatenate), and then plug the result into variable a. One way to read it is “make a equal to b + c.” With this in mind, the statement

a = a+1;

is reasonable. It is an instruction to determine the current value of the variable a, add 1 to it, and then plug in the result back into the variable a. One way to read it is “make a equal to the old value of a plus 1.” This is a very common operation, and so there is a shorthand for it:

a++.  

There is another shorthand for adding anything to a variable and resetting the variable with the new value. Here is an example using the concatenation of strings meaning of the + operator:

output+=String(val)+" "; 

This means take the value of val and produce a string (if val had the value 5, then String(val) has the value “5”) and add onto it a blank. (The effect of using   is to guarantee a blank value. It is required in certain situations in HTML.) The result of all these operations is then added onto the variable output. This is used in many of the programs for this book to display results.

In JavaScript, you will see a double equal sign, ==, often in if statements. The double equal sign is a comparison operator and yields a result of true or false. (True and false are values in JavaScript, called Boolean values, after George Boole.) For example,

if (c==-1) {
  deficient++; }
else if (c==1) {
  abundant++; }
else {
 perfect++; }

checks out the value of a variable c and, depending on whether it has the value −1, 1, or anything else, increments certain other variables. JavaScript has other comparison operations, for example > and >=. If you see a single equal sign in an if statement, it probably is a mistake.

Mathematics can make use of superscripts and subscripts, square root symbol, summation symbol, multiple line expressions, and other things. Programming code must make do with one line and a limited number of symbols (no Greek letters). As I indicated, we can and should use meaningful names for variables and functions, and we can use techniques such as under_scores and camelCasing.

To indicate 2p, we make use of Math.pow(2,p). For square root, we can use Math.sqrt(n). Situations involving subscripts may be appropriate places to use arrays. To get to the kth element of an array myScores, the JavaScript is

myScores[k].

The index values (the indices) for arrays and for character strings start with 0, not 1.

Don’t be worried if you don’t understand all of this right now. It will become clear when you see the code in context and as you do your own programming. The important thing is to be ready to see similarities and differences between the languages of mathematics and programming.

There are other programming languages that have an even more mathematical nature than JavaScript. For example, check out the language Haskell. It supports infinite sets!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset