{"id":368,"date":"2022-06-19T13:49:48","date_gmt":"2022-06-19T13:49:48","guid":{"rendered":"https:\/\/baldsolutions.com\/?p=368"},"modified":"2022-06-19T13:49:49","modified_gmt":"2022-06-19T13:49:49","slug":"square-and-multiply-less-computations","status":"publish","type":"post","link":"https:\/\/baldsolutions.com\/index.php\/2022\/06\/19\/square-and-multiply-less-computations\/","title":{"rendered":"Square and multiply &#8211; less computations"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Introduction<\/h1>\n\n\n\n<p>Computing large numbers isn&#8217;t usually our day-to-day issue. We use multiplication when it comes to finding out how much of a paint would be enough to cover walls in our room. However, computers have to deal with multiplication and exponentiation on a regular basis. These days many cryptography formulae require to deal with large numbers computations. One of the key factor in cryptography is to make it fast and efficient enough so that it could be applied for an ordinary user.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Large exponentiation<\/h1>\n\n\n\n<p>Let&#8217;s consider the following example. We have an algorithm that at some point needs to compute the following: 7<sup>30000<\/sup>. This is definitely a big number, however. Firs approach to that would be basic multiplication like 7 * 7 * 7 * &#8230; * 7 (30000 multiplications). It seems to be a good enough but as I&#8217;ve mentioned earlier the efficiency is crucial.<\/p>\n\n\n\n<p>Now, let&#8217;s do the same but in a slightly different approach  &#8211;  using square and multiply algorithm. As you probably know from math classes the following formulae are true: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>7<sup>11<\/sup> = 7 <sup>10<\/sup>  * 7 <sup>1<\/sup><\/li><li>7 <sup>10<\/sup> = 7 <sup>5<\/sup> * 7 <sup>5<\/sup><\/li><\/ul>\n\n\n\n<p>Having that in mind we can compute the 7 <sup>30000<\/sup> as follows (I prepared a simple program that prints to operations for given base and exponent):<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"339\" height=\"370\" src=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2022\/06\/image.png\" alt=\"\" class=\"wp-image-372\" srcset=\"https:\/\/baldsolutions.com\/wp-content\/uploads\/2022\/06\/image.png 339w, https:\/\/baldsolutions.com\/wp-content\/uploads\/2022\/06\/image-275x300.png 275w\" sizes=\"auto, (max-width: 339px) 100vw, 339px\" \/><\/figure>\n<\/div>\n\n\n<p>As you can see instead of 30000 multiplications we are able to do the same exponentiation in just 20 operations (we can ignore the first line)! I decided not to explain the algorithm in details<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Summary<\/h1>\n\n\n\n<p>To sum up, the  number of operations have a direct impact in terms of program performance. I didn&#8217;t explain here the exact algorithm&#8217;s steps as I believe that the above image explain the main idea of the algorithm well. I also encourage to read <a href=\"https:\/\/en.wikipedia.org\/wiki\/Exponentiation_by_squaring\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"this \">this <\/a>Wikipedia page. Moreover, I&#8217;ve got inspired by <a href=\"https:\/\/www.youtube.com\/watch?v=cbGB__V8MNk&amp;ab_channel=Computerphile\" target=\"_blank\" rel=\"noreferrer noopener\">this<\/a> Computerphile video &#8211; I truly recommend this YouTube channel, there are many well-explained and interesting things to learn.<\/p>\n\n\n\n<p>As always I&#8217;ve prepared an example and you can find it on my <a href=\"https:\/\/github.com\/tglowka\/baldsolutions\/tree\/master\/.NET\" target=\"_blank\" rel=\"noreferrer noopener\">github<\/a>, project: <a href=\"https:\/\/github.com\/tglowka\/baldsolutions\/tree\/master\/.NET\/square-and-multiply\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"\">square-and-multiply<\/a>.<\/p>\n\n\n\n<p>Have a nice day, bye!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Computing large numbers isn&#8217;t usually our day-to-day issue. We use multiplication when it comes to finding out how much of a paint would be&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/baldsolutions.com\/index.php\/2022\/06\/19\/square-and-multiply-less-computations\/\">Continue reading<span class=\"screen-reader-text\">Square and multiply &#8211; less computations<\/span><\/a><\/div>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-368","post","type-post","status-publish","format-standard","hentry","category-net","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/comments?post=368"}],"version-history":[{"count":8,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/368\/revisions"}],"predecessor-version":[{"id":377,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/368\/revisions\/377"}],"wp:attachment":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/media?parent=368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/categories?post=368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/tags?post=368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}