{"id":6,"date":"2014-04-21T21:59:16","date_gmt":"2014-04-21T21:59:16","guid":{"rendered":"http:\/\/konrad.burnik.org\/wordpress\/?p=6"},"modified":"2022-01-31T14:45:43","modified_gmt":"2022-01-31T14:45:43","slug":"square-root-of-two","status":"publish","type":"post","link":"https:\/\/konrad.burnik.org\/wordpress\/square-root-of-two\/","title":{"rendered":"Another look at the square root of two"},"content":{"rendered":"<p>The other day I was thinking about approximating <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-a32e36658a786336180818ca1355bc91_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"23\" style=\"vertical-align: -2px;\"\/> in terms of segments of size <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-634848eed30e227108a103609c5a5011_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#49;&#47;&#50;&#94;&#107;\" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"33\" style=\"vertical-align: -5px;\"\/> for <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-0e0e22d187f1a4dbdb46165e0eb80813_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#107;&#61;&#48;&#44;&#49;&#44;&#50;&#44;&#92;&#100;&#111;&#116;&#115;\" title=\"Rendered by QuickLaTeX.com\" height=\"16\" width=\"103\" style=\"vertical-align: -4px;\"\/>, The question I was interested was \"What is the least number of segments of length <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-634848eed30e227108a103609c5a5011_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#49;&#47;&#50;&#94;&#107;\" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"33\" style=\"vertical-align: -5px;\"\/> that we need to cross over <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-a32e36658a786336180818ca1355bc91_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"23\" style=\"vertical-align: -2px;\"\/> starting from zero?\" and one particular sequence of integers I came up with was this:<\/p>\n<p class=\"ql-center-displayed-equation\" style=\"line-height: 17px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-e0fc94c01da00342e00775693d25f446_l3.png\" height=\"17\" width=\"283\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91;&#50;&#44;&#32;&#51;&#44;&#32;&#54;&#44;&#32;&#49;&#50;&#44;&#32;&#50;&#51;&#44;&#32;&#52;&#54;&#44;&#32;&#57;&#49;&#44;&#32;&#49;&#56;&#50;&#44;&#32;&#51;&#54;&#51;&#44;&#32;&#55;&#50;&#53;&#44;&#92;&#100;&#111;&#116;&#115;&#46;&#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p>\n<p>Sadly, at the time of writing this post the <a href=\"https:\/\/oeis.org\/\">Online Encyclopedia of Integer Sequences <\/a>did not return anything for this sequence.<\/p>\n<p><span style=\"line-height: 1.714285714; font-size: 1rem;\">The sequence represents the numerators for dyadic rationals which approximate <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-a32e36658a786336180818ca1355bc91_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"23\" style=\"vertical-align: -2px;\"\/> within precision <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-3711a84fa24bde60b27c7e43ff80dfad_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#50;&#94;&#123;&#45;&#107;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"27\" style=\"vertical-align: 0px;\"\/> (within k-bits of precision), but is this sequence computable? In what follows I assume that the reader is familiar with the basic definitions of <a href=\"http:\/\/en.wikipedia.org\/wiki\/%CE%9C-recursive_function\">mu-recursive functions<\/a>. <\/span><\/p>\n<p><span style=\"line-height: 1.714285714; font-size: 1rem;\">It turns out that our sequence can be described by a recursive function <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-ac259e34b70c754a063183a43a2d042e_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#102;&#58;&#32;&#92;&#109;&#97;&#116;&#104;&#98;&#98;&#123;&#78;&#125;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#97;&#114;&#114;&#111;&#119;&#32;&#92;&#109;&#97;&#116;&#104;&#98;&#98;&#123;&#78;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"16\" width=\"79\" style=\"vertical-align: -4px;\"\/><\/span><\/p>\n<p class=\"ql-center-displayed-equation\" style=\"line-height: 23px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-aefe2e6f0a40eb584c47d92fcddf26cf_l3.png\" height=\"23\" width=\"186\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91;&#102;&#40;&#107;&#41;&#32;&#61;&#32;&#92;&#109;&#117;&#46;&#110;&#32;&#91;&#110;&#94;&#50;&#32;&#62;&#32;&#50;&#94;&#123;&#40;&#50;&#107;&#43;&#49;&#41;&#125;&#93;&#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p>\n<p> for all <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-9a4c8576d8eb4547cc989351900e123e_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#107;&#92;&#103;&#101;&#113;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"42\" style=\"vertical-align: -3px;\"\/>.<\/p>\n<p>The idea of the function <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-9086e973b0b4e28a62dfe0d54ad71a9f_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#102;\" title=\"Rendered by QuickLaTeX.com\" height=\"16\" width=\"10\" style=\"vertical-align: -4px;\"\/> is that for each <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-baf0cb4c6e731452601e81740251d343_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#107;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: 0px;\"\/> it outputs the number of <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-634848eed30e227108a103609c5a5011_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#49;&#47;&#50;&#94;&#107;\" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"33\" style=\"vertical-align: -5px;\"\/> segments that fit into <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-a32e36658a786336180818ca1355bc91_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"23\" style=\"vertical-align: -2px;\"\/> plus one.<\/p>\n<p>Since for each <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-baf0cb4c6e731452601e81740251d343_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#107;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: 0px;\"\/>, the value <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-04e3dccce58a979e779230dd3dfe8e45_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#102;&#40;&#107;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"33\" style=\"vertical-align: -5px;\"\/> is the smallest integer such that <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-befeffdfaf96f7d007c624a491de11da_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#102;&#40;&#107;&#41;&#47;&#50;&#94;&#107;&#32;&#62;&#32;&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"21\" width=\"108\" style=\"vertical-align: -5px;\"\/> we have<\/p>\n<p class=\"ql-center-displayed-equation\" style=\"line-height: 22px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-34065433a7eb98a9168ffef91a90547b_l3.png\" height=\"22\" width=\"234\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91;&#40;&#102;&#40;&#107;&#41;&#32;&#45;&#32;&#49;&#41;&#47;&#50;&#94;&#107;&#32;&#60;&#32;&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#125;&#32;&#60;&#32;&#102;&#40;&#107;&#41;&#47;&#50;&#94;&#107;&#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p>\n<p> for each <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-c4acb3984ba488ff87527c15643d5225_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#107;&#92;&#103;&#101;&#113;&#32;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"42\" style=\"vertical-align: -3px;\"\/>.<\/p>\n<p>From this we obtain the error bound <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-f686b29c926027d69c88cdd03f1772d8_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#124;&#102;&#40;&#107;&#41;&#47;&#50;&#94;&#107;&#32;&#45;&#32;&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#125;&#124;&#32;&#60;&#32;&#50;&#94;&#123;&#45;&#107;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"21\" width=\"164\" style=\"vertical-align: -5px;\"\/>.<\/p>\n<p>Approximation of <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-a32e36658a786336180818ca1355bc91_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"23\" style=\"vertical-align: -2px;\"\/> within precision <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-3711a84fa24bde60b27c7e43ff80dfad_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#50;&#94;&#123;&#45;&#107;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"27\" style=\"vertical-align: 0px;\"\/> can be \"represented\" by a natural number <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-04e3dccce58a979e779230dd3dfe8e45_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#102;&#40;&#107;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"33\" style=\"vertical-align: -5px;\"\/>. To get the actual approximation by a dyadic rational just divide by <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-6f99173cbf16936c985be79cc8476ce1_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#50;&#94;&#107;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"16\" style=\"vertical-align: 0px;\"\/> but what we want is to avoid division. We want to calculate only with natural numbers!<\/p>\n<p><strong>Implementation<\/strong><\/p>\n<p>The function f has a straightforward (although inefficient) implementation in Python.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def calcSqrt2Segments(k):\n    n = 0\n    while(n*n &lt;= 2**(2*k+1)):\n        n = n + 1\n    return n<\/pre>\n<p>This is in fact terribly inefficient, but in computability theory efficiency is not the goal, the goal is usually to prove that something is uncomputable even if you have all the resources of time and space at your disposal. Nevertheless, here is a slightly better version we get if we notice that the next term <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-4ccfb544706f537b87f0c6c75baca472_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#102;&#40;&#107;&#43;&#49;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"64\" style=\"vertical-align: -5px;\"\/> is about twice as large as <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-04e3dccce58a979e779230dd3dfe8e45_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#102;&#40;&#107;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"33\" style=\"vertical-align: -5px;\"\/> with a small correction.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def calcSqrt2segmentsRec(k):\n    if k == 0:\n        return 2\n    else:\n        res = 2*calcSqrt2segmentsRec(k-1) - 1\n        if res*res &lt;= 2**(2*k+1):\n           res = res + 1\n        return res<\/pre>\n<p>This of course suffers from stack overflow problems, so memoization is a natural remedy.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def calcSqrt2segmentsMemo(k):\n    m = dict()\n    m[0] = 2\n    for j in range(1, k+1):\n        m[j] = 2*m[j-1] - 1\n        if m[j]*m[j] &lt;= 2**(2*j+1):\n            m[j] = m[j] + 1\n    return m[k]<\/pre>\n<p>Memoization can use-up memory and we can do \u00a0better. We don't need to memorize the whole sequence up to k to calculate <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-4ccfb544706f537b87f0c6c75baca472_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#102;&#40;&#107;&#43;&#49;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"64\" style=\"vertical-align: -5px;\"\/> we only need the value <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-04e3dccce58a979e779230dd3dfe8e45_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#102;&#40;&#107;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"33\" style=\"vertical-align: -5px;\"\/>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def calcSqrt2segmentsBest(k):\n    m = 2\n    for j in range(1, k+1):\n        m = 2*m - 1\n        if m*m &lt;= 2**(2*j+1):\n            m = m + 1\n    return m<\/pre>\n<p>After defining a simple timing function and testing our memoization and \"best\" version we see that the best version is not so good in terms of running time when compared with memoization, they are both approximately about the same in terms of speed (the times are in seconds):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">&gt;&gt;&gt; timing(calcSqrt2segmentsMemo, 10000)\n0.6640379428863525\n&gt;&gt;&gt; timing(calcSqrt2segmentsBest, 10000)\n0.6430368423461914\n&gt;&gt;&gt; timing(calcSqrt2segmentsMemo, 20000)\n3.3311898708343506\n&gt;&gt;&gt; timing(calcSqrt2segmentsBest, 20000)\n3.3061890602111816\n&gt;&gt;&gt; timing(calcSqrt2segmentsMemo, 30000)\n8.899508953094482\n&gt;&gt;&gt; timing(calcSqrt2segmentsBest, 30000)\n8.848505973815918\n&gt;&gt;&gt; timing(calcSqrt2segmentsMemo, 50000)\n30.04171895980835\n&gt;&gt;&gt; timing(calcSqrt2segmentsBest, 50000)\n29.96371293067932\n&gt;&gt;&gt; timing(calcSqrt2segmentsMemo, 100000)\n164.99343705177307\n&gt;&gt;&gt; timing(calcSqrt2segmentsBest, 100000)\n168.6026430130005<\/pre>\n<p>Neverteless, we can use our \"best\" program to calculate <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-a32e36658a786336180818ca1355bc91_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"23\" style=\"vertical-align: -2px;\"\/> to arbritrary number of decimal places (in base 10). First, we need to determine how many bits are sufficient to calculate <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-a32e36658a786336180818ca1355bc91_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"23\" style=\"vertical-align: -2px;\"\/> to <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-13f056e0258345efa5dae8cdebe0af30_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#110;\" title=\"Rendered by QuickLaTeX.com\" height=\"8\" width=\"11\" style=\"vertical-align: 0px;\"\/> decimal places in base 10. Simple calculation yields:<\/p>\n<p class=\"ql-center-displayed-equation\" style=\"line-height: 18px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-d9d8f6fc43e39e59ba3a1628d09fbade_l3.png\" height=\"18\" width=\"113\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91;&#107;&#32;&#62;&#32;&#92;&#108;&#99;&#101;&#105;&#108;&#32;&#110;&#32;&#108;&#111;&#103;&#95;&#50;&#32;&#49;&#48;&#32;&#92;&#114;&#99;&#101;&#105;&#108;&#46;&#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p>\n<p>In Python we need a helper function that calculates this bound that avoids the nasty log and ceiling functions, so here it is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def calcNumDigits(n):\n    res = 1\n    for j in range(2, n+1): \n        if( j%3 == 0 or j%3 == 1 ):\n          res = res + 3\n        else:\n          res = res + 4\n    return res<\/pre>\n<p>At last, calculating <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-a32e36658a786336180818ca1355bc91_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"23\" style=\"vertical-align: -2px;\"\/> to arbritrary precision is done with this simple code below, returning a String with the digits of <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-a32e36658a786336180818ca1355bc91_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"23\" style=\"vertical-align: -2px;\"\/> in base 10. Note once more, that all this calculation is done only with the natural numbers and here we are using Python's powerful implementation of arithmetic with arbritrary long integers (which is not as nicely supported for decimals at least at the time of writing this post).<\/p>\n<p>Note also, that instead of dividing <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-04e3dccce58a979e779230dd3dfe8e45_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#102;&#40;&#107;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"33\" style=\"vertical-align: -5px;\"\/> by <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-6f99173cbf16936c985be79cc8476ce1_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#50;&#94;&#107;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"16\" style=\"vertical-align: 0px;\"\/> we are multiplying it with <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-3141f60b1ef05772020b7edff0834b34_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#53;&#94;&#107;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"16\" style=\"vertical-align: 0px;\"\/> to get a natural number with all of it's digits being an approximation of <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/konrad.burnik.org\/wordpress\/wp-content\/ql-cache\/quicklatex.com-a32e36658a786336180818ca1355bc91_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"23\" style=\"vertical-align: -2px;\"\/> with k bits of precision. This natural number we are then converting to a string, inserting a decimal point '.' and returning this as our result. So, here is the code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def calcSqrt2(n):\n    k = calcNumDigits(n)\n    res = str(5**k * calcSqrt2segmentsBest(k))\n    return res[0:1] + '.' + res[1:n]<\/pre>\n<p>Running it\u00a0gives us nice results:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">&gt;&gt;&gt; calcSqrt2(30)\n'1.41421356237309504880168872421'\n&gt;&gt;&gt; calcSqrt2(300)\n'1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140799'\n&gt;&gt;&gt; calcSqrt2(3000)\n'1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008344491148185876555542864551233142199263113325179706084365597043528564100879185007603610091594656706768836055717400767569050961367194013249356052401859991050621081635977264313806054670102935699710424251057817495310572559349844511269227803449135066375687477602831628296055324224269575345290288387684464291732827708883180870253398523381227499908123718925407264753678503048215918018861671089728692292011975998807038185433325364602110822992792930728717807998880991767417741089830608003263118164279882311715436386966170299993416161487868601804550555398691311518601038637532500455818604480407502411951843056745336836136745973744239885532851793089603738989151731958741344288178421250219169518755934443873961893145499999061075870490902608835176362247497578588583680374579311573398020999866221869499225959132764236194105921003280261498745665996888740679561673918595728886424734635858868644968223860069833526427990562831656139139425576490620651860216472630333629750756978706066068564981600927187092921531323682'<\/pre>\n<p><strong>Note:<\/strong><br \/>\nThis can of course be generalized to calculating the square root of any number and of any order. This is an easy exercise for the reader.<\/p>\n<p>Copyright \u00a9 2014, Konrad Burnik<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style3 like-6 jlk' href='javascript:void(0)' data-task='like' data-post_id='6' data-nonce='b932f7ab63' rel='nofollow'><img class='wti-pixel' src='https:\/\/konrad.burnik.org\/wordpress\/wp-content\/plugins\/wti-like-post\/images\/pixel.gif' title='Like' \/><span class='lc-6 lc'>+1<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style3 unlike-6 jlk' href='javascript:void(0)' data-task='unlike' data-post_id='6' data-nonce='b932f7ab63' rel='nofollow'><img class='wti-pixel' src='https:\/\/konrad.burnik.org\/wordpress\/wp-content\/plugins\/wti-like-post\/images\/pixel.gif' title='Unlike' \/><span class='unlc-6 unlc'>0<\/span><\/a><\/div> <\/div> <div class='status-6 status align-left'><\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>The other day I was thinking about approximating in terms of segments of size for , The question I was interested was \"What is the least number of segments of length that we need to cross over starting from zero?\" and one particular sequence of integers I came up with was this: &nbsp; &nbsp; Sadly, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"_s2mail":"yes","footnotes":""},"categories":[4],"tags":[],"class_list":["post-6","post","type-post","status-publish","format-standard","hentry","category-computable-analysis"],"_links":{"self":[{"href":"https:\/\/konrad.burnik.org\/wordpress\/wp-json\/wp\/v2\/posts\/6","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/konrad.burnik.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/konrad.burnik.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/konrad.burnik.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/konrad.burnik.org\/wordpress\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":67,"href":"https:\/\/konrad.burnik.org\/wordpress\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"predecessor-version":[{"id":1210,"href":"https:\/\/konrad.burnik.org\/wordpress\/wp-json\/wp\/v2\/posts\/6\/revisions\/1210"}],"wp:attachment":[{"href":"https:\/\/konrad.burnik.org\/wordpress\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/konrad.burnik.org\/wordpress\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/konrad.burnik.org\/wordpress\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}