« 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
20090722 11:34
•
by
antelopelovefan

Here's another VBScript version meant to run as an Excel macro.
It assumes the values to be multipled are in cells A2 and B2 of the first Worksheet and it stores all of the intermediate numbers in the cells below as it calculates. The final output is put in cell C2

Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:35
•
by
Quxxy
(unregistered)

A version for the D programming language; both as an interative function and a template:
// Regular version. Can also be used in compiletime code. 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:35
•
by
f3nd3r
(unregistered)

Wow, you make PHP look good, that is pretty impressive.

Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:37
•
by
Amadan
(unregistered)

In Ruby,

Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:37
•
by
Northpole
(unregistered)

Ruby, one line, nonrecursive:
a,b,res=a/2,b*2,(res0)+(a&1)*b while a>0 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:42
•
by
campkev

Second attempt at C#, this works with negative numbers:
public int RussianMultiply(int operand1, int operand2) { int res = 0; while (operand1 != 0) { if (operand1 % 2 == 1) res += operand2; else if (operand1 % 2 == 1) res = operand2; operand1 /= 2; operand2 *= 2; } return res; } 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:42
•
by
jweir77

Different twist on a Ruby solution:

Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:43
•
by
halber_mensch

I'd argue it's not quite the russian algorithm though, since you've both reversed the operands and you are evaluating alog2(a) more iterations than would be done in the real algorithm. For example, if I were to feed 5,4 into the russian algorithm, it would make 3 iterations (the left column being 5,2,1). Taking into account that you've swapped the parameters, your algorithm would evaluate 5 iterations (the left column being 5,2,1,0,0), or two more (5  log2(5)) iterations than you should if you were adhering to the real algorithm, which stops evaluating when reaching 1 in the left column. Your solution also fails for negative values of a. 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:43
•
by
campkev

second attempt at C# recurisive, this works with negative numbers:
public int RecursiveRussianMultiply(int operand1, int operand2) { return operand1 == 0 ? 0 : RecursiveRussianMultiply(operand1 / 2, operand2 * 2) + (operand1 % 2 == 1 ? operand2 : 0) + (operand1 % 2 == 1 ? operand2 : 0); } 

Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:46
•
by
Alex Guzman
(unregistered)

int first = 18;
int second = 23; int result = 0; while ((first = first / 2) >= 1) { second = second * 2; if (first % 2 != 0) result += second; } 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:47
•
by
kastein

Yeah... it isn't really a good idea. I thought it was more sane until I considered the results... for 8 bit multiplication we have two 8 bit inputs and a 16 bit output, so the table is 2^16 bits long with 16 bit entries... that's 1M transistors without the decoders/addressing logic. The average 8 bit CPU has a few thousand to a few tens of thousands of transistors. It just gets worse from there  multiplying two 16 bit numbers requires 4G 32bit entries (128G transistors!) and multiplying two 32 bit numbers requires 1024E transistors... multiplying 64 bit numbers using a lookup table would require more transistors than there are on the planet (by at least a factor of 1 pentillion based on my napkin calculations...) If you want to know how processors multiply, several methods are listed here: wikibooks: multiply Booth's Algorithm some more are listed here, not all easily applicable to microprocessor design: multiplication algorithms More (basically the peasant's method in hardware!) binary multiplier 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:48
•
by
Jürgen
(unregistered)

Python:
Ruby:

Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:53
•
by
Bryan
(unregistered)

All we need is a SAS submission and this thread will be complete.

Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:53
•
by
Alex Muscar
(unregistered)

Common Lisp
Imperative/iterative and functional solutions. Ain't it nice that Lisp's so versatile? :P

Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:55
•
by
Josh Bohde
(unregistered)

Yeah, I was saving some characters (at the cost of a lot of memory). Here's a recursive version that is 48 characters m=lambda a,b:[0,b][a&1]+m(a>>1,b<<1)if a else 0 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:56
•
by
Sal
(unregistered)

Postscript:
/rmul{0{2 index 0 eq{exit}if exch dup 1 bitshift 4 1 roll 3 1 roll dup 1 bitshift 5 1 roll 1 and 1 eq {add}{pop}ifelse}loop exch pop exch pop}def 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:58
•
by
AdT
(unregistered)

All the people who don't know what an optimizing compiler is also fail. 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:59
•
by
Alex Muscar
(unregistered)

Common Lisp:
Shorter version of the recursive implementation:

Re: Programming Praxis: Russian Peasant Multiplication
20090722 11:59
•
by
Murray
(unregistered)

public String multiply(int a, int b){
return "Murray is awesome"; } 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:03
•
by
PopeHappyCat
(unregistered)

Damn, you beat me too it. But here you can has lolcode spec 1.2
(Untested) HAI 1.2 CAN HAS STDIO? I HAS A X I HAS A Y I HAS A MULTIPL X R NUMBR Y R NUMBR VISIBLE "CAN HAS X?:)" GIMMEH X VISIBLE "CAN HAS Y?:)" GIMMEH Y HOW DUZ I RPM YR X AN YR Y I HAS A MUTLIPL ITZ 0 I HAS A MODD I HAS A LOLCAT ITZ MAEK WIN A NUMBR BTW IS DUMMY VARIABLE FOR L00P IM IN YR RPMLOOP UPPIN YR LOLCAT WILE DIFFRINT X AN 1 X R MAEK QUOSHUNT OF X AN 2 A NUMBR Y R PRODUKT OF Y AN 2 MODD R MOD X AN 2 BOTH SAEM MODD AN 1, O RLY? YA RLY, MULTIPL R SUM OF MULTIPL AN Y NO WAI, BTW NOTHING HAPPENS OIC IM OUTTA YR RPMLOOP FOUND YR MULTIPL IF U SAY SO MULTIPL R RPM X Y VISIBLE "PRODUKT OF " N X N " N " N Y N " IZ " N MULTIPL N ":)" KTHXBYE 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:03
•
by
Fooba
(unregistered)

C implementation:
int russian(int a, int b) {int c=0,d=a^b;a*=(a<0)?1:1;for(a<<=1;(a>>=1)!=0;b<<=1)c+=a&0x1?b:0;return((c^d)<0?c:c);} 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:03
•
by
koblas

Another python version

Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:06
•
by
Bosshog
(unregistered)


Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:07
•
by
halber_mensch

m(1,2) .... File "<stdin>", line 1, in <lambda> File "<stdin>", line 1, in <lambda> RuntimeError: maximum recursion depth exceeded =( 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:08
•
by
Timepass
(unregistered)

result: Multiplication of 18 and 23:414 Multiplication of 13 and 13:169 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:08
•
by
chriswatson

C# using the delightful LINQ to objects

Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:11
•
by
Jürgen
(unregistered)

Alex thats nice, and very similiar to my ruby/python versions, except where summarization occurs.
I didn't bother with the 3rd acc parameter because ruby/python don't do tailend optimization on recursive calls any way. That's probably why many diss recursive solutions, but my profiler tells me theres little difference especially if you compare to solutions using python list comprehensions or ruby's .each iteration. 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:11
•
by
Anonymous
(unregistered)

NO U!

Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:12
•
by
Rob W.
(unregistered)

#!/usr/bin/env python
import sys try: sys.argv[1] sys.argv[2] except IndexError: numbers = raw_input("Please enter the two integers you wish to multiply separated by a space: ").split(' ') else: numbers = sys.argv[1:3] left = int(numbers[0].strip()) right = int(numbers[1].strip()) product = 0 while left != 0: if left % 2 != 0: product += right left /= 2 right*= 2 print "The product of " + numbers[0].strip() + " x " + numbers[1].strip() + " is " + str(product) 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:13
•
by
serg
(unregistered)

Some thoughts...
8< int mult(int a, int b) { int result = 0; while(a) { if(a & 0x01) { result += b; } a >>= 1; b <<= 1; } return result; } 8< int mult2(int a, int b) { int result = 0; if(a != 0) { result = mult2(a >> 1, b << 1); if(a & 0x01) { result += b; } } return result; } 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:13
•
by
Jannik Jochem
(unregistered)

My functionalstyle java version...

Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:14
•
by
Osno
(unregistered)

So you're basically agreeing with the " a * b " solution? Because defining multiplication by using multiplication is kind of weird (ok, I agree is in the spec, but anyway). And no, when you're using rounding in vb.net (or the "\" operator) I can assure you that doesn't get optimized. And C# doesn't get optimized, at least not in IL (but maybe at the JIT level). 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:14
•
by
wolf550e
(unregistered)

http://www.phy6.org/outreach/edu/roman.htm
This is not a Russian method but an ancient Roman one. 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:17
•
by
tirerim

#!/usr/bin/perl I admit that this takes a bit of a shortcut: it checks each pair of numbers as it comes up, and adds the second to the result if necessary at that time, rather than saving them all and waiting for the end. For anyone who really likes whitespace:

Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:17
•
by
SimonY
(unregistered)

let peasant a b =
let rec peasant a b acc = if a = 1 then b + acc else match a%2 with  0 > peasant a/2 b*2 acc  1 > peasant a/2 b*2 acc+b in peasant a b 0 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:18
•
by
Dave Havlicek
(unregistered)

ruby:
class Fixnum 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:19
•
by
Thuktun

Well, that's freakishly easy.

Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:23
•
by
JoLoCo
(unregistered)

Here, for the sake of it, is a Sinclair BASIC version.
Is this the first entry to feature line numbers? 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:23
•
by
Patrick Simpson
(unregistered)

An F# quicky:
let rec RussianPeasantMultiplication a b = 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:24
•
by
idle
(unregistered)

untested Java, quick and dirty...
public static int mult(int x, int y){ Integer x1 = new Integer(x); int result = 0; for(int i = 0; i < Integer.toBinaryString(x1).length(); i++){ if(Integer.toBinaryString(x1).endsWith("1")) result += y * (int)Math.pow(2, i); x1 = Integer.rotateRight(x1, 1); } return result; } 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:25
•
by
halber_mensch

Extrapolated from discussion with Josh Bohde:
m=lambda a,b:b*(a&1)+m(a>>1,b<<1) if abs(a)>1 else a*b 55 chars... not as short but handles all integers. 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:25
•
by
Noah Easterly
(unregistered)

Haskell:

Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:26
•
by
klagdon
(unregistered)

Accounts for either argument being zero, also throws an exception if trying to multiply a negative number. Tested with given main() method

Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:26
•
by
Chad M
(unregistered)


Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:26
•
by
Dave van der Brugge
(unregistered)

So many nice solutions, yet nobody has offered a solution using XSLT yet.
So here is mine. The xsl and xml code can be seen here: http://damastabrain.mine.nu/RPA/highlight.php For a better highlight function (at least Firefox), view the xsl itself: http://damastabrain.mine.nu/RPA/russianPeasantAlgorithm.xsl And ofcourse to show the outcome: http://damastabrain.mine.nu/RPA/russianPeasantAlgorithm.xml Note: it Does work with negative numbers, on either one of the two or both sides. (Integer only) 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:28
•
by
jjs105
(unregistered)


Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:29
•
by
darklajid

Forget about the sticker, I played a lot with F# today. Thanks for the motivation.

Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:31
•
by
imhe

Accounts for 0 and negative numbers, tested with main
//Earlier posted as guest, now posting as member}
Multiplication of 18 and 23:414 Multiplication of 13 and 12:156 Multiplication of 12 and 12:144 Multiplication of 12 and 12:144 Multiplication of 12 and 12:144 
Re: Programming Praxis: Russian Peasant Multiplication
20090722 12:32
•
by
Codism
(unregistered)

Yet another Linq solution:
public static int rpm(int a, int b) { return Enumerable. Range(0, a < 2 ? 1 : (int)Math.Ceiling(Math.Log(a % 2 == 0 ? a + 1 : a, 2))) .Select(i => new { L = a / (1 << i), R = b * (1 << i) }) .Where(x => x.L % 2 != 0).Sum(x => x.R); } 
« 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 » 