03 August 2011

Lesson 1: Mathematical Syntax

This lesson covers:

  • Opening a terminal and starting bc
  • Entering numerical expressions (arithmetic and the square root)
  • The difference between real and integer modes
  • The modulo operation

You should read the Welcome post before reading this one, especially if you're running Windows and you need to get a copy of bc.

Opening a terminal and starting bc

We’ll use a terminal to run bc. A terminal lets you type commands directly to your computer. Other names for terminal include shell, command prompt, and command line. There’s almost certainly an easy way for you to open a terminal window on your computer:

When you open a terminal window, you’ll get a prompt, which is just some text telling you the computer is ready for your commands. To start bc, type bc -lq at the prompt. That's bc, space, dash, lowercase L (as in library), and lowercase Q (as in quiet). Then press Enter. On this blog, text that you need to type will look like this:

christopher:~$ bc -lq

Once you enter that, bc will be running. Anything you enter at this point will be interpreted by bc. When you're done, you can exit bc by typing halt or quit, or hitting Ctrl-D (hold down the Control key and press D). That will bring you back to the prompt.

Entering mathematical expressions

You can use bc like a calculator. Just type in a mathematical expression and press Enter.

Try typing these examples into bc and make sure you get the same results:

1 + 2 + 4 + 7 + 14
28
60 * 24 * 365
525600
2.54 * 12 * 5280 / (100 * 1000)
1.60934400000000000000
9^3 + 10^3 - 12^3
1

Problem 1: Fermat’s Last Theorem

We’re ready to solve our first problem, how exciting! Is the following statement true or false?

$$572483^5 = 356187^5 + 561386^5$$

Try to solve this problem using bc, and then click below for my solution.

Click to reveal solution
Click to hide solution

It's false. You can enter the expression on the left side of the equals sign, and the expression on the right side of the equals sign, and you can see that the two numbers have the same first 14 digits, but they're different starting at the 15th digit.

572483^5
61491200753265393043264942643
356187^5 + 561386^5
61491200753265012660230743883

By the way, this statement is related to Fermat's Last Theorem, which was the greatest unintentional prank ever played on mathematicians. In 1637 the mathematician Pierre de Fermat said that certain statements (including the one in this problem) would always be false. He said he had a marvelous proof of this theorem. He then managed to never tell anyone his proof until he died, leaving everyone to wonder if the proof really existed. It turns out that proving this theorem is really, really hard. Nobody did it for over 300 years, but lots of people tried. Maybe if Fermat hadn't said there was an easy proof, nobody would have spent so much time on it.

Imagine for a minute that you've spent years trying to prove Fermat's Last Theorem, and some joker sends you that statement. If it's true, it invalidates years of your life's work. You have to know if it's true. If you don't have a computer, that means you have to multiply out the numbers the hard way, and for ten tense minutes, you're wondering whether you're going to have to get a job herding sheep. Finally you get the answer and see that you haven't wasted your time, but not before you almost have a heart attack.

(Well unless you're Sophie Germain. Then you just say, "I didn't prove Fermat's Last Theorem, but I proved enough of it to know at a glance that your statement is false. Oh, and also I taught myself math 180 years before the local college accepted women.... so yeah.")

On the other hand, if you're not Sophie Germain, but you do have a computer with bc on it, you can open a terminal and see the statement is false in just a few seconds, and then spend the next ten minutes watching cat videos. Smooth.

More on mathematical expressions

  • Spaces don’t matter, so just add spaces wherever it makes your expressions easier to read.
  • Sometimes in math we use multiplication without a multiplication sign. For instance (10+2)(10-2) means to multiply (10+2) by (10-2). In bc, you need to include the multiplication sign every place there’s a multiplication, so write it (10+2)*(10-2).
  • You can take the square root of a number by typing sqrt and then parentheses around the number you want to take the square root of.
    (What is the square root of a number?)

Let's try entering some complicated expressions into bc. The famous number π (or pi) has an infinite number of digits after the decimal point:

π = 3.141592653589793238462643383279502884197169399375....

No matter how you add, subtract, multiply, divide, and square-root whole numbers, you'll never get exactly π. But there are lots of ways to get close. Using bc, we can see how close an expression is to π. For instance, π is approximately 22/7, but let's see how close:

22/7
3.14285714285714285714

π starts 3.141 and this number starts 3.142. Since the first 2 digits after the decimal point are correct, this approximation is good to 2 digits. Here are two approximations that are good to 4 and 6 digits, which we can find out by entering them into bc:

$$√{{40}/{3} - √{12}}$$

$${99^2}/{2206 √{2}}$$

sqrt(40/3 - sqrt(12))
3.14153333870509461863
99^2 / (2206 * sqrt(2))
3.14159273001330566031

Problem 2: π approximations

These π approximations were discovered by Srinivasa Ramanujan in 1911. He had an amazing ability to come up with creative equations like these without a calculator. One mathematician said that every number was Ramanujan's personal friend. Fortunately for those of us who aren't friends with numbers, we can use calculators when we have to.

How many digits after the decimal match π for each of the expressions?

$$7/3 (1 + {√{3}}/{5})$$

$$√{√{9^2 + {19^2}/{22}}}$$

$$(63/25) {17 + 15 √{5}}/{7 + 15 √{5}}$$

Click to reveal solution
Click to hide solution

They're good to 3, 8, and 9 digits. If you had any trouble entering the expressions, here's how you can do it:

7/3 * (1 + sqrt(3)/5)
3.14162371019880940362
sqrt(sqrt(9^2 + 19^2/22))
3.14159265258264612520
63/25 * (17 + 15*sqrt(5)) / (7 + 15*sqrt(5))
3.14159265380568820190

Rounding error

If you use division in your program, you may run into rounding errors. This means that the numbers you get are not exactly equal to the correct value. One-third plus two-thirds should add up to 1, but it doesn’t quite work out like that:

1/3 + 2/3
.99999999999999999999

There are ways to deal with this, but we're just going to keep in mind that numbers you get out of bc may have rounding error.

Real and integer mode

Like most programming languages, bc makes a distinction between real math and integer math. Basically, integer math lets you only use whole numbers, and real math lets you use numbers with decimals. Both modes are useful for solving different kinds of problems.

The simplest way to switch modes is when you start bc. If you start it up with bc -lq, you’ll be in real mode. If you leave off the l and just type bc -q, you’ll be in integer mode. There’s a little more to it than that, and it's explained below. You can skip this explanation if you want; it's not very important for what we'll be doing.

Click to reveal more explanation
Click to hide explanation

This is one area where bc is different from most programming languages. Most languages use floating-point math, and bc uses fixed-point math. Fixed-point math is actually a little easier to understand, but there are good reasons why floating-point is more popular.

With fixed-point math, there's a fixed number of decimals after the decimal point. bc uses arbitrary precision fixed-point math, which means that you can set how many decimals there are, using the scale variable.

scale = 3
1/7
.142
scale = 40
1/7
.1428571428571428571428571428571428571428

Integer mode is simply when you set scale = 0, so no digits appear after the decimal point. When you start up bc with bc -q (no l), scale is automatically set to 0, so you're in integer mode. When you start up bc with bc -lq, scale is automatically set to 20. While bc is running, you can switch between modes at any time by setting scale to however many decimal places you need.

But like I said, you don't have to worry about this. On this blog, we'll use bc -lq for real mode and bc -q for integer mode, and 20 decimals will always be enough for us.

The main difference between real mode and integer mode shows up when you divide. If you’re like me, when you were in elementary school first learning division, you were taught that 44 divided by 7 is 6R2, meaning “6 remainder 2”. Then when your teacher thought you could handle decimal points you learned that 44 divided by 7 is really 6.285714.... Basically, the first answer is what you get when you divide in integer mode, and the second answer is what you get in real mode.

Modulo operation

When you’re in integer mode, you can get the remainder by using the modulo operation, which uses the percent sign %. Since 44 divided by 7 is 6 remainder 2, 44 modulo 7 = 2. Here’s what you see if you run bc first in real mode and then in integer mode:

christopher:~$ bc -lq
44 / 7
6.28571428571428571428
halt
christopher:~$ bc -q
44 / 7
6
44 % 7
2

The modulo operation is pretty useful for problem solving. For instance, if we want to know whether one number evenly divides into another number, we can check whether the modulo is 0. If we want to know what’s the smallest number that doesn’t divide evenly into 60, we can see that it’s 7 because 60 modulo 7 is not zero:

60 % 1
0
60 % 2
0
60 % 3
0
60 % 4
0
60 % 5
0
60 % 6
0
60 % 7
4

Remember that the modulo will only work properly if you start bc in integer mode.

Fermat’s Little Theorem

Here’s another use of the modulo. We can use it to figure out whether a number is prime using something called Fermat’s Little Theorem:

If p is a prime number, and a is any number between 1 and p-1, then the remainder when you divide a(p-1) by p is always 1.

(What's a prime number?)

17 is prime. That means that if you take some number, raise it to the 16th power, and then divide by 17, Fermat's Little Theorem says the remainder will always be 1:

2^16 % 17
1
3^16 % 17
1
4^16 % 17
1

27 is not prime, because 27 = 3 × 9. That means that if you take some number, raise it to the 26th power, and then divide by 27, Fermat's Little Theorem doesn't say what will happen. The remainder could be anything:

2^26 % 27
13
3^26 % 27
0
4^26 % 27
7

Problem 3: Prime testing

One of these numbers is prime: 37901, 37907, 37909. Which one is it?

Click to reveal solution
Click to hide solution

37907 is the prime one. 37901 = 151 × 251, and 37909 = 167 × 227. Here's how we can tell:

2^37900 % 37901
12802
3^37900 % 37901
4380
2^37906 % 37907
1
3^37906 % 37907
1
2^37908 % 37909
24813
3^37908 % 37909
37474

See? When we apply Fermat's Little Theorem to 37907, we always get 1, but with 37901 or 37909 we don't.

Next time

That's it for mathematical syntax! We haven't written any actual programs yet, but we'll get to that in Lesson 2.

Thanks for reading! Feel free to leave comments and feedback.

No comments:

Post a Comment