Home DFS SB OD SDN

Notice: How To Tip The Webmaster.


You are at the section Fun With Math-General Stuff

Computerized Long Form Math

This is a way how to get your computer to calculate with numbers longer than the number variables your computer is capable of handling.

To get an understanding, let's take these basic rules of addition and multiplication:

    a0       a0

  + b0     x b0
  ---      ---
  c1c0     c1c0

Where a0 and b0 are single digit numbers, and c1c0 can be a two-digit number.

In variable a0 is a one-digit number.
In variable b0 is a one-digit number.
In variable c0 is the ones portion of a two-digit number.
In variable c1 is the tens portion of a two-digit number.

Let a0 = 3 and b0 = 4
Add a0 and b0 and store the result in c0, which is 7 in this case
Since c0 is less than 10, then moving the tens digit to the c1 variable is not necessary.

Multiply a0 and b0 and store the result in c0, which is 12 in this case
Since c0 is greater than 9, you'll need to move the tens digit to the c1 variable. To do that, assuming that c1 is already pre-initialized to zero, Then, you take the number in c0, and divide it by 10, and leave off the decimal part, then add the integer portion to c1 like this:
c1 = c1 + INT(c0 / 10) (if the "c" array are decimal numbers)
c1 = c1 + c0 \ 10 (if the "c" array are integer numbers)

To remove the tens portion of the number in c0, you can use either method:
c0 = c0 MOD 10 (which gives the remainder of 2 in this case)
c0 = c0 - 10 * INT(c0 / 10)
c0 = c0 - 10 * (c0 \ 10)

So c1 = 1 and c0 = 2 for the multiplication problem.


Let's make this harder. Let's use more variables in each array. We're going to use the numbers 65 and 47 for this arrangement:
     a1a0        a1a0

   + b1b0      x b1b0
--------    --------
 c3c2c1c0     c3c2c1c0

We're putting the "8" in a1, the "5" in a0, the "4" in b1, and the "7" in b0.
The example is for addition. We're going to store the answer 132 in the array "c" in positions. c2, c1, and c0

The rules for adding two numbers go like this:

     a1a0    8 5

   + b1b0    4 7
 --------
     c1c0    1 2 (c0 = a0 + b0, overflow digit from c0 goes to c1)
   c2c1    1 3   (c1 = a1 + b1 + c1, overflow digit from c1 goes to c2)
 ========
   c2c1c0  1 3 2 (bring down all the lowermost variables)
Doing the computerizd math in these steps: (note that the c array is preinitialized to zero before you add)
 c0 = c0 + a0 + b0                 12 = 5 + 7
 c1 = c1 + INT(c0 / 10)            1 = INT(12/10)
 c0 = c0 - 10 * INT(c0 / 10)       2 = 12 - 10*INT(12/10)
 c1 = c1 + a1 + b1                 13 = 1 + 8 + 4
 c2 = c2 + INT(c1 / 10)            1 = INT(11/10)
 c1 = c1 - 10 * INT(c1 / 10)       3 = 13 - 10*INT(13/10)
Now lets change gears to Multiplication!

We're putting the "8" in a1, the "5" in a0, the "4" in b1, and the "7" in b0.
The example is for multiplication. We're going to store the answer 3,995 in the array "c" in positions. c3, c2, c1, and c0

The rules for multiplying two numbers go like this:

     a1a0       8  5

   * b1b0       4  7
 --------
     c1c0       3  5 (c0 = a0 x b0, overflow digit from c0 added to c1)
   c2c1      5  9   (c1 = a1 x b0 + c1, overflow digit from c1 added to c2)
   c2c1      5 29   (c1 = a0 x b1 + c1,
            7  9     overflow from c1 added to c2)
 c3c2     3  9     (c2 = a1 x b1 + c2, overflow digit from c2 added to c3)
=========
 c3c2c1c0 3  9  9  5 (bring down all the lowermost variables)
Doing the computerizd math in these steps: (note that the c array is preinitialized to zero before you add)
 c0 = c0 + a0 x b0                 35 = 0 + 5 x 7
 c1 = c1 + INT(c0 / 10)            3 = 0 + INT(35/10)
 c0 = c0 - 10 * INT(c0 / 10)       5 = 35 - 10*INT(35/10)

 c1 = c1 + a1 x b0                 59 = 3 + 8 x 7
 c2 = c2 + INT(c1 / 10)            5 = 0 + INT(59/10)
 c1 = c1 - 10 * INT(c1 / 10)       9 = 59 - 10*INT(59/10)

 c1 = c1 + a0 x b1                 29 = 9 + 5 x 4
 c2 = c2 + INT(c1 / 10)            7 = 5 + INT(29/10)
 c1 = c1 - 10 * INT(c1 / 10)       9 = 29 - 10*INT(29/10)

 c2 = c2 + a1 x b1                 39 = 7 + 8 x 4
 c3 = c3 + INT(c2 / 10)            3 = 0 + INT(39/10)
 c2 = c2 - 10 * INT(c2 / 10)       9 = 39 - 10*INT(39/10)

Doing math with two 3-digit numbers is noticibly harder:
        a2a1a0

   *    b2b1b0
 ---------------
          c1c0    (c0 = c0 + a0 x b0)
        c2c1      (c1 = c1 + a1 x b0)
      c3c2        (c2 = c2 + a2 x b0)
        c2c1      (c1 = c1 + a0 x b1)
      c3c2        (c2 = c2 + a1 x b1)
    c4c3          (c3 = c3 + a2 x b1)
      c3c2        (c2 = c2 + a0 x b2)
    c4c3          (c3 = c3 + a1 x b2)
  c5c4            (c4 = c4 + a2 x b2)
=============
  c5c4c3c2c1c0
So we need to simplify the multiplication into a formula. We could multiply and add the numbers in each element in one part and move the overflows to the next elements to the left. The addition formula would be much easier to program.

For the multiplication table:

Dimension a(100), b(100), c(10000)
* you may want to use smaller or larger values of array sizes, but be sure that
the sizes of arrays a() and b() multiply to the size of array c().

Select number a$ and b$ (we put these in string variables instead of numbers since the
numbers can hold up to 8 or 16 significant digits.

Break up the digits in a$ and b$ and place each of them into the array elements
of a() and b(). The ones digit goes in element 0 for 100, the tens
go element 1 for 101, the hundreds in element 2, 1000's in 3, and so forth.

Suppose we can put this complicated thing into a simple loop formula.
This is what abc x def looks like when coded straight:

c0 = c0 + a0 x b0
c1 = c1 + a1 x b0
c2 = c2 + a2 x b0

c1 = c1 + a0 x b1
c2 = c2 + a1 x b1
c3 = c3 + a2 x b1

c2 = c2 + a0 x b2
c3 = c3 + a1 x b2
c4 = c4 + a2 x b2

In the first third of the program, the b() array stays the same while
the other arays increments by one
In the second third of the program, the b() array is one higher,
and the other arrays increments by one, but where the c() array begins
is one higher.
In the third third, the b() array is up one more, the c() array begins
another one higher.

So, let's place the formula in a loop:
FOR x = 0 to 2
FOR y = 0 to 2
  c(x + y) = c(x + y) + a(x) * b(y)
NEXT y
NEXT x

The length of a$ and b$ varies, so we need to include their provisions:

FOR x = 0 to LEN(a$)
FOR y = 0 to LEN(b$)
  c(x + y) = c(x + y) + a(y) * b(x)
NEXT y
NEXT x

What about the overflow digits?

 c1 = c1 + INT(c0 / 10)
 c0 = c0 - 10 * INT(c0 / 10)
 c2 = c2 + INT(c1 / 10)
 c1 = c1 - 10 * INT(c1 / 10)
 c3 = c3 + INT(c2 / 10)
 c2 = c2 - 10 * INT(c2 / 10)

Lets try this method. Bear in mind that we need to set the loop to run
as many times as there are digits in a$ and b$ combined.

CAUTION: Do not reinitialize the c() array to 0!

For x = 0 to LEN(a$)+LEN(b$)
  c(x+1) = c(x+1) + INT(c(x) / 10)
  c(x) = c(x) - 10 * INT(c(x) / 10)
NEXT x

To add two really big numbers together, we'll use this program:
For the addition loop, we go from this:

c0 = a0 + b0
c1 = a1 + b1

To this:

FOR x = 0 to LEN(a$) + LEN(b$)
 c(x) = a(x) + b(x)
NEXT x

Let's put the whole program together!
DIM a(100), b(100), c(10000)
CLS

DO

a$ = "": b$ = ""
PRINT "INPUT two whole numbers:"; : INPUT a$, b$
FOR x = 0 TO LEN(a$) + LEN(b$): a(x) = 0: b(x) = 0: NEXT x

FOR x = 1 TO LEN(a$)
 a(x - 1) = VAL(MID$(a$, LEN(a$) - x + 1, 1))
NEXT x
FOR x = 1 TO LEN(b$)
 b(x - 1) = VAL(MID$(b$, LEN(b$) - x + 1, 1))
NEXT x

' operation$ = "x"
 FOR x = 0 TO LEN(a$) + LEN(b$): c(x) = 0: NEXT x

 FOR x = 0 TO LEN(a$) - 1
 FOR y = 0 TO LEN(b$) - 1
   c(x + y) = c(x + y) + a(x) * b(y)
 NEXT y
 NEXT x

FOR x = 0 TO LEN(a$) + LEN(b$)
  c(x + 1) = c(x + 1) + INT(c(x) / 10)
  c(x) = c(x) - 10 * INT(c(x) / 10)
NEXT x

c$ = ""
FOR x = (LEN(a$) + LEN(b$)) TO 0 STEP -1
  c$ = c$ + LTRIM$(STR$(c(x)))
  IF LEFT$(c$, 1) = "0" AND x > 0 THEN c$ = ""
NEXT x

PRINT a$; " x "; b$; " = "; c$


' operation$ = "+"
 FOR x = 0 TO LEN(a$) + LEN(b$): c(x) = 0: NEXT x

 FOR x = 0 TO LEN(a$) + LEN(b$)
  c(x) = a(x) + b(x)
 NEXT x

FOR x = 0 TO LEN(a$) + LEN(b$)
  c(x + 1) = c(x + 1) + INT(c(x) / 10)
  c(x) = c(x) - 10 * INT(c(x) / 10)
NEXT x

c$ = ""
FOR x = LEN(a$) + LEN(b$) TO 0 STEP -1
  c$ = c$ + LTRIM$(STR$(c(x)))
  IF LEFT$(c$, 1) = "0" AND x > 0 THEN c$ = ""
NEXT x

PRINT a$; " + "; b$; " = "; c$

LOOP

Menu:
Fun With Math-General Stuff Main Page Math Terms Formulas: Geometry Number Series Perfect Numbers-definition Hex Decimal Integer Conversion Table How To Compute The Square Root What's 13 times 7? Prove That 1 = 2 Number Tricks It's Impossible Computerized Long Form Math
Related:
Fun With Math Strip Home Page DavesFunStuff
Market Zone:
Dave's Fun Stuff
TV Zone:
Find your favorite TV shows with "Let's Watch TV!"
Notable:
Dave's Fun Stuff SDN Media News and More
Footer:
Dave's Fun Stuff Super Birthdays Contact Webmaster



© 1995-2023. davesfunstuff.com. All Rights Reserved. Reproduction of any part of this website without expressed written consent is prohibited.

Help Support Our Ad-Free Web Section

Just use our PayPal link to pay.

Please Donate Cash to help pay for webhosting, domain payments, expenses and labor in keeping this section going. Thank you.

$2, $5, $10, $20, $50, $75, $100, $ANY

Notice Of Disclosure (updated June 2023):

"David Tanny is the owner and operator of the domains davesfunstuff.com and davidtanny.com"

Website Cookie Policy