Home > Delphi, Object Pascal, OP4JS, Smart Mobile Studio > Decomposing multiplications, Egyptian Style

Decomposing multiplications, Egyptian Style

February 13, 2015 Leave a comment Go to comments
The eye of Horus

The eye of Horus

Ever wanted a function that takes a number and breaks it down? For instance, let’s say someone types in 1924, wouldn’t it be great if you could quickly and easily generate how to sum up that number? Which in this case is 4 + 128 + 256 + 512 + 1024 = 1924. Not the function you use on a daily basis, I know – but still a cool feature to have.

Egyptian mathematics

The Egyptians that built the pyramids were clever people. For instance, they had two alphabets (one religious and one secular) and they also had two multiplication methods. They also had a number for everything in nature, which made it possible for them to calculate resonance even more accurately than we do now (because every sound also had a number, so matching number, material and sound is easy enough). Every single stone in the Amun temple is carved to fit a number and a shape — and the temple itself is built as a duplicate of the human body (with the holy-of-holies where the pineal gland is positioned. Thats where Alexander the great was buried, in Luxor). So these guys knew a lot of cool stuff that we have forgotten or never even investigated.

But back to their way of multiplying numbers. Their most common way to doing this was simply called “summing up”. And it goes a little something like this:

Let’s say you want to find the solution to: “9 * 13”. What the Egyptians did was to first create a table where they double-up to reach the closest match to “9”, like this:

1
2
4
8

Then they created a second row, where they doubled 13, which is our multiplier:

1  ..  13
2  ..  26
4  ..  52
8  ..  104

With these two tables written as above, they then made a checkmark on the numbers which, when you add them together from the left table, makes 9. In this case that is #1 and #8.

1  ..  13 —
2  ..  26
4  ..  52
8  ..  104 —

To get the correct answere to “9 * 13” all you have to do is to add the checked numbers together from the right table. Which means that the correct answer is 13 + 104 = 117. Pretty cool right?

Use the binary force

You may have noticed that the left-side table is actually plain old binary. So yes, the ancient Egyptians actually invented (or discovered) binary. But it also means that we can use ordinary bit testing to figure out how to turn a number into an equation. We simply check the bits in the integer value and then sum up from the generated right-side table.

Here it is, written in Smart Pascal (Smart Mobile Studio):


const
  QTX_BITS:  Array[0..12] of Integer =
    ( 1,
      2,
      4,
      8,
      16,
      32,
      64,
      128,
      256,
      512,
      1024,
      2048,
      4096); //..Fill in the rest here, 32 bits in an integer

  w3button1.OnClick:=Procedure (sender:Tobject)
    var
      mValue: Integer;
      x:  integer;
      mTotal: Integer;
    begin

      var mStack:Array of String;

      mValue:=1924;
      for x:=0 to 31 do
      begin
        if TInteger.getBit(x,mValue) then
        begin
          mStack.add(QTX_BITS[x].toString);
          writeln('Bit #' + x.toString + ' = ' + QTX_BITS[x].toString);
        end else
        writeln('Bit #' + x.toString + ' = ' + '------');
        if QTX_BITS[x]>=mValue then
        break;
      end;

      writeln(' ');
      writeln('Adding:');
      if mStack.length>0 then
      Begin
        mTotal:=0;
        for x:=mStack.low to mStack.high do
        begin
          if x>mStack.low then
          writeln(' + ' + mStack[x]) else
          writeln('   ' + mStack[x]);
          inc(mTotal,TVariant.AsInteger(mStack[x]));
        end;
        Writeln('--------------------');
        Writeln(' = ' + mTotal.toString);
      end;
  end;
end;

When you run this, providing the number 1924, it breaks it down and gives you the following output:

Bit #0 = ------
Bit #1 = ------
Bit #2 = 4
Bit #3 = ------
Bit #4 = ------
Bit #5 = ------
Bit #6 = ------
Bit #7 = 128
Bit #8 = 256
Bit #9 = 512
Bit #10 = 1024
Bit #11 = ------

Adding up:
     4
+  128
+  256
+  512
+ 1024
--------------------
= 1924

Turning this into a neat little function should be easy enough for anyone. And I’m fairly sure that it can be re-factored into something even more impressive, using simple addition and multiplication, like: “4 + (32 * 4) + (128 * 2) + 512 + ( 32 * 32)” making your string output look incredibly smart and clever.

The final product

Having played around with this code, and also fixed two typo’s in this post, I sat down and cleaned the code up. Here is the final function. It returns a string giving you the numbers to add in order to produce the final outcome:

The final outcome in action

The final outcome in action

function QTX_GetNumberProducer(aNumber:Integer):String;
const
  QTX_BITS:  Array[0..31] of Integer =
  ( 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
    4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288,
    1048576, 2097152, 4194304, 8388608, 16777216, 33554432,
    67108864, 134217728, 268435456, 536870912, 1073741824,
    2147483648);
var
  x:      Integer;
  mStack: Array of String;
Begin
  result:="";
  if aNumber>0 then
  begin
    for x:=QTX_BITS.low to QTX_BITS.high do
    begin
      if TInteger.getBit(x,aNumber) then
      mStack.add(QTX_BITS[x].toString);
      if QTX_BITS[x]>=aNumber then
      break;
    end;

    if mStack.length>0 then
    begin
      result:=aNumber.toString + ' = ';
      for x:=mStack.low to mStack.high do
      begin
        if x>mStack.low then
        result += '+' + mStack[x] else
        result += mStack[x];
      end;
    end;
  end else
  result:="0";
end;

And as a bonus I added this:


Function QTX_GetNumberMultiplier(aFirst,aSecond:Integer):String;
var
  mSum:Integer;
Begin
  mSum:=aFirst*aSecond;
  result:=aFirst.toString + ' x ' + aSecond.toString +' = '
    + QTX_GetNumberProducer(mSum);
end;

The last snippet produces an output like this. Which can be handy for children when learning to multiply (or confuse them utterly, whatever works):

A bit of fun for the kids

A bit of fun for the kids

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: