Multiply Function [x]
08-03-2018, 05:33 PM
Post: #8
 Thomas Klemm Senior Member Posts: 1,447 Joined: Dec 2013
RE: Multiply Function [x]
(08-03-2018 01:07 PM)wynen Wrote:  The multiplication of the significants could be done by a shift and add algorithm. Wikipedia

Peasant or binary multiplication

Here's a program for the HP-11C:
Code:
LBL A       ; times STO 0       ; b a CLx         ; s=0 a LBL 0       ; while x<>y        ; a s x=0         ; a=0? GTO 2       ; done RCL 0       ; b a s STO+ 0      ; b → 2b x<>y        ; a b s 2           ; 2 a b s ÷           ; a÷2 b s s ENTER       ; a÷2 a÷2 b s INT         ; a'=⌊a÷2⌋ a÷2 b s x=y         ; 2∣a GTO 1       ; even R↓          ; a÷2 b s a' R↓          ; b s a' a÷2 +           ; s'=s+b a' GTO 0       ; while LBL 1       ; even R↑          ; s'=s a' GTO 0       ; while LBL 2       ; done R↓          ; s RTN         ;

This is a translation of the following Python program:
Code:
def times(a, b):     s = 0     while a != 0:         if a % 2 != 0:             s += b         a /= 2         b += b     return s

It works for positive integers $$a$$ and $$b$$ and calculates $$a\times b$$.

Cheers
Thomas
 « Next Oldest | Next Newest »

 Messages In This Thread Multiply Function [x] - Gamo - 08-03-2018, 09:27 AM RE: Multiply Function [x] - Massimo Gnerucci - 08-03-2018, 11:31 AM RE: Multiply Function [x] - Pekis - 08-03-2018, 01:04 PM RE: Multiply Function [x] - rprosperi - 08-03-2018, 01:13 PM RE: Multiply Function [x] - wynen - 08-03-2018, 01:07 PM RE: Multiply Function [x] - Dave Britten - 08-03-2018, 02:10 PM RE: Multiply Function [x] - Dieter - 08-03-2018, 02:40 PM RE: Multiply Function [x] - Thomas Klemm - 08-03-2018 05:33 PM RE: Multiply Function [x] - Thomas Klemm - 08-03-2018, 06:45 PM RE: Multiply Function [x] - Leviset - 08-03-2018, 10:54 PM RE: Multiply Function [x] - ijabbott - 08-03-2018, 11:05 PM RE: Multiply Function [x] - Gamo - 08-04-2018, 01:41 AM RE: Multiply Function [x] - Gamo - 08-04-2018, 04:23 AM RE: Multiply Function [x] - Namir - 08-04-2018, 08:26 AM RE: Multiply Function [x] - Thomas Puettmann - 08-04-2018, 09:03 AM RE: Multiply Function [x] - brickviking - 08-05-2018, 03:12 AM RE: Multiply Function [x] - Gamo - 08-05-2018, 03:33 AM RE: Multiply Function [x] - Thomas Klemm - 08-05-2018, 04:07 PM

User(s) browsing this thread: 1 Guest(s)