- 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
My take using Progress V9 (will probably work in most older versions as well) Code:
Admin
static int m(int x,int y){return x==1?y:x%2==0?m(x/=2,y*=2):y+m(x/=2,y*=2);}
Admin
Not sure if anyone posted this solution yet. Here's mine:
private int Multiply(int x, int y) { return x == 1 ? y : Multiply(x / 2, y * 2) + x % 2 * y; }
Admin
To get the real prolog feeling:
may need some optimization though...
Admin
To get the real prolog feeling:
may need some optimization though...
Admin
PHP and Perl, and gives the same output for both:
Admin
Came up with a more condensed PHP function after another 45 minutes or so:
function multiply($x, $y) { return $x % 2 * $y + ($x > 1 ? multiply(floor($x / 2), $y * 2) : 0); }
Admin
Here's some Lua code:
Code only: 189 bytes Code with BBCode coloring: 1190 bytes
Admin
Yes there is a better way, the MOD operator. Also, when creating a whole report for it, why not include a nice, verbose, selection screen?
Admin
#! -- Encoding: Latin-1 --
import threading import Queue
classic spaghetti code
def rupmul1(a,aa):
aaa = lambda:(a % 2) and aa
aaaa = aaa() while a > 1: a, aa = a/2, aa*2 aaaa += aaa() return aaaa
functional programming
rupmul2 = lambda a,b: sum(map( lambda b:a&b[0] and b[1],zip(map( lambda a:pow(2,a),range(31)),map( lambda a:b*pow(2,a),range(31)))))
multicore power using threads + functional programming = TOTAL WIN!
def rupmul3(a,b,=Queue.Queue()):
return sum(map(lambda _:.get(),map(lambda :[threading.Thread(target=lambda : __.put((a&1<<) and b<<),args=(,)).start(),][-1],range(32))))
def selftest():
import random
if name == "main": selftest()
Admin
OH DEAR!!! LOL!
Admin
That ARM code is both wrong and far too long. This is perhaps slightly less wrong:
Admin
Apologies if this has been pointed out already but you can shorten that slightly.
sub m{my($a,$b)=@_;my $t;while($a){$t+=$b*($a&1);$a>>=1;$b*=2}$t}
Admin
Tail recursive in IL (based on 278641, not sure if C# can compile a tail recursivity, I'll have to check later). The first param is the acumulator and should be initialized to 0 (or wrapped in another method):
.method private hidebysig static int64 Russian(int64 a, int64 f, int64 s) cil managed { .maxstack 4 ldarg.0 dup ldarg.1 brfalse.s done ldarg.1 ldc.i4.1 and ldarg.2 mul add ldarg.1 ldc.i4.1 shr ldarg.2 ldc.i4.1 shl tail. call int64 RussianMult.Program::Russian(int64, int64, int64) done: ret }
Admin
BTW, I think I've never seen a post in this site with 12 pages of comments.
Admin
C++ with a 'simple' iterator.
Admin
Python, nominate for shortest one :) 49 characters. And, with python, numbers multiply peasants.
m=lambda a,b:sum(b<<i for i in range(a)if a&1<<i)
Admin
Java:
Admin
This nearly works; I'll leave the rest to the in-house coders.
Admin
Wow, the number of unreadable and unmaintainable examples outweight the readable and maintainable ones by about 50:1! That's the real WTF...
Admin
my crappy C version that doesn't work for negative numbers ;) ----------------------- <snip> --------------------------
----------------------- <snip> ---------------------
Admin
The C# compiler does tail call optimisation only on x64 afaik.
Admin
Smalltalk
Note that this overwrites the System's definition of * and everything still works.
Admin
Another SQL (T-SQL) Answer: DECLARE @X INT, @Y INT, @Z INT SELECT @X=18,@Y=23,@Z=0 WHILE @X!=1 BEGIN IF (@X%2)!=0 SET @Z=@Z+@Y SET @X=@X/2 SET @Y=@Y*2 END SET @Z=@Z+@Y SELECT @Z
Admin
577 comments later and still no recursive VB.Net solution, so here's my second try; it handles negatives as well and doesn't use multiplication:
Admin
Just checked on an x64. At least by default, it doesn't do tail recursion. Also, when compiled in Release the output is really really close to my solution. The only real difference is that it branches on non-equal instead of false.
Admin
I believe the problem is that we've made this poor russian peasant thirsty, after warping his brain with such compressed algorithms in as many languages we could come up with. PDP-8, nice touch, but its still ASM! CP/M shell anyone???
Admin
There is a 44 character python, and a 40 characther bc, although you still have several characters to go to catch perl :)
Admin
Admin
Only works with positive integers.
Admin
Another version, now in Rebol.
It generates rebol code composed from additions and checks for oddity and then executes it. For 23 and 18 generated code looks like:
Admin
Ok, I couldn't resist. Here's an (amended!) brainfuck interpreter in Java, running the brainfuck code from Qwertyuiopas:
As you can see, you may enter integers (instead of characters whose ASCII code is interpreted), also we are dealing with Brainfuck++ here, since it has the % command ;)
Admin
Haskell (with quickCheck to test it) :
Admin
The output:
The lines starting with -- should be considered crossed out
Admin
soulution as c++ template:
i.e.:Admin
Well, I wanted to go back and make this more "clever", but this works just fine (C#):
(I haven't checked the comments for this solution, that would have been cheating!)
Admin
37 using * and /
41 without
Admin
Function peasant(factor1, factor2) peasant = 0 If factor1 < 0 Then factor2 = -factor2 While factor1 <> 0 If (factor1 / 2) <> Round(factor1 / 2, 0) Then peasant = peasant + factor2 factor1 = factor1 / 2 - (factor1 Mod 2) / 2 factor2 = 2 * factor2 Wend End Function
Admin
When you start with the 45 in the left column, you forgot to include the 76 in your sum.
Oh, and by the way:
Admin
In rebol with caching of generated code stuff.
Admin
I'm in the process of learning Haskell so here's my haskell solution. Feel free to give feedback so I can improve.
rus 0 _ = 0 rus a b | a < 0 = (-1) * (rus (abs a) b) | b < 0 = (-1) * (rus a (abs b)) | odd a = b + (rus (div a 2) (b2) ) | otherwise = (rus (div a 2) (b2) )
Admin
Erlang, as a module.
Admin
c++ templates
template <unsigned int a, unsigned int b> struct RM_ { static const unsigned int result = a & 0x1 ? b + RM_<a / 2, b * 2>::result : RM_<a / 2, b * 2>::result; }; template <unsigned int b> struct RM_<1, b> { static const unsigned int result = b; };
unsigned int c = RM_<18, 23>::result;
Admin
The only real solution:
Admin
This is in Java. It was tested and works for negatives.
Admin
Late to the game, I guess. Replying without viewing comments, wonder if mine is original :)
http://pastebin.com/f50d7e6d5
Admin
I guess I should post the code, not just a link to it:
/**
void usage (void) { printf ("usage: rmult <int> <int>\n") ; exit (-1) ; }
int main (int argc, char **argv) { int a, b, total = 0 ;
if (argc != 3) usage () ;
a = atoi (argv[1]) ; b = atoi (argv[2]) ; if (a <= 0 || b <= 0) usage () ;
printf ("%i * %i = ", a, b) ;
while (1) { if (a % 2) total += b ;
}
printf ("%i\n", total) ;
return 0 ; }
Admin
public static double RussianMultiply(int left, int right) { var columns = new Dictionary<int, int> {{left, right}}; var product=0; while (left > 1) columns.Add(left /= 2, right *= 2);
Admin
Admin
Oops... bugfix: