- 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
I'm a latecomer. Still fun to do.
Code handles negative values OK.
2 VB.Net ways:
Way 1 - Recursive:
Protected Sub RussianPeasantMultiplication(ByVal n1 As Integer, ByVal n2 As Integer, ByVal n3 As Integer) If Math.Abs(n1) >= 1 Then Console.WriteLine(n1 & " x " & n2) n3 = n3 + IIf(n1 Mod 2, Math.Abs(n2), 0) * IIf(n1 < 0, -1, 1) * IIf(n2 < 0, -1, 1) RussianPeasantMultiplication(Math.Floor(Math.Abs(n1 / 2)) * IIf(n1 < 0, -1, 1), n2 * 2, n3) Else Console.WriteLine("TOTAL: " & n3) End If End SubWay 2 - While Loop:
Protected Sub RussianPeasantMultiplication1(ByVal n1 As Integer, ByVal n2 As Integer) Dim RunningTotal As Integer = 0 While (Math.Abs(n1)) Console.WriteLine(n1 & " x " & n2) RunningTotal = RunningTotal + IIf(n1 Mod 2, Math.Abs(n2), 0) * IIf(n1 < 0, -1, 1) * IIf(n2 < 0, -1, 1) n1 = Math.Floor(Math.Abs(n1 / 2)) * IIf(n1 < 0, -1, 1) n2 = n2 * 2 End While Console.WriteLine("TOTAL: " & RunningTotal) End SubAdmin
unsigned int mu_ru(a, b) { unsigned int c = 0;
}
Admin
Admin
Java
Admin
public class Praxis {
public static int multiply(int a, int b) { if(a < 0) { a *= -1; b *= -1; }
}
public static void main(String[] args) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); int c = multiply(a, b); System.out.println(a + " x " + b + " = " + c); }
}
Admin
Maude solution
Admin
int ru_mult(int a, int b) { int result = 0; do { if(a % 2) result += b; b <<= 1; } while(a >>= 1); return result; }
i can't do it smaller + faster than that..
Admin
this doesn't work for me..
Admin
I'm teaching myself Clojure, so I created a version of this in Clojure using sequence transformations:
(defn peasant-step "Performs one step in the peasant multiplication algorithm, taking a two-element vector as input, and returning a two-element vector" [[a b]] [(quot a 2) (* b 2)]) (defn all-peasant-steps "For a given pair of numbers, returns all peasant multiplication steps" [a b] (for [s (iterate peasant-step [a b]) :while (#(not (zero? (first %))) s)] s)) (defn odd-peasant-steps "Return all peasant steps where the first element is odd" [steps] (for [s steps :when (odd? (first s))] s)) (defn peasant-multiply "Perform peasant multiplication on two numbers" [a b] (reduce + (map second (odd-peasant-steps (all-peasant-steps a b)))))Admin
Kind of late but:
Admin
#!usr/bin/perl6 -e
Int $i; Int $j; Int @i; Int @j; $i = +(input());
$j = +(input());
while i != 1 { @i ,= $i; @j ,= $j; $i +>= 1; $j +<= 1; } @j = gather for @i Z @j -> $test, $result { take $result if $test % 2 } say([+] @j);
Admin
import java.io.DataInputStream; import java.io.IOException; public class PergjysmoShumezo {
long k2=System.currentTimeMillis();
{ System.out.println("Koha e e ekzekutimit eshte k2-k1=" +(k2-k1)); } }
Admin
x is the left argument, y the right argument.
x bs y: bitshift (x<<y) y#0 A list of y 0's. (#:x) The binary representation of x. , Join these together. #. Convert binary list to a number.
x rm y: russian peasant multiplication. |.#:y Reverse binary of x (lowest bit first) I. Get indices of all 1's y bs"0 Bitshift each element in the list with y as left argument. +/ Sum.
Admin
public static long multiply(int a, int b) { long tot = 0; while (a > 0) { if ((a & 1) == 1) tot += b; a >>= 1; b <<= 1; } return tot; }captcha: "iusto" = I have gusto.
Admin
function rpm(x,y){return x?rpm(x>>1,y+y)+y*(x&1):0}
Very tweetable JavaScript indeed! :)
Admin
A solution in Factor programming language:
: 2* ( x -- y ) 1 shift ; inline : rpm ( x y -- x*y ) [ dup 0 > ] [ dup odd? [ over ] [ 0 ] if [ [ 2* ] [ 2/ ] bi* ] dip ] produce sum 2nip ;For integers, it produces the correct result so long as the second argument is non-negative.
Admin
I got a Clojure solution here. (I don't even know if the post is being still watched)
(defn russian-mult "Multiplies two number the russian way. Na sdorowje!" [fnum snum] (loop [left fnum right snum sum 0] (if (= left 1) (+ sum right) (recur (quot left 2) (* right 2) (if (even? left) sum (+ sum right))))))Admin
I wrote it in Befunge. Hopefully nothing was changed when I copied it...
>&&v >01g84*-.@ >:0`!| > ^ >:2%!v v_\:01g+01p\v >v < ^ \*2\/2< ^ <