Home > Object Pascal, Smart Mobile Studio > Multiplication decomposer

Multiplication decomposer

Here is a fun little number decomposer I made a while back. As you might know, the ancient egyptians were no strangers to binary. They also didnt use multiplication tables like we do – but instead used a method called “double down”.

To demonstrate this technique I wrote a simple little program that takes a multiplication and then breaks it down to the numbers an egyptian would look-up in his table to find the right answere.

egypt

It’s actually pretty cool. They did not apply multiplication like we do at all, but adding

You will need to drop two textboxes, 3 labels, one button and a memo control on your form (see layout in picture).

 

unit Form1;

interface

uses
  System.Types,
  System.Types.Convert,
  System.Objects,
  System.Time,
  SmartCL.System,
  SmartCL.Time,
  SmartCL.Graphics,
  SmartCL.Components,
  SmartCL.FileUtils,
  SmartCL.Forms,
  SmartCL.Fonts,
  SmartCL.Theme,
  SmartCL.Borders,
  SmartCL.Application,
  SmartCL.Controls.Button,
  SmartCL.Controls.Label,
  SmartCL.Controls.EditBox,
  SmartCL.Controls.Memo;

type

  TDecomposer = Class(TObject)
  private
    FBitValues:   TIntArray;
    FLeftPillar:  TIntArray;
    FRightPillar: TIntArray;
    FSumPillar:   TIntArray;
  protected
    procedure BuildLeftPillar(Number:Integer);
    procedure BuildRightPillar(Multiplier:Integer);
  public
    Property  LeftPillar:TIntArray read FLeftPillar;
    Property  RightPillar:TIntArray read FRightPillar;
    Property  SumPillar:TIntArray read FSumPillar;

    function  DecomposeNumber(aNumber:Integer):String;
    function  DecomposeMultiplication(aNumber,aMultiplier:Integer):String;

    Constructor Create;Virtual;
  end;

  TForm1 = class(TW3Form)
    procedure W3Button1Click(Sender: TObject);
  private
    {$I 'Form1:intf'}
  protected
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure Resize; override;
  end;

implementation

{ TForm1 }

procedure TForm1.W3Button1Click(Sender: TObject);
begin
  var LObj := TDecomposer.Create;
  try
    var LText := LObj.DecomposeMultiplication(StrToInt(w3editbox1.Text),StrToInt(w3editbox2.text));
    w3memo1.text := LText;
    writeln(LText);
  finally
    LObj.free;
  end;
end;

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
end;

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
end;

procedure TForm1.Resize;
begin
  inherited;
end;

//#############################################################################
// TDecomposer
//#############################################################################

constructor TDecomposer.Create;
begin
  inherited Create;

  (* Build table of bitvalues *)

  var mValue := 1;
  for var x :=1 to 32 do
  begin
    FBitValues.add(mValue);
    mValue:=mValue * 2;
  end;
end;

procedure TDecomposer.BuildLeftPillar(Number:Integer);
begin
  FLeftPillar.clear;
  if FBitValues.length>0 then
  begin
    var mValue := 1;
    for var x := FBitValues.low to FBitValues.high do
    begin
      if FBitValues[x] <= Number then         FLeftPillar.add(FBitValues[x])       else         break;     end;   end; end; procedure TDecomposer.BuildRightPillar(Multiplier:Integer); begin   FRightPillar.clear;   if FLeftPillar.length>0 then
  begin
    for var x := FLeftPillar.low to FLeftPillar.high do
    begin
      FRightPillar.add(Multiplier);
      Multiplier:=Multiplier * 2;
    end;
  end;
end;

function TDecomposer.DecomposeMultiplication
         (aNumber,aMultiplier:Integer):String;
begin
  var mSum := aNumber * aMultiplier;
  BuildRightPillar(aMultiplier);

  result := aNumber.toString + ' x '
     + aMultiplier.toString
     + ' = '
     + DecomposeNumber(mSum);
end;

function TDecomposer.DecomposeNumber(aNumber:Integer):String;
begin
  FSumpillar.clear;
  FLeftPillar.clear;
  FRightPillar.clear;

  BuildLeftPillar(aNumber);

  for var x := FLeftPillar.low to FLeftPillar.High do
  begin
    if TInteger.getBit(x,aNumber) then
    FSumPillar.add(FBitValues[x]);
  end;

  if FSumPillar.length>0 then
  Begin
    result:=aNumber.ToString + ' = ';
    for var x:=FSumPillar.low to FSumpillar.high do
    begin
      if x=FSumPillar.low then
      result += FSumPillar[x].toString else
      result += ' + ' + FSumPillar[x].toString;
    end;
  end;
end;

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);
Begin
  var LStack: array of string;
  if aNumber > 0 then
  begin
    for var x := QTX_BITS.low to QTX_BITS.high do
    begin
      if TInteger.getBit(x,aNumber) then
        LStack.add(QTX_BITS[x].toString);
      if QTX_BITS[x] >= aNumber then
        break;
    end;

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

function QTX_GetNumberMultiplier(aFirst,aSecond:Integer):String;
Begin
  var LSum := aFirst * aSecond;
  result := aFirst.ToString() + ' x ' + aSecond.ToString();
  result += ' = ';
  result += QTX_GetNumberProducer(LSum);
end;

initialization
  Forms.RegisterForm({$I %FILE%}, TForm1);
end.
  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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: