{"id":12046,"date":"2015-08-30T16:52:05","date_gmt":"2015-08-30T16:52:05","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=12046"},"modified":"2015-08-30T16:52:05","modified_gmt":"2015-08-30T16:52:05","slug":"mathcomp-sci-another-algorithm-for-exponentiation","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/mathcomp-sci-another-algorithm-for-exponentiation\/","title":{"rendered":"Math\/comp sci:  another algorithm for exponentiation"},"content":{"rendered":"<h1>The tutor follows up his earlier post about an algorithm for exponentiation.<\/h1>\n<p>In my <a href=\"?p=12002\">Aug 28 post<\/a> I brought up exponentiation and a straightforward algorithm that uses a loop.  However, there is a less straightforward, yet more efficient, algorithm for it:<\/p>\n<div style=\"font-family:monospace;color:brown\">\n#!\/usr\/bin\/perl<\/p>\n<p>$orbase=ARGV[0];<span style=\"color:orange\">#reads original base as a param. from function call<\/span><br \/>\n$orexp=ARGV[1];<span style=\"color:orange\">#the original exponent<\/span><br \/>\n$exp=$orexp;<br \/>\n$base=$orbase;<span style=\"color:orange\">#this program changes the exponent and base<\/span><br \/>\n$res=1;<span style=\"color:orange\">#this variable will become the result<\/span><\/p>\n<p>while($exp>0){<\/p>\n<p>if($exp%2==1){<span style=\"color:orange\">#checks if the exponent is odd<\/span><br \/>\n$res*=$base;<br \/>\n}<\/p>\n<p>$exp=int($exp\/2);<span style=\"color:orange\">#for example: int(11\/2)=5<\/span><\/p>\n<p>if($exp>0){<br \/>\n$base*=$base;<br \/>\n}<\/p>\n<p>}<span style=\"color:orange\">#end while loop<\/span><br \/>\nprint &#8220;$orbase to the exponent of $orexp is $res\\n\\n&#8221;;\n<\/div>\n<p>Let&#8217;s assume this file is named <span style=\"font-family:monospace\">exp.txt<\/span>.  To call it and ask it to evalute <b>2<\/b><sup style=\"font-size:14px\">10<\/sup>, one would type at the command prompt<\/p>\n<p><span style=\"font-family:monospace\">perl exp.txt 2 10<\/span> <\/p>\n<p>This algorithm is more efficient than the looping one because it can square the base, then halve the number of times it needs to be multiplied by itself.  Furthermore, it can separate the expression into even and odd segments, then tackle the even one(s) as just described.<\/p>\n<p>I&#8217;ll be talking more about the details of this algorithm in future posts:)<\/p>\n<p>Source:<\/p>\n<p>Grimaldi, Ralph P.  <u>Discrete and Combinatorial Mathematics<\/u>.  Addison-Wesley:  Toronto,<br \/>&nbsp;&nbsp;1994.<\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tutor follows up his earlier post about an algorithm for exponentiation. In my Aug 28 post I brought up exponentiation and a straightforward algorithm that uses a loop. However, there is a less straightforward, yet more efficient, algorithm for &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/mathcomp-sci-another-algorithm-for-exponentiation\/\"> <span class=\"screen-reader-text\">Math\/comp sci:  another algorithm for exponentiation<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105,3],"tags":[1019,1018],"class_list":["post-12046","post","type-post","status-publish","format-standard","hentry","category-computer-science","category-math","tag-efficiency","tag-exponentiation-algorithm"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/12046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=12046"}],"version-history":[{"count":32,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/12046\/revisions"}],"predecessor-version":[{"id":12078,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/12046\/revisions\/12078"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=12046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=12046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=12046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}