- 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
A couple of more solutions with Matlab. A solution, which follows the specifications pretty much to the letter:
And another solution for matrix input (works as Matlab function times would with positive integers):
Admin
Ok, I found a very small improvement to my earlier progs. Function:
1 char less, for 49 and 70 respectively...Not a function:
One char less, 34 chars for the algorithm, 61 for the one-liner.I actually thought / wouldn't return an int and bit shift had to be used, for some reason. Go me.
Admin
Admin
Admin
A little PDP-8 love :)
*200
MAIN, CLA CLL
LOOP, CLA CLL TAD ONE AND FACTA SNA JMP NOPE
CLA CLL TAD FACTB TAD OUT DCA OUT
NOPE, CLL TAD FACTB RAL DCA FACTB
CLL TAD FACTA RAR CLL SNA JMP NOMAS
DCA FACTA JMP LOOP
NOMAS, HLT
ONE, 0001 FACTA, 0010 FACTB, 0010 OUT, 0000
Admin
Here is Ruby solution that takes two arguments on the command line and prints their product on the standard output. Both arguments have to be non-negative, but they can be zero and it's no problem.
Here is a solution in PIC assembly, which will run on the 8-bit PIC18F14K50 processors that my company uses in our products The language is defined on page 306 of the PIC18F14K50 datasheet. The function peasantMultiply multiplies two 8-bit unsigned integers and stores the 16-bit unsigned result in PRODH:PRODL. The arguments to the function should be placed in pMultA and pMultB. A temporary register pMultBH is used so that we can have a 16-bit unsigned integer pMultBH:pMultB that stores the doubled value of B. The algorithm here is essentially the same as the Ruby code above, except that dividing A by two is done at the same time that we are testing whether it is odd or not.
So that's 15 instructions. I challenge anyone to come up with a smaller solution!
--David Grayson DavidEGrayson.com
Admin
Python solution that probably exactly matches one in the last 15 pages of submissions, but I couldn't be bothered looking:
Admin
Havn't seen any solutions in Labview. Lets see if the pic works:
http://share.ovi.com/media/zeppelin-79.public/zeppelin-79.10204
Admin
a href="http://share.ovi.com/media/zeppelin-79.public/zeppelin-79.10204">Peasant Multiplication - Share on Ovi
Admin
I am disturbed by the number of implementations (Alex's included!) that will obviously fail when asked to calculate 11 (or 00). But here's mine:
I read some of the comments after writing this, and wished I had considered the (elegant) recursive solution too. I believe my solution works for NUM1>=0 and any NUM2.
Admin
ah well. just read that article and even tho its late i will still submit the work of 2 minutes :)
Admin
Linq is lazy (just as F#). So you can use .Range to generate an infinite list and stop when left goes 0.
C#4 using bigintegers (so it makes "sense" to generate an infinite list):
Admin
A C# solution with yield return.
Admin
Lunkwill: 68k entries are commendable, I tried to optimize it a bit but bear in mind that I haven't programmed 68k in 15 years.
Untested ofc.
Admin
My attempt to at the shortest possible solution in Perl:
It's just too bad that there has to be a verbose initialization step :/
Admin
I believe this is a new record for number of comments. And it's definitely a new record for the ratio of genuine comments to spam/troll/rubbish comments.
Admin
As others seem to be going for shortest, here is my super-short 1-liner in python (which is the same as what I've posted previously, but without the embedded function required to decorate the function and allow full negative multiplication ;))
This will cause infinite recursion if called with a negative number for a though...
Admin
Version in Baltie 3: [image]
Admin
Written in Java, this function works for any two ints, returning the same value as normal multiplication would. (Though I have to admit, I didn't try all possible combinations to verify this claim.) A trivial command-line interface is provided, which, I'm sad to say, isn't quite as fool-proof as the implementation itself.
Admin
Admin
And specially for RedAdder, an improved function, non-semicolon version...
Func:
47 chars!And the one liner, no semicolons:
68 chars!Again thanks to Goujon for opening my eyes. I am not worthy :).
Admin
Guido van Robot version (not optimal):
The world should look like this:
12 and 13 are the numbers which will be multiplied. 500 is just big enough buffer of beepers. The result will be put atposition 2 3 (above 12 and 13).
Admin
I have also version for karelNB, but that's limited for results <= 8.
mult.kar:
Town example:
mult.kat:
Addendum (2009-07-27 20:07): Image of a successful run: [image]
Admin
C# (4.0):
Addendum (2009-07-27 21:37): Or:
Admin
But no Ada`?
Admin
Here's my version using vba - so at least loads of you can run it. I've sped it up by checking which no. is largest so it doesn't loop for ages.
'Function to do multiplication like a russian peasant Private Function MultiplyLikePeasant(ByVal n1 As Long, ByVal n2 As Long) As Long
End Function
Private Sub CommandButton1_Click() MsgBox MultiplyLikePeasant(18, 317) End Sub
Admin
btw it was indented when i previewed it...
Admin
Surround your code pastes with code tags. See http://en.wikipedia.org/wiki/BBCode.
Admin
Using VbScript ... the "Incredible Machine" sorta way
Admin
Here's my version:
int russian_multiply(int a, int b) { return a*b; }
On most hardware multipliers, this will be done with the peasant multiplication algorithm anyway :)
Admin
My version is C. The difference between mine and most of the others is:
Only bug is that 1*1 (and -neg) doesn't work. This is all noted in the function comment.
Note that I only hacked this for like an hour, when there was a lull in Real Work, and I didn't get much sleep last night, so there might be something I missed.
Code follows.
Admin
Much shorter version:
Admin
I know, I know, boring old Java. I did it in C first, but didn't want to embarrass myself too badly.
Admin
peasant = sum . map snd . filter (odd . fst) . fix (\f xs@((a,b):_) -> if a == 1 then xs else f $ (a
div
2,b * 2) : xs) . (:[])Admin
This almost 100% matches the * operator. I did get some differences when dealing with int.MaxValue and int.MinValue multiplications.
// C#
public int MultiplyLoop(int x, int y) { int result = 0; int sign = x ^ y; x = (x ^ (x >> 31)) - (x >> 31); y = (y ^ (y >> 31)) - (y >> 31);
}
Admin
private static int Multiply(int x, int y) { return x == 1 ? y : Multiply(x / 2, y * 2) + x % 2 * y; }
Admin
Ignore my previous post.
Updated version with zero and negative value support:
public static int Multiply(int x, int y) { return (x % 2 * y) + (Math.Abs(x) > 1 ? Multiply(x / 2, y * 2) : 0);
}
Admin
C
int rpmul(int a, int b) { int c = 0; for (; a >= 1; (a % 2) ? c += b : 0, a /= 2, b *= 2); return c; }
Admin
I found this one after the one about Josephus's circle. This one seemed much easier. Here it is in C#:
public static int peasantRiddle(int left, int right) { int leftVal = 0; int rightVal = 0; int result = 0; List<int> leftSide = new List<int>(); List<int> rightSide = new List<int>();
Admin
I know its a bit late, but I'd like to add a Powershell version.
Admin
I thought it was more fun to include the running status with this
Admin
An Error Occured (again)...
but... I'm pretty sure we can reach 750 comments on this article if the damn thing lets me post...
Admin
Try this...
(Common Lisp)
Admin
Try this...
(Common Lisp)
(rm -18 23)
Admin
Here it is in Whitespace:
(hahahahahahahahahahhaahaha!)
Admin
Congrats on reaching 750 comments everyone, this is a new record!
Admin
Works for negative numbers, 11, 00, etc... No validation of input, though...
Admin
Mine is late because I've only just discovered thedailywtf, so I naturally don't deserve a sticker, but hopefully the other 6502-heads will enjoy this:
Admin
Granted, it's whitespace, and it runs... But pushing 0s to the stack is not what this Praxis is about... It's about multiplying to numbers using a specific algorithm...
Here's one that actually works: (I hope the forum software doesn't screw it up. It looks good in Preview)
--
Admin
Oops... Should be
Serves me right for not copy/paste-ing