| « Bourne Into Oblivion | Support Should Never Be Necessary » |
Ever since the first OMGWTF Programming Contest, I've always wanted to bring back some element of "coding challenges" to the site. Ideally, this would be in the form of a second contest... but considering that contests require a ton of work, and the fact that interns around town have come to learn that interning at Inedo basically mean means shipping mugs, mailing stickers, testing contest entries, and acting as human ottomans, we'll have to go with something a bit scaled back. And that's where Programming Praxis will come in.
The goal of Programming Praxis is simple: provide an outlet for you, the enquiring software developer, to sharpen your programming skills on a problem a bit more interesting than the normal, boring stuff. That, and to put your code where you mouth is, so to say. There is no “right” answer and no perfect solution, but some will certainly be better than others. The best of these will get a TDWTF sticker.
So, without further ado, here is your first Programming Praxis.
"It is said that Russian peasants multiply using a most curious method," writes Phil Bewig. "They start by writing the two numbers to be multiplied at the head of two columns. Then they repeatedly divide the number in the left column by two (dropping any remainder) and double the number in the right column, writing the two new numbers immediately below their predecessors, until the number in the left column is one. Then they cross out all rows where the number in the left column is even, and add the remaining numbers in the right column, which is the desired product. For instance, the product eighteen times twenty-three is found like this."
"It is easy to see why this method works if you use the grade-school method of multiplication, but with binary numbers instead of decimal numbers."
10010 18
x 10111 x 23
------- -----
00000 0 x 1 x 23 0
101110 1 x 2 x 23 46
0000000 0 x 4 x 23 0
00000000 0 x 8 x 23 0
+ 101110000 1 x 16 x 23 + 368
----------- ------
= 110011110 = 414
"In binary," Phil continues, "18 is 1x24 + 0x23 + 0x22 + 1x21 + 0x20. The odd numbers in the left column correspond to the 1 bits in the binary representation of the multiplicand."
Your challenge: write a function that multiplies two numbers using the Russian peasant algorithm. There is no language restriction, though anything on the esoteric language list will probably be ignored. Spoiler alert: the solution(s) will undoubtedly appear in the comments.
Re: Programming Praxis: Russian Peasant Multiplication
2009-07-24 20:34
•
by
mol1111
|
|
Updated version of index. Added entries from yesterday, fixed jnz's entry, resolved most entries without specified language, removed duplicates.
ABAP dv 277800 Someone 277998 278677 APL Captain Obvious 277817 Steve the Cynic 278029 278054 Assembler Dascandy x86, ARM 277793 Bosshog 6502 277804 278103 Stalker x64 277911 kastein IA64 277930 Bob Montgomery 6502 278147 Lunkwill m68k 278154 flax ARM 278263 Kevin Kofler m68k 278381 Gumpy Guss PDP-8 278418 bd_ Rabbit 2000 278438 Chris Walton x86 278499 C x86 278562 Anonymous ARM 278710 Moe 6502 279053 curtmack 6502 279343 BASIC Alex Papadimoulis VBScript 277748 Kees QB/VB ? 277803 Takis VB.Net 277814 278799 RayS VBA 277842 278407 djjeavons VB.Net 277887 Slinky VB? 278001 antelopelovefan VBA 278068 JoLoCo Sinclair 278126 Yorick Sinclair 278372 bluebearr AutoIt 278460 James VBA 278489 Don Knisley VBA 278534 k1 278548 278550 some VBA coder VBA 278862 Nix Nada VBA 279306 bc/dc Nick Pope 277951 278207 brainfuck Qwertyuiopas 278037 Eyrieowl 278241 Philipp 278837 Boombuler 279204 Brilliant Paula 277801 C/C++ Tim 277783 277784 Larry H. 277798 Stefan 277813 Zombywuf templates 277815 Matthew Verleger 277827 darkwraith 277832 KimmoS 277850 KnoNoth 277851 whileloopsareugly 277873 TerraNova 277876 YES WE CAN! 277912 little3lue templates 277914 GWO 277924 277962 277964 277965 avl 277929 ponky 277952 278066 hh10k 277969 buzkie 277984 shub 277985 278055 _flt 278003 Harleqin 278009 Zor 278046 278053 278059 Kman templates 278062 278880 Alex Guzman 278084 Fooba 278100 serg 278113 hornet 278173 278191 Bored 278175 xtremezone 278176 278190 278205 David Young 278185 Haggis 278189 Ronuk Raval 278209 Humanoid Life-form 278238 Alan Woodland templates 278240 Михаил 278267 OmnipotentEntity 278270 MG 278284 Resistance Assembler 278321 Rob Hulswit templates 278329 278331 mxsscott 278342 Rob H 278352 J.I. 278356 Sam 278391 spiderlama 278416 0ffh 278451 jnz 278473 Chris Walton 278499 demo templates 278528 khahem 278731 Mr. Black 278769 n1L templates 278844 markwhi 278906 278908 Lightnix 278994 TP 279023 C# Jay 277786 Dopefish 277788 Brettm 277812 ruckc 277828 Dan 277834 277856 Damien 277843 Felix Ungman 277867 GM 277894 cwink 277899 277905 Fabio Lima 277922 277957 Jonathan 277967 Osno 277972 277997 278021 groknroll 277980 buzkie 277984 campkev 277991 277994 278078 278081 Sean Handley 277995 Oliver Klozoff 278000 WraithShade 278057 Floyd LINQ 278082 Alex Guzman 278084 chriswatson LINQ 278106 Codism LINQ 278139 Jared Youtsey 278149 David C. 278161 278226 jeff.woodhull LINQ 278201 It'sMe 278203 Shiftyness 278236 Humanoid Life-form 278238 aef123 278245 278247 stannius LINQ 278261 blueberry 278286 VirtualBlackFox LINQ 278292 nine 278293 TNProgrammer 278302 Josue Chi 278303 dontera 278324 The Answer + 2 278328 hornet threads 278340 Rob H 278352 Lernin ur codez 278376 PatrickBeebe 278393 278974 Veryth LINQ 278433 brian 278450 Thomas Eyde 278456 Joel 278461 joeyadams 278471 JXO 278474 astander 278544 col obvious 278598 Alex 278628 czetts 278846 HCGL 278911 Jeremy Burns 278922 Paul N 278983 Tachyon ASP.Net 279255 Jason 279280 D Rief 278006 Quxxy 278069 Erlang Mike McNally 278434 278440 MichaelWH 278877 John Stracke 279008 F# darklajid 277890 277941 278137 Patrick Simpson 278127 ajp 278357 279029 cmugford 278486 FORTRAN rst 277978 java.lang.Chris; 278266 Groovy Matt 277949 Haskell freako 277838 Dave Ingram 277839 Sebastian Paaske Tørholm 277870 HypocriteWorld 277878 Maxim 277889 Noah Easterly 278130 semanticprecision 278170 Andrew Nurse 278198 Jonas Kramer 278220 ikorb 278299 valderman 278405 278406 Twey 278439 Joost 278567 jefu 278838 dub 278874 iggy 278935 vog 279045 MichaelWH 279047 dysfunctor 279081 Cale Gibbard 279334 Java Cookie 277785 Jay 277786 Mat Rantell 277802 ruckc 277828 Will 277831 Drew 277844 Arjan 277879 Philipp 277903 277919 Kook 277963 277968 buzkie 277984 Alex Guzman 278084 imhe 278105 278138 Jannik Jochem 278115 Thuktun 278124 idle 278128 klagdon 278131 278242 amischiefr 278152 Greg R 278212 278231 Davi Cavalcanti 278228 DeepThought 278230 bb 278235 cincinnatvs 278237 Humanoid Life-form 278238 aef123 278245 278247 Josue Chi 278303 Fred Dagg 278408 subanark 278537 astander 278544 col obvious 278598 Alex 278628 Rorick 278749 Queex 278761 Coyne 278892 Javascript Z 277796 Zishan 277882 Jason Knight 277898 Mestafais 278005 Markus Persson 278039 jjs105 278136 jonnyq 278208 Phroggy 278221 astonerbum 278248 278264 278343 Scott 278294 Beat by the system 278319 Rob Hulswit 278329 Jeb Walter Strate 278345 Crindigo 278419 Chris 278484 cofiem 279233 Ed 279287 JS ROX! 279340 LISP newlisp.org newLISP 277861 278283 MP 2778711 leppie Scheme 277881 dee Scheme 277900 Bob Scheme 277907 278206 Éibhear Emacs 277910 Dmitri Savichev Scheme 277992 277999 lyml Scheme 277996 Harleqin Common Lisp 278009 Alex Muscar Common Lisp 278090 278096 278160 278514 278516 Trey Jackson Emacs 278194 samanddeanus Scheme 278271 278998 asdfghj Scheme 278311 guille_ 278317 Ted Walther, newLISP fan newLISP 278344 278986 unit3 Common Lisp 278417 noSignal Scheme 278469 iamtim2 newLISP 278491 jvmbsd7 Scheme 278493 Chris Judge 278868 LOLCODE JaguarOne 277920 PopeHappyCat 278099 279245 Lua Stefan 277854 Will 278010 Tiogshi 278368 Methuselah 278675 Kenneth Pullen 279099 Mathematica J 278467 Joel Klein 279356 MSIL Osno 278153 278197 278729 278786 278801 MUMPS MUMPS 278211 Trurl 278246 278255 Other hydrus Clojure 277792 db2 HP 48 277857 Dave Ingram vimscript 277862 pjt33 SML 277864 SR ColdFusion 277896 Will MUSH 277942 Samuel A. Falvo II Forth 277976 Sal Postscript 278093 Cindi Knox mIRC 278174 darkscout Matlab 278298 Roger RPGLE 278337 Tom Medley - www.tommedley.com ML 278350 ikegami circuit diagram 278370 dee COBOL 278403 yowzer ocaml 278428 StarLite Progress V9 278583 Ralf Smalltalk 278794 Rorick Rebol 278810 278873 Whatever Befunge-93 278988 Rusty Nail Verilog 279040 Greg awk 279087 Marlon False 279219 279220 279225 Corpsegrinder AppleScript 279236 279237 Pascal Azeroth 277837 malach Delphi 277860 baka0815 277868 277877 278020 278028 278065 DougB Delphi 278179 Trevor Dubinsky Delphi 278229 mol1111 Turbo Vision 278374 Perl Me 277807 277845 epv 277849 Steven de B 277884 WarheadsSE 277944 278061 Anonymous 277977 tirerim 278119 John Evans 278141 Jim Halfpenny 278143 Igor V. 278199 Darren 278223 Warr regex 278260 278420 qoo3h 278353 Andrew Rodland 278358 H2 278388 Florent 278401 278404 Toby Gray 278538 278540 bugmonkey 278658 rjp 278714 Maurits 279046 Yorick 279222 asd 279246 curtmack 279331 PHP The Wolf 277787 277799 Erin 277797 Clark S 277858 277872 Ryan 277863 Daniel 277904 277909 FatherStorm 277932 aBase 278036 278214 John Evans 278141 HonoredMule 278158 278168 278184 IcePanther 278195 Tim 278196 Paul 278265 LeCoyote 278396 mjomble 278413 mneimeyer 278524 LatecomerX 278617 278673 bugmonkey 278658 Evo 278886 278934 AngeloR. 278960 symcbean 279208 Prolog Anonymous Coward 277938 Mike5 278204 A.T. 278641 278646 279196 Corpsegrinder 279051 Python ath 277789 277806 278244 phihag 277811 277818 277869 Fenris 277823 Jeff Kemp 277852 277940 Tempura 277875 Stalin 277880 Jim 277901 Eutactic 277915 RECURSIVEMAN 277916 rob tracey 277917 krat 277918 drBeat 277925 ross 277931 halber_mensch 277936 277943 277955 277979 278129 Real russian peasant :) 277974 Remy 278004 Billy 278007 Josh Bohde 278017 278080 278092 278104 278166 278854 Wulf0r 278024 Jürgen 278086 koblas 278101 Rob W. 278111 Chad M 278132 Jörg 278163 martinp 278172 JJM 278193 Ronuk Raval 278209 lostlogic 278222 SoaperGEM 278305 ZzzZZZz 278318 Lee Crabtree 278355 sdeek 278359 opussf 278364 KukkerMan 278366 Edinburgh Mike 278375 mwchase 278395 Adrian 278402 Jukka 278412 tdh 278444 Mr.'; Drop Database -- 278459 agrif 278497 Python 278688 Lom 278732 the real wtf fool 278842 workmad3 278975 inky 278996 Scott 279009 kramthegram 279290 279292 279297 raony 279352 Ruby arnemart 277825 277855 Mike Dotterer 277829 exo 277926 Nicholas Laux 277958 277971 Xthlc 277983 278026 derula 278050 Amadan 278071 Northpole 278072 jweir77 278079 Jürgen 278086 Dave Havlicek 278122 Jeremy Weathers 278216 Chewbie 278224 Jimmy Selgen 278367 Redredredredredred 278380 Pony Princess 278967 Scala Alex Ciarlillo 277937 Ian McLaird 278023 Matt 278249 Unlabeled Meat 278332 juancn 278962 Landei 279042 Moritz 279082 Shell mat BASH 277791 bluebearr Batch 278012 278243 Ken B Batch 278032 moltonel BASH 278262 278272 278282 Julian Calaby BASH 278485 The_Assimilator PowerShell 279028 andyesslinger Batch 279049 Spreadsheet acsi 278150 treyb Excel 278300 Joe Excel 279333 SQL SQLDork 277795 KnowOracle Oracle 277816 277822 277824 Chris Hunt Oracle 277836 csulli13 277888 MeesterTurner T-SQL 277959 wombat T-SQL 278186 shane blake T-SQL 278210 Dave Havlicek PL/pgSQL 278322 Bodestone 278341 DFHawthorne Oracle 278409 Paul N T-SQL 278425 278427 Hidayat Oracle 278526 SlyEcho 278535 Boneist Oracle 278555 EJCorcoran T-SQL 278795 dee MySQL 279007 DBA_In_OKC T-SQL 279145 Tcl/Tk Albrecht from Germany 278142 278145 Unspecified SimonY 278120 Michael Clark (mjac.co.uk) 278360 Eric 278371 Wheaties 278426 XSLT Dave van der Brugge 278133 wombat 278808 Addendum (2009-07-24 20:53): BTW If anybody (Alex?) interested, here are sources for the index generator (datafile + python script). |
| « Bourne Into Oblivion | Support Should Never Be Necessary » |