{"id":3284,"date":"2012-10-03T06:52:15","date_gmt":"2012-10-03T05:52:15","guid":{"rendered":"http:\/\/emilkirkegaard.dk\/en\/?p=3284"},"modified":"2012-10-03T06:52:15","modified_gmt":"2012-10-03T05:52:15","slug":"an-alternative-way-to-calculate-squares-without-using-multiplication","status":"publish","type":"post","link":"https:\/\/emilkirkegaard.dk\/en\/2012\/10\/an-alternative-way-to-calculate-squares-without-using-multiplication\/","title":{"rendered":"An alternative way to calculate squares.. without using multiplication"},"content":{"rendered":"<p>I was once at a party, and i was somewhat bored and i found this way of calculating the next square. It works without multiplication, so its suitable for mental calculation.<\/p>\n<p>Seeing that i have recently learned python, here&#8217;s a python version of it:<\/p>\n<pre>n = 10 # how many sqs to return\r\n\r\nb = []\r\ndef sq(x):\r\n\u00a0\u00a0\u00a0 return x*x\r\nfor y in range(1,n):\r\n\u00a0\u00a0\u00a0 print sq(y)\r\n\u00a0\u00a0\u00a0 b.append(sq(y))\r\n\r\n\r\ndef sqx(x):\r\n\u00a0\u00a0\u00a0 if x == 1:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return 1\r\n\u00a0\u00a0\u00a0 if x == 2:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return 4\r\n\u00a0\u00a0\u00a0 return (sqx(x-1)-sqx(x-2))+sqx(x-1)+2\r\n\r\na = []\r\nfor y in range (1,n):\r\n\u00a0\u00a0\u00a0 print sqx(y)\r\n\u00a0\u00a0\u00a0 a.append(sqx(y))<\/pre>\n<p>In english. First, set the first two squares to 1 and 4, since this method needs to use the two previous squares to calculate the next. Then calculate the absolute difference between these two. Suppose we are looking for 3<sup>2<\/sup>, so previous two are 1 and 4. Abs diff is 3. Add 2 to this, result 5. Add 5 to previous square, so 4+5=9. 9 is 3<sup>2<\/sup>.<\/p>\n<p>I have no idea why this works, i just saw a pattern, and confirmed it for the first 20 integers or so.<\/p>\n<p>In the code above, i have defined the function recursively. It is much slower than the other function. I suppose both are slower than the low-level premade function pow(n,m). But it certainly is cool. :P<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was once at a party, and i was somewhat bored and i found this way of calculating the next square. It works without multiplication, so its suitable for mental calculation. Seeing that i have recently learned python, here&#8217;s a python version of it: n = 10 # how many sqs to return b = [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1766],"tags":[],"class_list":["post-3284","post","type-post","status-publish","format-standard","hentry","category-math-science","entry"],"_links":{"self":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/3284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/comments?post=3284"}],"version-history":[{"count":1,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/3284\/revisions"}],"predecessor-version":[{"id":3285,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/3284\/revisions\/3285"}],"wp:attachment":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/media?parent=3284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/categories?post=3284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/tags?post=3284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}