- 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
This is my quick hack using VBScript.
I don't feel it fully implements the algorithm (no "maintaining two columns"), or is as efficient as it could be.... but, it's a start of what your code should NOT end up like...
Function Mult( a, b ) While a > 1 a = a \ 2 ' Surprise: "" is Int division! b = b * 2
If a Mod 2 = 1 Then Mult = Mult + b Wend End Function
Admin
Untested, might not work for negative numbers.
Admin
Actually not sure why I did do..while instead of just while... Oh well.
Admin
Cheeky Java solution..
private int multiple(int a, int b) {
}
:-) http://www.supercookie.co.uk
Admin
public static int multiply(int first, int second){ if(first == 1){ return second; }
}
Admin
And in PHP:
Admin
A recursive C# solution:
Admin
Same thing in python:
Admin
Waiting for a solution in brainfuck...
Admin
This is my implementation of the algorithm using standard *nix bash script (with the addition of awk):
#!/bin/bash
X="$1" Y="$2"
if [ $X -lt 0 ] then SIGN=-1 X=$(( X * -1 )) else SIGN=1 fi
LEFT="$X" RIGHT="$Y"
while [ "$X" -ne 1 ] do X=$(( X / 2 )) Y=$(( Y * 2 )) LEFT="$LEFT $X" RIGHT="$RIGHT $Y" done
L=
echo $LEFT | wc -w
RESULT=0 for I inseq 1 $L
do X=echo $LEFT | awk "{print $"$I"}"
Y=echo $RIGHT | awk "{print $"$I"}"
if [ $(( X % 2 )) -ne 0 ] then RESULT=$(( RESULT + Y )) fi doneRESULT=$(( RESULT * SIGN ))
echo "$1 x $2 = $RESULT"
Admin
Obligatory Clojure entry:
Admin
X86:
ARM:
Both approximated. Appreciate comments.
Admin
Wow! I had not time to submit a solution. Everyone was too busy Russian to provide an answer!
Admin
And a SQL implementation...
create function wtf.RussianMultiply(@m1 bigint, @m2 bigint) returns bigint as begin declare @out bigint set @out =0 while @m1 > 1 select @m1 = @m1 / 2, @m2 = @m2 * 2, @out = @out + case when @m1 & 1 = 1 then @m2 else 0 end return(@out) end go
Admin
function ruM (left, right) { var route = '', remainder = 0; while (left>1) { if (left % 2 != 0) { remainder += right; left -= 1; //route += 'remainder: ' + remainder + '\n'; } left /= 2; right *= 2; //route += left + 'x' + right + '\n'; } return right + remainder; }
Admin
A simpler PHP version:
<?php function in_mother_russia($x, $y) { $z = 0; while ($x) { if ($x % 2) $z += $y; $x = floor($x / 2); $y *= 2; } return $z; } echo 'In mother Russia, 18 x 23 = '.in_mother_russia(18, 23);Admin
Not tested.
Admin
Not sure why I used that array bullcrap, too used to string manipulation I guess. Here's a better version for PHP:
Admin
using ABAP (only a bit esoteric:-))
Admin
public class Paula extends WTF { public static string Paulabean = "brillant!"; }
Admin
A short recursive Java version:
public static long multiply( long left, long right ) { return ( ( left % 2 ) == 0 ? 0 : right + ( left == 1 ? 0 : multiply( left / 2, right * 2 ) ); }
Admin
Function Diederick ( Jan, Piet ) While Jan > 1 Jan = Jan \ 2 Piet = Piet * 2 If Jan Mod 2 = 1 Then Diederick = Diederick + Piet Wend End Function
Admin
Admin
Another Python version. This time it handles zero and negative numbers properly... Floats works as second arg but not as first. Seems like too much work to fix it though...
Admin
Bah to all your bloaty solutions. Perlmongers do it in one line :P
sub m(){my($a,$b)=@_;my $t;while($a){$t+=$b if ($a&1);$a>>=1;$b<<=1;}return $t;}
Admin
Win!
Admin
In Soviet Russia, peasants multiply you.
Admin
Various iterative solutions in python:
Admin
Ta for the teaser, well back to my reports :(
Addendum (2009-07-22 09:01): Condensed version:
Admin
simple c with a little optimization
Admin
VB.Net
Admin
Admin
Oracle SQL version:
Admin
I haven't done APL in a LONG time...
Admin
An addition to my solution (see above): The python3 version is not only one line, but only one statement! Is there any other language where you can implement it in one statement without recursion?
Admin
seems like you killed esolangs :D
Admin
Bah. Looks like we've bost the esolangs.org server.
Anyone know if ColdFusion was on there? ;o)
Admin
log(2, 18) should read log (1, &1)
Admin
Handle Negs
Admin
Correct Oracle SQL
Admin
Here's a recursive version in Ruby:
Admin
As this is the exact implementation of the multiply function on binary computing hardware, a simple
should do for most platforms :)
Admin
int RussianSum( int A, int B ){ return A ? RussianSum(A>>2,B<<2) + (!!(A&1))*(B): 0;
}
Admin
Without reading the comments.
Admin
A ruby version:
def russian_peasant_multiply(numerator, denomenator) table = [[numerator, denomenator]] while table.last[0] != 1 (n, d) = table.last table << [n / 2, d * 2] end table.reject { |r| r[0] % 2 != 1 }. inject(0) { |sum, r| sum + r[1]} end
Admin
// todo: implement russian algorithm #define multiply(a, b) ((a)*(b))
Admin
Java
public int mult(int a, int b){ return (b*(a%2)) + (a==1?0:mult(a/2,b*2)); }
Admin
My solutions with unit testing goodness:
#include <iostream>
using namespace std;
void assert_equals(unsigned long a, unsigned long b) { if (a != b) { cerr << a << " != " << b << endl; exit(1); } }
unsigned long russianMult(unsigned a, unsigned b) { unsigned long ret = 0; while (a > 0) { if (a % 2) ret += b; a >>= 1; b <<= 1; } return ret; }
int main() { assert_equals(414, russianMult(18, 23)); assert_equals(0, russianMult(0, 2)); assert_equals(0, russianMult(2, 0));
}
Admin
Whoops... Shoulda logged in first. :)
Admin
Non-recursive C#:
public int PeasantMul(int i, int j) { int accum = 0;
while (i != 0) { accum += (i / Math.Abs(i)) * ((0 - (i & 1)) & j); i /= 2; j *= 2; }
return accum; }