| « 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 16:59
•
by
qoo3h
(unregistered)
|
|
Is anyone reading this far?
In Perl, implementing the algorithm as described, showing working: #!/usr/bin/perl -w use strict; my $a = shift or die( "need arg 1" ); my $b = shift or die( "need arg 2" ); my $neg = ($a < 0 xor $b < 0) ? -1 : 1; my @nums; for ($a = abs $a, $b = abs $b; $a > 0; push(@nums,{'a'=>$a, 'b'=>$b}), $a >>= 1, $b <<= 1) {} map {print "$_->{'a'} + $_->{'b'}\n" } @nums; print( "###\n" ); @nums = grep {$_->{'a'} % 2} @nums; map {print "$_->{'a'} + $_->{'b'}\n" ;$a += $_->{'b'} } @nums; print( 'sum: ', $a * $neg, "\n" ); |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:03
•
by
Lee Crabtree
(unregistered)
|
|
A Python solution. It could definitely be done in less code, but I'd rather be able to understand it.
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:04
•
by
J.I.
(unregistered)
|
|
Pretty similar in C;
int l(int l1, int ll) { return l1&-2?l1%2*ll+l(l1/2,ll*2):ll; } Looks kind of like a traffic accident, doesn't it? |
|
I didn't check to see if there was an F# one done yet or not. But here is my attempt.
F# Version 1.9.6.16, compiling for .NET Framework Version v4.0.20506 |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:10
•
by
Andrew Rodland
(unregistered)
|
|
I was disturbed by the fact that the Perl uses the multiplication operator when it doesn't need to, so I offer:
Or, moderately golfed: sub rpmul{($r,$i,$j)=(0,@_);$r+=$j&1&&$i,$i<<=1,$j>>=1while$i;$r;}
Or, from a slightly different angle:
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:11
•
by
sdeek
(unregistered)
|
|
python. Gratuitous use of list comprehensions and generators.
Doesn't work for negative numbers. Patches welcome ;-)
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:13
•
by
Michael Clark (mjac.co.uk)
(unregistered)
|
|
fun pmulti 1 r t = t + r
| pmulti l r t = pmulti (l div 2) (r * 2) (t + r * (l mod 2)) pmulti 3 4 0 will give you 3 * 4, t is just an accumulator |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:15
•
by
opussf
(unregistered)
|
|
In Python:
a,b = 190, 232 if a<0: a=-a; b=-b print sum([b<<s for s in xrange(0,int(math.log(a,2)+1)) if ((a>>s>0) and ((a>>s)%2)) ]) |
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:26
•
by
Jimmy Selgen
(unregistered)
|
|
(Readable) Ruby version
Handles negative numbers (and unlike some of the examples i read, returns correct signs) class Rpm |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:29
•
by
Tiogshi
(unregistered)
|
|
For maximum speed, I pre-allocate the tables I use for storage, and avoid all floating-point multiplies and divides. A complete executable program in Lua; run with an argument (any argument) for the peasant-product, without to use the baseline-for-testing "Gold" function.
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:31
•
by
ikegami
|
|
As a circuit diagram
(Handles 4-bit unsigned integers) The selection of input wires for AND gates perform the right shifts. The AND gates perform the oddness test. The selection of input wires for the adders perform the left shifts. Addendum (2009-07-22 23:27): I forgot to mention this was done in MS Paint |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:31
•
by
Eric
(unregistered)
|
|
Take your pick:
(defun multiply (x y) |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:31
•
by
Yorick
(unregistered)
|
|
Speccy basic, recursive:
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:35
•
by
mol1111
|
|
Originally I thought about sending 1235th boring C# version or 436th Python version but then I realized that there is no GUI version (except for few PHP or JS versions with HTML WUI [Web User Interface :-) ]). So I created one using my all-time favourite library: Turbo Vision (so it's really TUI and not GUI). Tested with BP7 and recent FreePascal.
Addendum (2009-07-22 21:05): Download (Source+binaries for DOS and Windows) |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:36
•
by
Edinburgh Mike
(unregistered)
|
|
#!/usr/bin/env python
def rus_mult(A, B): if A == 1: return B else: return rus_mult(A / 2, B * 2) + (A % 2) * B def run_tests(): tests = [(18, 23), (12, 3), (1, 1), (15, 2), (8, 22), (7, 13), (9, 20)] for (A, B) in tests: print "Test", A, "*", B, "Result:", rus_mult(A, B) == A * B if __name__ == "__main__": run_tests() |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:42
•
by
Lernin ur codez
(unregistered)
|
|
C#, handles negative numbers 0, 1.
And, as usual, the most elegant solution is the recursive one.
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:47
•
by
Redredredredredred
(unregistered)
|
|
theres some ruby code for you
def rmul(a,b) |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:48
•
by
Kevin Kofler
(unregistered)
|
|
Here's one in Motorola 68000 assembly:
.xdef __mulsi3 Note that this one is actually useful, it can be used if you don't have a working libgcc for your target. :-) Though that one is faster. ;-) That said, on CPUs like the Z80 which don't have a native multiplication instruction, Russian Peasant is actually extremely useful. |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:52
•
by
Rasmus
(unregistered)
|
|
def Mul(a,b):
return DirectionsFromLeaders( "mul", a, b ) example: > Mul(3,56) : Illegal Question, Caller terminated |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:58
•
by
H2
(unregistered)
|
|
My Perl-solution with optimization, a bit of math and several tests. It also handles negative numbers.
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 17:59
•
by
Ben
(unregistered)
|
Funnily enough, I did the same thing... then realized that is_odd() can be superseded by the simple statement "if (num%2)". Mine prints output, but it's basically the same. I wasn't aware that Python supported +=, though. |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 18:07
•
by
Sam
(unregistered)
|
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 18:08
•
by
PatrickBeebe
|
|
In c#
public static int RussianMultiplication(int x, int y) { if ((y >> 31 | 1) * y < (x >> 31 | 1) * x) y = (x + y) - (x = y); int result = 0; for (result = 0; x != 0; x /= 2, y >>= 1) result += ((x >> 31) | 1) * ((0 - (x & 1)) & y); return result; } Handles 0, negatives, and optimizes which digits to place in which column. And. Fast. Addendum (2009-07-23 13:39): Whoops, that should have been: public static int RussianMultiplication(int x, int y) { if ((y >> 31 | 1) * y < (x >> 31 | 1) * x) y = (x + y) - (x = y); int result = 0; for (result = 0; x != 0; x /= 2, y += y) result += ((x >> 31) | 1) * ((0 - (x & 1)) & y); return result; } |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 18:11
•
by
mwchase
|
|
Bah, one-liners. I figured I'd write my code in true WTF style.
#!/usr/bin/env python Note that I only used arithmetic-related stuff on the lists. Everything else is done bit-wise. See if you can spot the weird abuses and seemingly-random edge cases! (Hint: if there's code that doesn't seem to serve much of a purpose, especially in an if-statement, it's probably deflecting execution around an infinite loop. Either that or I thought it would be funny.) Addendum (2011-01-30 17:48): Looking back on this, it turns out I could have made parts of it even worse. The if-statement in extend, for example, could use instead "min(d.keys())&-2" (or ~1, if you like. The values are equivalent) I think the lesson to take away from this is, if any potential employers decided to see if Google knew about my python experience, this is all a joke. |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 18:11
•
by
LeCoyote
|
|
Two PHP functions in there: the first one does the maths, the second one displays a (very) barren page detailing the steps.
<?php Fun :-) Syntax highlighting was just ... too much free time ;) |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 18:17
•
by
AshG
(unregistered)
|
This one rocks! But you need to expand adders as well, either on a separate sheet or the same one. Also show the transistor logic for each AND gate and adders. |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 18:24
•
by
Florent
(unregistered)
|
|
$ perl -E'($i,$j)=@ARGV;while($i){($i!=int($i/2)*2||!int($i/2))&&($k+=$j);$j*=2;$i=int($i/2)}say$k' 18 23
414 $ |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 18:29
•
by
Adrian
(unregistered)
|
|
Here's my code in python:
|
|
I'm a little disappointed that I haven't seen any submissions in the language actually used to perform Important tasks involving numbers: COBOL.
Tested with OpenCobol 1.0. |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 18:35
•
by
Florent
(unregistered)
|
|
Or shorter :
$ perl -E'($i,$j)=@ARGV;while($i){($i%2||!$i/2)&&($k+=$j);$j*=2;$i=int($i/2)}say$k' 18 23 414 $ |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 18:37
•
by
valderman
(unregistered)
|
|
IMO, the peasants aren't actually multiplying and dividing by two, rather, they're doubling and halving numbers, a less generic but far simpler operation, that's why using * and / is cheating.
Anyway, some more Haskell. This one handles negative numbers (don't think any Haskell entry so far does that,) is tail recursive AND uses type classes! import Data.Bits (shiftL, shiftR, Bits) |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 18:38
•
by
valderman
(unregistered)
|
|
Oops, that second | a < 0 ... should be | b < 0 ...!
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 18:47
•
by
RayS
|
|
Updated from earlier post that I never bothered to updeate correctly.
done in wonderful VBA, and just like in the article, shows full manual working out table in the debug window. Works with negatives, zeros, hopefully everything really. I stuck to the principle and process of the manual process instead of taking shortcuts. Public Function RussianMultiply(x As Long, y As Long) As Long Sample output >RussianMultiply 18,23 |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 18:55
•
by
Fred Dagg
(unregistered)
|
|
package test;
import java.util.ArrayList; import java.util.List; public class RussianMultiplication { public RussianMultiplication() {} public long multiply(long parameter1, long parameter2) { if (parameter2 < parameter1) { return handleMultiplication(parameter2, parameter1); } else { return handleMultiplication(parameter1, parameter2); } } private long handleMultiplication(long lhs, long rhs) { List<RussianNumber> numberList = new ArrayList<RussianNumber>(); numberList.add(new RussianNumber(lhs, rhs)); while (lhs > 1) { lhs = lhs / 2; rhs = rhs * 2; numberList.add(new RussianNumber(lhs, rhs)); } long result = 0; for (RussianNumber number : numberList) { if (number.getLhs() % 2 != 0) { result = result + number.getRhs(); } } return result; } public static void main(String[] args) { RussianMultiplication rm = new RussianMultiplication(); long result = rm.multiply(23, 18); System.out.println("Result: " + result); } private class RussianNumber { private long lhs; private long rhs; public RussianNumber(long lhs, long rhs) { this.lhs = lhs; this.rhs = rhs; } public long getLhs() { return lhs; } public void setLhs(long lhs) { this.lhs = lhs; } public long getRhs() { return rhs; } public void setRhs(long rhs) { this.rhs = rhs; } public String toString() { return lhs + "::" + rhs; } } } |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 18:55
•
by
DFHawthorne
|
|
Another Oracle SQL Example:
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 19:00
•
by
Jukka
(unregistered)
|
|
In Python, aiming for readability.
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 19:04
•
by
mjomble
|
|
How about a good dose of OOM?
The code's too long to post here, but beyond this link you can enjoy it in fully highlighted PHP syntax: http://justas.ggcmedia.com/MultiplicationFramework.html (usage example at the end of the file) Sample output: Multiplying 18 by 23 Russian peasant style: ...hmm, I need to go clean my hands now. |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 19:14
•
by
spiderlama
(unregistered)
|
|
int mul_rus(int a, int b)
{ int c = 0; while (a != 1) { if (a & 1) c += b; a >>= 1; b <<= 1; } return b + c; } |
|
Here's a fairly straightforward implementation in common lisp:
It could be all on one line, but that'd be harder to read. If someone has suggestions on how to simplify it, that'd be great. :) |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 19:24
•
by
Gumpy Guss
(unregistered)
|
|
In PDP-8 assembly language. Untested
X, 0 Y, 0 Z, 0 RUSMUL, 0 // entry point CLA DCA Z / clear total LP, TAD X SNA JMP DONE ROR DCA X SZL CLA TAD Y TAD Z DCA Z TAD Y ROL DCA Y JMP LP DONE, TAD Y TAD Z JMP I RUSMUL |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 19:29
•
by
Crindigo
(unregistered)
|
|
Sloppy JS version with animated step-by-step output: http://crindigo.com/stuff/praxis.html
|
|
Uses NO math operations, only regex and string operations. Almost all major work is kept in the global $_ variable. Numbers are represented internally using a base-30-like system, then converted directly back to decimal at the end of each operation. Some assumptions about collating sequence were made (e.g. \d == [0-9]).
#!/usr/bin/perl |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 19:48
•
by
Paul N
(unregistered)
|
|
--SQL Server using Common Table Expression
DECLARE @number1 int; DECLARE @number2 int; SET @number1 = 18; SET @number2 = 23; WITH Multiplication (Row, Factor1, Factor2) AS ( SELECT 1, @number1, @number2 UNION ALL SELECT Row + 1, Factor1 / 2, Factor2 * 2 FROM Multiplication WHERE Factor1 > 1 ) SELECT SUM(Factor2) FROM Multiplication WHERE Factor1 % 2 = 1; |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 19:53
•
by
Wheaties
(unregistered)
|
|
func(a, b):
if(a == 1) return b if(a%2 == 1) return b*2 + func(a/2, b*2) else return func(a/2, b*2) Had to do it with recursion. I can't think of a reason why not to do it that way. I also haven't looked at the comments. Someone probably came up with this solution or it has already been shown to be wrong. I could also shrink it to be a single line but then it wouldn't be nearly as readable. |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 19:56
•
by
Paul N
(unregistered)
|
|
--It works better as a stored procedure
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Paul N -- Create date: 7/22/2009 -- Description: multiplies two numbers using -- Russian Peasant Multiplication -- ============================================= CREATE PROCEDURE dbo.[Russian Peasant Multiplication] -- Add the parameters for the stored procedure here @number1 int, @number2 int AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here WITH Multiplication (Row, Factor1, Factor2) AS ( SELECT 1, @number1, @number2 UNION ALL SELECT Row + 1, Factor1 / 2, Factor2 * 2 FROM Multiplication WHERE Factor1 > 1 ) SELECT SUM(Factor2) FROM Multiplication WHERE Factor1 % 2 = 1; END GO |
|
open List
let oddcar (n, _) = (n land 1) = 1 let sum = fold_left (+) 0 let rec do_mul = function | (x,_) :: _ as lst when x == 1 -> sum (map snd (filter oddcar lst)) | (x,y) :: _ as lst -> do_mul ((x / 2, y * 2) :: lst) let mul x y = do_mul [x,y] |
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 20:12
•
by
Veryth
|
|
C# + LINQ
Addendum (2009-07-23 01:23): More LINQ-ish:
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 20:12
•
by
Mike McNally
(unregistered)
|
|
simple erlang:
|
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-22 20:19
•
by
bd_
(unregistered)
|
; Rabbit 2000 assembler |
| « 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 » |