a * b

A

Andrey Tarasevich

Siam said:
...
Here's a way without an accumulating sum, however :p

for(int i=1;i<b; i++)
{
a += a/i;
}
...

I'd say that you are still accumulating a sum here. This is more of a way to do
it without preserving the original value of 'a'.
 
C

carlmuller

Victor said:
If multiplication is not available on your hardware, then you create a loop
in which you add 'a' to 'a' (b-1) times. That's what they were supposed to
teach you in grade school.

That wasn't how I was taught multiplication in primary school.
Where are you from, the Roman Empire?
We have positional systems these days.
As they teach you in computer class, repeated addition is a really poor
algorithm,
since the runtime is proportional to the value of the number.
You should at least get runtime proportional to the log of the value of
the number,
as Ivan's method showed.
Of course my favourite multiplication algorithm is slightly different.
(a+b)*(a+b) = (a*a)+(b*b)+(2*a*b)

Therefore:
ab = (square_table[a + b] - square_table[a] - square_table) >> 1;

You just need the square_table to be big enough.
Carl.
 
R

Ron Natalie

That wasn't how I was taught multiplication in primary school.
Where are you from, the Roman Empire?
We have positional systems these days.
As they teach you in computer class, repeated addition is a really poor
algorithm,
since the runtime is proportional to the value of the number.

Well I can tell you how it worked back in the early processor days
when there wasn't an inherent multiplier. You did a series of
SHIFT and ADDS (one for each bit in the multiplier). This was
a constant time operation but many times slower than just the
ADD. The multiplication result accumulator was a double wide
register as a result.
 
L

Luc The Perverse

Ron Natalie said:
Well I can tell you how it worked back in the early processor days
when there wasn't an inherent multiplier. You did a series of
SHIFT and ADDS (one for each bit in the multiplier). This was
a constant time operation but many times slower than just the
ADD. The multiplication result accumulator was a double wide
register as a result.

It would be very easy with a for loop :)

But remember, the OP couldn't fit the processor instruction for a*b into
memory, so I think a loop is completely out of the question ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top