- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Nice one for a golf challenge.
Admin
Recursive approach using ORACLE PL/SQL:
Admin
You and your prmature optimizations! I made my code follow the instructions (in Pascal implementation of SCAR)
Admin
What, no Haskell? Oh you sorry bunch of infidels...
mlt :: Int -> Int -> Int mlt 1 y = y mlt x y = (if x
mod
2 == 0 then 0 else y) + mlt (xdiv
2) (y * 2)Admin
Haskell:
Admin
A few people have missed a bug in their code when the first value equals 1.
Admin
Bah. Looks like me hearing of this method for the first time. In Soviet Russia we just multiply it by electronic calculators. Or by abacus. Or by slide rule. Whatever appropriate.
Admin
Quick and dirty (just how I like it!) and hopefully stays true to the manual working process, not being naughty and skipping a few steps. When run in a suitable VBA host (e.g. Excel) will even show the working table.
Sample output:
Addendum (2009-07-22 09:26): Oops, posted the +ve numbers only version. Replace Sqr(x) with Sqr(Abs(x))), add a new long s, where s = Sgn(x) * Sgn(y), and amend the output as required
Admin
C# (I normally do VB, but wanted simple iterator support):
Admin
Java. Untested and recursive.
public void int multiply(int x, int y) { if(x == 1) { return y; } else if(x%2 == 1) {
return y + multiply(x/2, y2); } else { return multiply(x/2, y2); } }
Admin
I was concerned my entry was too long and too legible. So I've shaved 11 bytes off it :)
sub m(){my($a,$b)=@_;my$t;while($a){$t+=$b if$a&1;$a>>=1;$b<<=1;}$t;}
Admin
You got there a couple of minutes before me... and more concisely. In my defence, it has been about 5 years since I last wrote any Haskell... but I share your sentiment ;)
Admin
TRWTF is the animated gif
Admin
int russianPeasantMultiply(int a, int b) { // Russian peasant just optimized his code return a * b; }
Admin
Perl solution. Obeys strict pragma and has error checking.
Admin
simple non-WTF C++ template version:
template <typename T> T russian_multiply(T paula, T bean) { T brillant = 0; for(;;) { if( paula & 1 ) brillant += bean; paula /= 2; if( paula == 0 ) return brillant; bean *= 2; } }
#include <iostream>
int main() { long a, b; std::cin >> a >> b; std::cout << a << "*" << b << "=" << russian_multiply(a,b) << std::endl; }
Admin
//Not really important part :) #define and ,int #define lets int #define be = #define is = #define jack_is_alive (jack>1){ #define jack_is_not_even (jack%2) #define plus + #define divided / #define multiplied * #define begin_action { #define end_action }
//The code itself lets multiple(lets jack and john) begin_action lets bill be 0; while jack_is_alive if jack_is_not_even bill is bill plus john; jack is jack divided 2; john is john multiplied 2; end_action return bill plus john; end_action
//You can call function like that multiple(int num1, int num2). It's written in C of course
Admin
I've just learned Python so I just had to contribute... this version doesn't actually use the * or / operators, which I think defeats the purpose somewhat. Works with all integers, positive or negative.
Admin
Ah, but yours is truer to the spirit of the algorithm provided. Mind, I'm a mere dabbler in the high mysteries of Haskell.
Admin
Lua
Admin
New and improved recursive ruby version:
<3 operator overloading
Admin
C# again, but without that stinky Math.Abs():
public int PeasantMul(int i, int j) { int accum = 0;
while (i != 0) { accum += ((i >> 31) | 1) * ((0 - (i & 1)) & j); i /= 2; j *= 2; }
return accum; }
Admin
In UserRPL, as I've got my HP 48 handy, and I'm in the mood for the unconventional approach.
<< 0 3 ROLLD WHILE OVER 0 > REPEAT IF OVER 2 MOD THEN ROT OVER + 3 ROLLD END 2 * SWAP 2 / FLOOR SWAP END DROP2 >>
Admin
Admin
Oxymoron!
Admin
In Delphi:
function RussianPeasantMultiply(a,b: Integer); begin result := 0; while a>0 do begin if (a AND 1) = 1 then result := result + b; a := a div 2; b := b * 2; end; end;
Admin
in newLISP:
Admin
Since I got beaten to the punch with the Haskell version, here's one for vimscript:
To use: save to ~/peasant.vim, :source ~/peasant.vim, then
will insert the result of 42 × 71. Maybe if I have some time later, I'll make it print out the workings too ;)Admin
Here's a PHP version that displays the process sequentially, like the example:
Result is: '.substr($dispResult, 0, strlen($dispResult) - 2).'= '.number_format($result).'
'; } ?> </body> </html>Working demo at http://sandbox.secularcoding.com/wtf.php?left=18&right=23
Admin
fun mult_inner(x, y, accum) = if (x = 0) then accum else if (x mod 2 = 1) then mult_inner(x div 2, y * 2, accum + y) else mult_inner(x div 2, y * 2, accum); fun mult(x, y) = mult_inner(x, y, 0);
I think this is the first recursive implementation in the comments to be truly tail-recursive (and hence use a fixed amount of memory with a non-braindead compiler).
Admin
Every good WTF starts with these words.
Admin
Someone straighten me out here but I think the peasant don't know math for crap.
May be I'm doing it wrong but when I Multiply 45 x 76, I get 3420
Peasants get:
Flip the numbers and you get the right answer 76 45 38 90 0 19 180 180 9 360 360 4 720 0 2 1440 0 1 2880 2880
Peasant math apparently only works if one of the number is even and in the first column.
This must be why they remain peasants.
Admin
C#, with yield return, of course!
Admin
Well, here is a version in Pascal.
Anyone noticed this only works if the left number is even?!
1823 works fine (414), bit 2318 would be 396.
Admin
code golf: Python 3, 1 statement, 1 line, 70 characters:
Admin
Oh hey, I did this in my first algorithms class lecture a while back.
Haskell:
Admin
LISP
((defun russianpeasant ("They start by writing the two numbers to be multiplied at the head of two columns.) (Then they repeatedly divide the number in the left column by (two ) (dropping any remainder) and ((double) the number in the right column), writing the (two new numbers) immediately below their predecessors, until the number in the left column is one.) ((Then they) (cross out all rows) where the number in the left column is even), and (((add the remaining numbers) in the right column), which is the desired product)). (For instance, the product eighteen times twenty-three is found like this.")))))
Admin
CAPTCHA: appellatio -- sounds like a dirty fruit fetish
Admin
int mul(unsigned a, unsigned b) { for (int s=0; a; a>>=1, b<<=1) s+=a&0x1?b:0; return s; }
Admin
You forgot to add the initial 76: 45 is odd.
Admin
Simple and readable recursive python-function:
Admin
"Features":
Admin
Ok, screw that about the left number needing to be even. Just forgot to use that one, too.
Well, guess it goes with my name. ;)
Admin
Better Haskell version: (this one pretty much follows the algorithm given part by part).
Sometimes the unfoldr function feels silly.
And to satisfy your craze for testing:
Admin
Recursive Java version. Also works with negative and zero.
Admin
Only works under Python 3.x
Admin
#!r6rs ;Scheme (for Phil :)
(define (russian* x y) (cond [(zero? x) 0] [else (let-values (((d m) (div-and-mod x 2))) (+ (if (zero? m) 0 y) (russian* d (bitwise-arithmetic-shift-left y 1))))]))
Admin
Admin
Note the first line in particular.
Admin
Perl, with some nice recursion...
Probably already mentioned before in some variations, but I just want to have such a cool WTF-sticker :-)