« Prev  Page 1  Page 2  Page 3  Page 4  Page 5  Page 6  Page 7  Page 8  Page 9  Page 10  Page 11  Page 12  Page 13  Page 14  Page 15  Page 16  Next » 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 05:54
•
by
Joost
(unregistered)

And of course in Haskell:
multiply n m = sum $ map snd $ filter (odd . fst) $ russian n m where russian 1 m = [(1,m)] russian n m = (n,m) : russian (n `div` 2) (m * 2) 
Progress V9 version of Russian Peasant Multiplication
20090723 06:10
•
by
StarLite

My take using Progress V9 (will probably work in most older versions as well)
Code:

Re: Programming Praxis: Russian Peasant Multiplication
20090723 06:15
•
by
col obvious
(unregistered)

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);}

Re: Programming Praxis: Russian Peasant Multiplication
20090723 06:28
•
by
LatecomerX
(unregistered)

I've been a TDWTF reader for the past few months, and I'm dedicating my first comment to this interesting question here:
In PHP: <? function multiply($x, $y, $o = 0) { return $x > 1 ? multiply(floor($x / 2), $y * 2, $o + $x % 2 * $y) : $o + $y; } echo multiply(18, 23); ?> Also available at: http://phpieceofcake.com/?83344437 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 06:40
•
by
Alex
(unregistered)

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; } 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 06:45
•
by
A.T.
(unregistered)

To get the real prolog feeling:
may need some optimization though... 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 06:51
•
by
A.T.
(unregistered)

To get the real prolog feeling:
may need some optimization though... 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 06:54
•
by
bugmonkey

PHP and Perl, and gives the same output for both:
# 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 07:08
•
by
LatecomerX
(unregistered)

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); }

Re: Programming Praxis: Russian Peasant Multiplication
20090723 07:14
•
by
Methuselah

Here's some Lua code:
function russian(a, b) Code only: 189 bytes Code with BBCode coloring: 1190 bytes 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 07:23
•
by
dv
(unregistered)

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?

Re: Programming Praxis: Russian Peasant Multiplication
20090723 07:26
•
by
Python
(unregistered)

#! * Encoding: Latin1 *
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 for k in range(60): a = random.randrange(1,10000) b = random.randrange(1,10000) n = a*b assert rupmul1(a,b) == rupmul2(a,b) == rupmul3(a,b) == a*b if __name__ == "__main__": selftest() 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 07:32
•
by
London Developer
(unregistered)

OH DEAR!!! LOL! 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 07:40
•
by
Anonymous
(unregistered)

That ARM code is both wrong and far too long. This is perhaps slightly less wrong:
mov r2,#0 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 07:41
•
by
rjp
(unregistered)

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} 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 08:00
•
by
Osno
(unregistered)

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 } 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 08:01
•
by
Osno
(unregistered)

BTW, I think I've never seen a post in this site with 12 pages of comments.

Re: Programming Praxis: Russian Peasant Multiplication
20090723 08:01
•
by
khahem

C++ with a 'simple' iterator.

Re: Programming Praxis: Russian Peasant Multiplication
20090723 08:02
•
by
Lom
(unregistered)

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) 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 08:13
•
by
Rorick
(unregistered)

Java:

Re: Programming Praxis: Russian Peasant Multiplication
20090723 08:16
•
by
Queex
(unregistered)

This nearly works; I'll leave the rest to the inhouse coders.
public static int multiply(int x, int y) { 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 08:17
•
by
ath
(unregistered)

Wow, the number of unreadable and unmaintainable examples outweight the readable and maintainable ones by about 50:1!
That's the real WTF... 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 08:18
•
by
Mr. Black
(unregistered)

my crappy C version that doesn't work for negative numbers ;)
 <snip> 
 <snip>  
Re: Programming Praxis: Russian Peasant Multiplication
20090723 08:29
•
by
Alex Muscar
(unregistered)

The C# compiler does tail call optimisation only on x64 afaik.

Re: Programming Praxis: Russian Peasant Multiplication
20090723 09:08
•
by
Ralf
(unregistered)

Smalltalk
Note that this overwrites the System's definition of * and everything still works.

Re: Programming Praxis: Russian Peasant Multiplication
20090723 09:13
•
by
EJCorcoran

Another SQL (TSQL) 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 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 09:17
•
by
Takis
(unregistered)

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:

Re: Programming Praxis: Russian Peasant Multiplication
20090723 09:18
•
by
Osno
(unregistered)

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 nonequal instead of false.

Re: Programming Praxis: Russian Peasant Multiplication
20090723 09:21
•
by
WarheadsSE

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. PDP8, nice touch, but its still ASM! CP/M shell anyone??? 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 09:23
•
by
WarheadsSE

There is a 44 character python, and a 40 characther bc, although you still have several characters to go to catch perl :) 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 09:24
•
by
WarheadsSE

It has, but then its perl.. TIMTOW! stub..! oww. 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 09:25
•
by
wombat
(unregistered)

Only works with positive integers.

Re: Programming Praxis: Russian Peasant Multiplication
20090723 09:27
•
by
Rorick
(unregistered)

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:

Re: Programming Praxis: Russian Peasant Multiplication
20090723 10:12
•
by
Philipp
(unregistered)

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 ;) 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 10:15
•
by
jefu
(unregistered)

Haskell (with quickCheck to test it) :

Re: Programming Praxis: Russian Peasant Multiplication
20090723 10:20
•
by
the real wtf fool

The output:
The lines starting with  should be considered crossed out 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 10:24
•
by
n1L
(unregistered)

soulution as c++ template:
//////////////////////////////i.e.: int res = Pmul<2311, 1234>(); 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 10:27
•
by
czetts

Well, I wanted to go back and make this more "clever", but this works just fine (C#):
public static int MultiplyLikeARussianPeasant(int x, int y) (I haven't checked the comments for this solution, that would have been cheating!) 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 10:43
•
by
Josh Bohde
(unregistered)

37 using * and /
41 without

Re: Programming Praxis: Russian Peasant Multiplication
20090723 10:50
•
by
some VBA coder
(unregistered)

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 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 10:55
•
by
Chris Judge
(unregistered)

When you start with the 45 in the left column, you forgot to include the 76 in your sum. Oh, and by the way:

Re: Programming Praxis: Russian Peasant Multiplication
20090723 11:01
•
by
Rorick
(unregistered)

In rebol with caching of generated code stuff.

Re: Programming Praxis: Russian Peasant Multiplication
20090723 11:01
•
by
dub
(unregistered)

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) (b*2) )  otherwise = (rus (div a 2) (b*2) ) 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 11:06
•
by
MichaelWH

Erlang, as a module.
module(peasant). 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 11:09
•
by
Kman
(unregistered)

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; 
The only real solution:

Re: Programming Praxis: Russian Peasant Multiplication
20090723 11:25
•
by
Coyne

This is in Java. It was tested and works for negatives.

Re: Programming Praxis: Russian Peasant Multiplication
20090723 11:48
•
by
markwhi
(unregistered)

Late to the game, I guess. Replying without viewing comments, wonder if mine is original :)
http://pastebin.com/f50d7e6d5 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 11:51
•
by
markwhi
(unregistered)

I guess I should post the code, not just a link to it:
/** * Submission for TDWTF Programming Praxis 1: Russian Peasant Multiplication * July 23, 2009 */ #include <stdio.h> #include <stdlib.h> 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 ; if (a == 1) break ; a /= 2 ; b *= 2 ; } printf ("%i\n", total) ; return 0 ; } 
Re: Programming Praxis: Russian Peasant Multiplication
20090723 11:54
•
by
HCGL
(unregistered)

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); foreach (var row in columns) product += row.Value * (row.Key % 2); return product; } 
« Prev  Page 1  Page 2  Page 3  Page 4  Page 5  Page 6  Page 7  Page 8  Page 9  Page 10  Page 11  Page 12  Page 13  Page 14  Page 15  Page 16  Next » 