| « 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
2009-07-22 02:27
•
by
Alex Papadimoulis
|
|
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 |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:20
•
by
Tim
(unregistered)
|
Untested, might not work for negative numbers. |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:22
•
by
Tim
(unregistered)
|
|
Actually not sure why I did do..while instead of just while... Oh well.
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:22
•
by
Cookie
(unregistered)
|
|
Cheeky Java solution..
private int multiple(int a, int b) { int rtn = 0; while (a > 0) { if (a % 2 != 0) { rtn += b; } a = a / 2; b = b * 2; } return rtn; } :-) http://www.supercookie.co.uk |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:23
•
by
Jay
(unregistered)
|
|
public static int multiply(int first, int second){
if(first == 1){ return second; } int result = 0; if(first%2 != 0){ result += second; } result += multiply(first/2, second * 2); return result; } |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:24
•
by
The Wolf
|
|
And in PHP:
<?php |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:25
•
by
The Dopefish
(unregistered)
|
|
A recursive C# solution:
static int Multiply(int a, int b) { if (a < 2) return (a == 1 ? b : 0); if (a % 2 == 1) return b + Multiply(a / 2, b * 2); return Multiply(a / 2, b * 2); } |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:26
•
by
ath
(unregistered)
|
|
Same thing in python:
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:26
•
by
snoofle
|
|
Waiting for a solution in brainfuck...
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:26
•
by
mat
(unregistered)
|
|
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 in `seq 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 done RESULT=$(( RESULT * SIGN )) echo "$1 x $2 = $RESULT" |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:34
•
by
hydrus
(unregistered)
|
|
Obligatory Clojure entry:
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:37
•
by
Dascandy
(unregistered)
|
|
X86:
ARM:
Both approximated. Appreciate comments. |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:39
•
by
ParkinT
|
|
Wow!
I had not time to submit a solution. Everyone was too busy Russian to provide an answer! |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:39
•
by
SQLDork
(unregistered)
|
|
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 |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:40
•
by
Z
(unregistered)
|
|
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; } |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:40
•
by
Erin
(unregistered)
|
|
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); |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:41
•
by
Larry H.
(unregistered)
|
Not tested. |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:41
•
by
The Wolf
|
|
Not sure why I used that array bullcrap, too used to string manipulation I guess. Here's a better version for PHP:
<?php |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:41
•
by
dv
(unregistered)
|
|
using ABAP (only a bit esoteric:-))
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:42
•
by
Paula
(unregistered)
|
|
public class Paula extends WTF {
public static string Paulabean = "brillant!"; } |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:42
•
by
Mat Rantell
(unregistered)
|
|
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 ) ); } |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:43
•
by
Kees
(unregistered)
|
|
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 |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:44
•
by
Bosshog
(unregistered)
|
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:48
•
by
ath
(unregistered)
|
|
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...
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:49
•
by
Me
(unregistered)
|
|
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;} |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:49
•
by
Anonymous
(unregistered)
|
|
Win!
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:51
•
by
Bombe
|
|
In Soviet Russia, peasants multiply you.
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:53
•
by
phihag
(unregistered)
|
|
Various iterative solutions in python:
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:54
•
by
Brettm
|
|
Ta for the teaser, well back to my reports :(
Addendum (2009-07-22 09:01): Condensed version:
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:55
•
by
Stefan
(unregistered)
|
|
simple c with a little optimization
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:55
•
by
Takis
(unregistered)
|
|
VB.Net
|
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:57
•
by
KnowOracle
(unregistered)
|
|
Oracle SQL version:
|
|
I haven't done APL in a LONG time...
Z <- A RUSSIAN B |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:59
•
by
phihag
(unregistered)
|
|
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?
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 08:59
•
by
freakpants
(unregistered)
|
|
seems like you killed esolangs :D
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 09:00
•
by
SR
(unregistered)
|
|
Bah. Looks like we've bost the esolangs.org server.
Anyone know if ColdFusion was on there? ;o) |
Re: Programming Praxis: Russian Peasant Multiplication - Typo
2009-07-22 09:00
•
by
KnowOracle
(unregistered)
|
|
log(2, 18) should read log (1, &1)
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 09:01
•
by
Fenris
(unregistered)
|
|
Handle Negs
>>> def mult(a,b): ... neg = (a < 0) ... if neg: ... a *= -1 ... res = 0 ... while a >= 1: ... if a % 2 != 0: ... res += b ... a /= 2 ... b *= 2 ... if neg: ... return res * -1 ... else: ... return res ... |
|
Correct Oracle SQL
|
|
Here's a recursive version in Ruby:
class Integer |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 09:05
•
by
Herman
(unregistered)
|
|
As this is the exact implementation of the multiply function on binary computing hardware, a simple
int Mult(int a, int b) should do for most platforms :) |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 09:05
•
by
Matthew Verleger
(unregistered)
|
|
int RussianSum( int A, int B ){
return A ? RussianSum(A>>2,B<<2) + (!!(A&1))*(B): 0; } |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 09:05
•
by
ruckc
|
|
Without reading the comments.
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 09:05
•
by
Mike Dotterer
(unregistered)
|
|
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 |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 09:06
•
by
gosse
(unregistered)
|
|
// todo: implement russian algorithm
#define multiply(a, b) ((a)*(b)) |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 09:06
•
by
Will
(unregistered)
|
|
Java
public int mult(int a, int b){ return (b*(a%2)) + (a==1?0:mult(a/2,b*2)); } |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 09:07
•
by
darkwraith
(unregistered)
|
|
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)); return 0; } |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 09:08
•
by
mverleg1
|
Whoops... Shoulda logged in first. :) |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 09:09
•
by
Dan
(unregistered)
|
|
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; } |
| « 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 » |