{"id":444,"date":"2009-11-27T10:10:30","date_gmt":"2009-11-27T17:10:30","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2465"},"modified":"2009-11-27T10:10:30","modified_gmt":"2009-11-27T17:10:30","slug":"egyptian-multiplication","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=444","title":{"rendered":"Ancient Egyptian Multiplication"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\negyptianMultiply(12, 99);\r\n\r\n\/\/ trace(12 * 99) \/\/ test to make sure it works\r\n\r\n\/* outputs:\r\n\/64 768\r\n\/32 384\r\n 16 192\r\n 8 96\r\n 4 48\r\n\/2 24\r\n\/1 12\r\n---\r\n1188\r\n*\/\r\n \r\n\r\nfunction egyptianMultiply(valueA:Number, valueB:Number):void {\r\n\t\r\n\tvar left:Array = &#x5B;];\r\n\tvar right:Array = &#x5B;]\r\n\t\r\n\t\/\/ swap if valueB is smaller than value A\r\n\tif (valueB &lt; valueA){\r\n\t\tvar swap:int = valueB;\r\n\t\tvalueB = valueA;\r\n\t\tvalueA = swap; \r\n\t}\r\n\t\r\n\t\/\/ create left and right columns\r\n\tvar currentLeft:int = 1;\r\n\tvar currentRight:int = valueA;\r\n\t\r\n\twhile (currentLeft &lt; valueB){\r\n\t\tleft.push(currentLeft);\r\n\t\tright.push(currentRight);\r\n\t\tcurrentRight += currentRight;\r\n\t\tcurrentLeft += currentLeft; \r\n\t}\r\n\t\r\n\t\r\n\t\/\/ add up the right column based on the left\r\n\tcurrentLeft = 0;\r\n\tvar rightSum:int;\r\n\tvar leftSum:int;\r\n\tvar i:int = left.length - 1;\r\n\t\r\n\twhile (currentLeft != valueB){\r\n\t \r\n\t  leftSum = currentLeft + left&#x5B;i];\r\n\t  \/\/ if the next number causes the sum\r\n\t  \/\/ to go above valueB, skip it\r\n\t  if (leftSum &lt;= valueB){\r\n\t\tcurrentLeft = leftSum;\r\n\t\trightSum += right&#x5B;i];\r\n\t\ttrace(&quot;\/&quot; + left&#x5B;i]  + &quot; &quot; + right&#x5B;i]);\r\n\t  } else {\r\n\t\ttrace(&quot; &quot; + left&#x5B;i]  + &quot; &quot; + right&#x5B;i]);\r\n\t  }\r\n\t  i--;\r\n\t}\r\n\ttrace(&quot;---&quot;);\r\n\ttrace(rightSum);\r\n}\r\n<\/pre>\n<p>Someone mentioned egyptian multiplication to me yesterday&#8230; So read a little about it <a href=\"http:\/\/www.jimloy.com\/egypt\/mult.htm\" target=\"blank\">here<\/a> and whipped up this example. For some reason I decided to do it in processing &#8230; once it worked I ported it to ActionScript.<\/p>\n<p><em>The above link describing the technique I used is from <a href=\"jimloy.com\">http:\/\/www.jimloy.com\/<\/a>&#8230; I haven&#8217;t spent much time digging around the site, but it appears to have some pretty nice stuff on it&#8230;<\/em><\/p>\n<p>If you&#8217;re curious, here is the original processing version:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nVector left = new Vector();\r\nVector right = new Vector();\r\n\r\nint valueA = 10;\r\nint valueB = 8;\r\n\r\nif (valueB &lt; valueA){\r\n    int swap = valueB;\r\n    valueB = valueA;\r\n    valueA = swap; \r\n}\r\n\r\nint currentLeft = 1;\r\nint currentRight = valueA;\r\nwhile (currentLeft &lt; valueB){\r\n    left.add(currentLeft);\r\n    right.add(currentRight);\r\n    currentRight += currentRight;\r\n    currentLeft += currentLeft; \r\n}\r\n \r\ncurrentLeft = 0;\r\n \r\nint result = 0;\r\nint i = left.size() - 1;\r\nwhile (currentLeft != valueB){\r\n \r\n  int temp = currentLeft + (Integer) left.get(i);\r\n  if (temp &lt;= valueB){\r\n    \r\n    currentLeft = temp;\r\n    result += (Integer) right.get(i);\r\n    println(&quot;\/&quot; + left.get(i) + &quot; &quot; + right.get(i));\r\n  } else {\r\n    println(&quot; &quot; + left.get(i)  + &quot; &quot; + right.get(i));\r\n  }\r\n    \r\n  i--;\r\n}\r\nprintln(&quot;---&quot;);\r\nprintln(result);\r\n<\/pre>\n<p>After writing, this I took a look at the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ancient_Egyptian_multiplication\" target=\"blank\">wikipedia entry<\/a>&#8230; I also found myself on <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ahmes\" target=\"blank\">this short page about a scribe called Ahmes<\/a>. (I recommend reading this if you are interested in PI)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>egyptianMultiply(12, 99); \/\/ trace(12 * 99) \/\/ test to make sure it works \/* outputs: \/64 768 \/32 384 16 192 8 96 4 48 \/2 24 \/1 12 &#8212; 1188 *\/ function egyptianMultiply(valueA:Number, valueB:Number):void { var left:Array = &#x5B;]; var right:Array = &#x5B;] \/\/ swap if valueB is smaller than value A if (valueB [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121,94],"tags":[3,14,4,123],"class_list":["post-444","post","type-post","status-publish","format-standard","hentry","category-math","category-misc","tag-actionscript","tag-as3","tag-flash","tag-processing"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=444"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/444\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}