Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #15091 > unrolled thread

Russian Peasant Multiplication / was question on how

Started by"Dr. Robert Kragler" <kragler@hs-weingarten.de>
First post2013-07-02 04:29 +0000
Last post2013-07-02 04:29 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Russian Peasant Multiplication / was question on how "Dr. Robert Kragler" <kragler@hs-weingarten.de> - 2013-07-02 04:29 +0000

#15091 — Russian Peasant Multiplication / was question on how

From"Dr. Robert Kragler" <kragler@hs-weingarten.de>
Date2013-07-02 04:29 +0000
SubjectRussian Peasant Multiplication / was question on how
Message-ID<kqtkvo$asa$1@smc.vnet.net>
On Sun, Jun 30, 2013 at 3:29 AM, <d.a.paxton@gmail.com> wrote:

Hi Dave,
in Wikipedia you will find an explicit example for the Russian Peasant 
Multiplication. This algorithm can be simply programed in Mathematica :

RussianPeasantMultplication[x0_Integer,y0_Integer]:=
Module[{x,y,yS,nD },
x=x0;y=y0; yS=y;
nD=Max @ (Length /@ IntegerDigits @ {x0,y0});
pf=PaddedForm[#,2nD+2]&;
If[x<0,(sgn=Sign[x];x=Abs[x];),(sgn=+1;x=x;)];
While[x>=1,(Print["x=",pf @ x,"; ","y=",pf @ y,"; ","yS=",pf @ yS ];
                    x=Floor[x/2];y=2y;
                    If[OddQ[x], yS=yS+y];)  ];
  Return[sgn*yS]
]

See for example

x0=-27;y0=82;
RussianPeasantMultplication[x0,y0]

gives as output

x=     27; y=     82; yS=     82
x=     13; y=    164; yS=    246
x=      6; y=    328; yS=    246
x=      3; y=    656; yS=    902
x=      1; y=   1312; yS=   2214

Out[32]= -2214

Regards
Robert

Robert Kragler
Email : kragler@hs-weingarten.de
URL :   http://portal.hs-weingarten.de/web/kragler

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web