views:

2390

answers:

16

Here is my short implementation of Russian Peasant Multiplication. How can it be improved?

Restrictions : only works when a>0,b>0

for(p=0;p+=(a&1)*b,a!=1;a>>=1,b<<=1);
+3  A: 

I think it's incomplete, and very hard to read. What specific sort of feedback were you looking for?

Mark Bessey
a,b are the numbers,after the for loop p will be their product.there,it's not hard to read.
xxxxxxx
it is harder to read than it needs to be, and the person who wrote it is not the person to judge whether it is hard to read. What's wrong with adding a few linebreaks?
Airsource Ltd
adding linebreaks breaks compactness :)
xxxxxxx
you mean "adding linebreaks breaks unreadability". Doesn't change the compiled size.
Airsource Ltd
Why don't you just write in assembly and stop screwing around with these play languages like C. You're obviously an outstanding programmer and you should probably go into teaching or consulting.
Tim
+13  A: 

I think it's a pretty poor attempt bragging about code which, although being supposedly smart, it's obfuscated to the point that no sane developer would ever use it on production code.

...Once again, what was your question?

Vicent Marti
+4  A: 

p is not initialised.

What happens if a is zero?

What happens if a is negative?

Update: I see that you have updated the question to address the above problems. While your code now appears to work as stated (except for the overflow problem), it's still less readable than it should be.

Greg Hewgill
p is to be initialized with 0multiplication here works just for a>0,b>0 :)thanks for asking
xxxxxxx
+11  A: 

I think it's terrible This is exactly the same code from the compiler's point of view, and (hopefully) a lot clearer

int sum = 0;
while(1)
{
    sum += (a & 1) * b;
    if(a == 1)
       break;

    a = a / 2;
    b = b * 2;
}

And now I've written it out, I understand it.

Airsource Ltd
yes,that was my first implementation :) then I remembered some tricks :)
xxxxxxx
I hope I never have to write code with you, ever.I can't see why anyone would take something like the above and produce what you have, unless they were making a half-arsed attempt at an Obfuscated C contest entry. :|
Rob Howard
Rob, it's obvious in hindsight, but I actually thought you were talking to me to start with, not spx2!
Airsource Ltd
I would think that bit shifting would be faster than division or multiplication, and I'm not sure if all compilers would do it that way. (Though they might, not sure here).
Lance Roberts
bit shifting IS faster than multiplication or division BUT any decent compiler will optimise a multiplication by a constant into a set of bitshifts if at all possible. The only compiler I ran into that didn't is an IAR H8 compiler. I suspect that's not the target platform here.
Airsource Ltd
Hey, while you're at it, why didn't you replace `a )
vladr
Vlad - I can read English too, but thatdoesn'tmeanIprefertoreaditlikethis.
Airsource Ltd
+6  A: 

It's terrible. Any other questions?

F.D.Castel
for the moment no :)but thank you anyway
xxxxxxx
You're welcome :) (BTW: Tanoku and Airsource gave better answers :) )
F.D.Castel
Subtle as a furious rhinoceros in a fine crystals shop... when I grow up I wanna be like you ;-)
schonarth
+7  A: 

There is still a multiplication in the loop. If you wanted to reduce the cost of the multiplications, you could use this instead:

for(p=0;p+=(-(a&1))&b,a!=1;a>>=1,b<<=1);
MizardX
yes,that is true my friend,fantastic !thank you :)but the restriction b>0 has saved you here,were it not for thatyou would've been wrong.
xxxxxxx
+3  A: 

This is for a code obfuscation contest? I think you can do better. Use misleading variable names instead of meaningless ones, for starters.

le dorfier
+18  A: 

It's a stunning example of unparalleled genius, I am in awe of your 1337 hacker skills.

Robert Gamble
I think you meant Haxx0r instead of hacker though :)
Greg Beech
...and ztunning?
Guge
+2  A: 

How can it be improved?

Does best Trump Impersonation

YOUR FIRED

FlySwat
that's "you're"
kajaco
hehe loved that... The whole build up and then a fail in grammar. ;)
Ray Booysen
Aahhahahahaha :)))
xxxxxxx
+2  A: 
int RussianPeasant(int a, int b)
{
    // sum = a * b
    int sum = 0;
    while (a != 0)
    {
        if ((a & 1) != 0)
            sum += b;
        b <<= 1;
        a >>= 1;
    }
    return sum;
}
CMS
+35  A: 
Svante
+5  A: 
Federico Ramponi
+6  A: 

There is an really easy way to improve this:

p = a * b;

It has even the advantage that a or b could be less than 0.

If you look how it really works, you will see, that it is just the normal manual multiplication performed binary. You computer does it internaly this way (1), so the easiest way to use the russian peasant method is to use the builtin multiplication.

(1) Maybe it has a more sophasticated algorithm, but in principle you can say, it works with this algorithm

flolo
A: 

Why are you guys doing his homework?

--larsw

larsw
+3  A: 

So many downvotes. I don't see any harm he caused by posting this!

P.S. For "learning"[playing around], it's ok to code like that. But professionally I'd not expect you to code like this!

ZiG
A: 

Answer with no multiplication or division:

function RPM(int a, int b){
    int rtn;
    for(rtn=0;rtn+=(a&1)*b,a!=1;a>>=1,b<<=1);
    return rtn;
}
vladr