Article Written for Computer Club

How Computers Do Math     by Don Grim

If you tell yourself "I wish I could do math like a computer", be careful for what you wish for and think again. Granted, a computer does math quickly but we wouldn't want to go through its particular process. A computer converts decimal numbers (0 through 9 digits) to binary numbers (0 and 1 digits). After the computer performs math calculations, it converts the binary numbers back to decimal numbers for our benefit. People would rather see decimal numbers than binary numbers since they have less digits and more meaning. As an example, the decimal number 57 has more meaning to us than its equivalent binary representation of 111001. So why do computers insist on using binary numbers? By using digits of just 0 and 1, the computer breaks down its work to simple On (1) and Off (0) switches enabling the speediest of calculations so that we don't wait as long for the processing.

A decimal number consists of multiples of 10 where a binary number consists of multiples of 2. Here's an example using the decimal number 57 and its binary equivalent:

000057 = 0 x 10^5 + 0 x 10^4 + 0 x 10^3 + 0 x 10^2 + 5 x 10^1 + 7 x 10^0

111001 = 1 x 2^5 + 1 x 2^4 + 1 x 2^3 + 0 x 2^2 + 0 x 2^1 + 1 x 2^0

Here is an example of how we would multiply 57 times 35 manually (remember your times table?):

  57

x 35

 ---

 285

171

----

1995

Here is how the computer does the same calculation which is more drawn out but quicker (for the computer):

convert 57 to a binary 111001

convert 35 to a binary 100011

     111001

   x 100011

     ------

     111001

    111001

111001

-----------

11111001011

convert 11111001011 to a decimal 1995

The original Pentium computer has trouble with certain math calculations. Pentium owners can contact Intel to get a free replacement chip that fixes the problem. Some binary numbers cause problems on the Pentium computer especially numbers with a long string of identical digits. The following calculation produces an answer of 256 on a Pentium computer where the correct answer is zero:

4195835 - ( (4195835 / 3145727) x 3145727 )

The binary representation of 4195835 is 10000000000010111111011 (many zeros).

The binary representation of 3145727 is 1011111111111111111111 (many ones).

So, if we could ask a computer about Math, it would probably answer "It's just a bunch of zeros and ones to me".

Article Written for Computer Club

Basic Bits     by Don Grim

Most hand-held calculators have a limit of 8 digits so if you multiply 99999999 times 99999999, you get a result of 9.99999 times 10^15 (scientific notation) which doesn't give too much meaning. In my pursuit to find a more accurate and meaningful solution for numbers with high digits, I turned to the trusty computer. However, the computer is limited to 16 digits. So, instead of giving up, I decided to "force" the computer to give me the results.

Though a computer limits the number of digits for a numerical variable (like A), it does not limit the number of characters for a string variable (like A$). Therefore, I set up the following program that inputs numbers as string variables (lines 40 & 50) so that I could input numbers with a lot of digits:


10 REM HDMULT.BAS - HIGH DIGITS MULTIPLICATION
20 DIM C(400)
30 CLS
40 INPUT"FIRST NUMBER";A$
50 INPUT"SECOND NUMBER";B$
60 AR$=""
70 FOR I=LEN(A$) TO 1 STEP -1
80 IF ASC(MID$(A$,I,1))<47.5 THEN 110
90 IF ASC(MID$(A$,I,1))>57.5 THEN 110
100 AR$=AR$+MID$(A$,I,1)
110 NEXT I
120 BR$=""
130 FOR I=LEN(B$) TO 1 STEP -1
140 IF ASC(MID$(B$,I,1))<47.5 THEN 170
150 IF ASC(MID$(B$,I,1))>57.5 THEN 170
160 BR$=BR$+MID$(B$,I,1)
170 NEXT I
180 FOR I=1 TO LEN(BR$)
190 FOR J=1 TO LEN(AR$)
200 K1=VAL(MID$(BR$,I,1))*VAL(MID$(AR$,J,1))
210 K3=INT(K1/10)
220 K2=K1-10*K3
230 C(I+J-1)=C(I+J-1)+K2
240 C(I+J)=C(I+J)+K3
250 NEXT J
260 NEXT I
270 FOR I=1 TO LEN(AR$)+LEN(BR$)
280 K1=INT(C(I)/10)
290 C(I)=C(I)-10*K1
300 C(I+1)=C(I+1)+K1
310 NEXT I
320 FOR I=LEN(AR$)+LEN(BR$)+1 TO 1 STEP -1
330 K1$=STR$(C(I))
340 IF VAL(K1$)>.5 THEN START=1
350 IF START<>1 THEN 370
360 C$=C$+RIGHT$(K1$,LEN(K1$)-1)
370 NEXT I
380 PRINT"PRODUCT IS:"
390 PRINT C$
400 END

Armed with the above program, high digit multiplication can be accomplished. First, I type GWBASIC HDMULT, and then I input 99999999 and 99999999 as the first and second numbers. Then the computer displays the correct accurate product of the two numbers which is 9999999800000001 (no more scientific notation).

The program works by inputting the numbers as string variables for unlimited digits (lines 40 & 50). In lines 60 through 170, the digits are reversed for easier multiplication and it excludes any characters that are not 0 through 9 (outside ASCII codes 48 through 57). In lines 180 through 310, the two numbers are multiplied in the same process as if you were multiplying by hand (multiplying two digits at a time in line 200). Lines 320 through 370 chop off unneeded leading zeros out of the resulting product.

The program is not only useful for accuracy but it helps with small numbers as well as large numbers. As an example, since we know that 99999999 times 99999999 equals 9999999800000001, we also know that 0.99999999 times 0.99999999 equals 0.9999999800000001.

You can also find some interesting patterns by multiplying large numbers. Some samples are:


111111111 times
111111111 equals
12345678987654321

11111111111111111 times
10101010101010101 equals
112233445566778898877665544332211

3333333333 times
33333333336666666667 equals
111111111111111111111111111111

37 times
3003003003003003003003003003 equals
111111111111111111111111111111

It makes you wonder what limits a computer has when it can be pushed to perform past its normal limits.

Article Written for Computer Club

Computers and Statistics     by Don Grim

Computers and Statistics go together like peanut butter and jelly. Computers make it easier to calculate statistics especially when factorials (!) and powers (^) are used in the calculations. You'll hear it said that computers don't lie and statistics don't lie. As long as the input is correct, the output is correct. However, even if the output is correct, it is hard to believe sometimes.


I'll give some examples of statistics that have fooled me. Even though I know the results are mathematically sound, they're the kind of statistics that are hard to believe at the top of the head.
Statistic One: If a lottery has a pick of 6 numbers out of 60 numbers, there
are 50,063,860 combinations.

Explanation: Suppose you had to pick only 2 numbers out of 4 numbers then
there are 6 combinations (1,2 1,3 1,4 2,3 2,4 and 3,4)

That translates to a formula of 4! / [(4-2)!*2!] = 6

Using the formula for the lottery, the result is:
60! / [(60-6)!*6!] = 50,063,860

Therefore, you'd have to buy 50,063,860 different lottery
tickets to insure being a winner!

Statistic Two: The probability that two people have the same birthday in a
group of 24 people is 53.83%

Explanation: Start small and suppose there are only 5 dates and 2 people.
The probability is the number of alike dates divided by the
total of all combinations. That probability is 5 / 25 or 20%

That translates to a formula of [5^2-5!/(5-2)!] / 5^2 = 20%

Using the formula for 365 dates and 24 people, the result is:
[365^24-365!/(365-24)!] / 365^24 = 53.83%

Therefore, whenever there is a group of 24 or more, you have
the betting odds in your favor if you bet that at least two
people have the same birthday!

Statistic Three: Pick two numbers at random. Multiply them together. You will
see 1 as the first digit 30% of the time. As an example:
53 x 371 = 19663 or 6 x 31 = 186 (first digit is 1)

Explanation: If you look at a slide rule, it shows all the results of
multiplications. On the slide rule, the digit 1 covers 30% of
the length of the slide rule. Slide rules allow multiplication
by adding logarithms. So, the limit of multiplied results
with the first digit at 1 is the logarithm of 2 which is
0.30103 or approximately 30%. The digits 1, 2, and 3 appear
in multiplications 60% (logarithm of 4) of the time.

Therefore, imagine if you had someone (or a computer) multiply
two numbers at random. If you bet on the first digit as 1, 2,
or 3 and had someone else bet on the digits 4 through 9, you
would still have the better odds winning 60% of the time!