{"id":725,"date":"2008-12-30T08:05:21","date_gmt":"2008-12-30T08:05:21","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2008\/12\/30\/continued-fractions-classic-repost\/"},"modified":"2008-12-30T08:05:21","modified_gmt":"2008-12-30T08:05:21","slug":"continued-fractions-classic-repost","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2008\/12\/30\/continued-fractions-classic-repost\/","title":{"rendered":"Continued Fractions (classic repost)"},"content":{"rendered":"<p><em> I&#8217;m away on vacation this week, taking my kids to Disney World. Since I&#8217;m not likely to have time to write while I&#8217;m away, I&#8217;m taking the opportunity to re-run an old classic series of posts on numbers, which were first posted in the summer of 2006. These posts are mildly revised.<\/em><\/p>\n<p> One of the annoying things about how we write numbers is the fact that we generally write things one of two ways: as fractions, or as decimals.<\/p>\n<p> You might want to ask, &#8220;Why is that annoying?&#8221; (And in fact, that&#8217;s what I want you to ask, or else there&#8217;s no point in my writing the rest of this!)<\/p>\n<p> It&#8217;s annoying because both fractions and decimals can both only describe <em>rational<\/em> numbers &#8211; that is, numbers that are a perfect ratio of two integers. The problem with that is that <em>most<\/em> numbers aren&#8217;t rational. Our standard notations are <em>incapable<\/em> of representing the precise values of the overwhelming majority of numbers!<\/p>\n<p> But it&#8217;s even more annoying than that: if you use decimals, then there are lots of rational numbers that you can&#8217;t represent exactly (i.e., 1\/3); and if you use fractions, then it&#8217;s hard to express the idea that the fraction isn&#8217;t exact. (How do you write &pi; as a fraction? 22\/7 is a standard fractional approximation, but how do you say &pi;, which is <em>almost<\/em> 22\/7?)<\/p>\n<p>So what do we do?<\/p>\n<p><!--more--><\/p>\n<p>One of the answers is something called <em>continued fractions<\/em>. A continued fraction is a very neat thing. Here&#8217;s the idea: take a number where you don&#8217;t know it&#8217;s fractional form. Pick the nearest simple fraction 1\/n that&#8217;s just a <em>little bit too large<\/em>. If you were looking at, say, 0.4, you&#8217;d take 1\/2 &#8211; it&#8217;s a bit bigger. Now &#8211; you&#8217;ve got an approximation, but it&#8217;s too large. So that means that the demoninator is <em>too small<\/em>. So you add a correction to the denominator to make it a little bit bigger.  And you just keep doing that &#8211; you approximate the correction to the denominator by adding a fraction to the denominator that&#8217;s just a little too big, and then you add a correction to <em>that<\/em> correction.<\/p>\n<p>Let&#8217;s look at an  example: 2.3456.<\/p>\n<ol>\n<li> It&#8217;s close to 2. So we start with 2 + (0.3456).<\/li>\n<li> Now, we start approximating the fraction. The way we do that is we take the <em>reciprocal<\/em> of 0.3456 and take the integer part of it: 1\/0.3456 rounded down is 2 . So we make it 2 + 1\/2; and we know that the denominator is off by 0.3088.<\/li>\n<li> We take the reciprocal again, and get 3, and it&#8217;s off by 0.736.<\/li>\n<li> We take the reciprocal again, and get 1, and it&#8217;s off by 0.264.<\/li>\n<li> Next we get 3, but it&#8217;s off by 208\/1000.<\/li>\n<li> Then 4, off by 0.168.<\/li>\n<li> Then 5, off by 0.16.<\/li>\n<li> Then 6, off by 0.25.<\/li>\n<li> Then 4, off by 0; so now we have an exact result.<\/li>\n<\/ol>\n<p> So as a continued fraction, 2.3456 looks like:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"continued.jpg\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_355.jpg?resize=327%2C215\" width=\"327\" height=\"215\" \/><\/p>\n<p> As a shorthand, continued fractions are normally written using a list notation inside of square brackets: the integer part, following by a semicolon, followed by a comma-separated list of the denominators of each of the fractions. So our continued fraction for 2.3456 would be written [2; 2, 3, 1, 3, 4, 5, 6, 4].<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"nine-by-sixteen.png\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_356.png?resize=292%2C196\" width=\"292\" height=\"196\" class=\"inset\" \/><\/p>\n<p> There&#8217;s a very cool visual way of understanding that algorithm. I&#8217;m not going to show it for 2.3456, because it&#8217;s a bit too complicated &#8211; it would be hard to draw out the complete diagram for in a legible way. So instead, we&#8217;ll look at a simpler number to make the visual work. Let&#8217;s write 9\/16ths as a continued fraction. Basically, we make a grid consisting of 16 squares across by 9 squares up and down, like this one.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"nine-by-sixteen-step1.png\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_357.png?resize=292%2C225\" width=\"292\" height=\"225\" class=\"inset\" \/><\/p>\n<p>  We draw the largest square we can on that grid. The number of squares of that size that we can draw is the first digit of the continued fraction. For 9\/16ths, we can draw one 9&times;9 square &#8211; so our first digit is one, and we&#8217;re left with a 7&times;9 rectangle:<\/p>\n<p> Now we just repeat: draw the largest square we can. That&#8217;s a 7&times;7, and we can only draw one of it. So the second digit is one; and we&#8217;re left with a 7&times;2:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"nine-by-sixteen-step2.png\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_358.png?resize=298%2C236\" width=\"298\" height=\"236\" \/><\/p>\n<p> And repeat: the largest square we can draw is a 2&times;2, and we can draw three of them. So the next digit is a three; and we&#8217;re left with 1&times;2 &#8211; which is 2 1&times;1, so the last digit is a 2.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"nine-by-sixteen-last.png\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_359.png?resize=350%2C236\" width=\"350\" height=\"236\" \/><\/p>\n<p> So the continued fraction for 9\/16ths is 1\/(1+1\/(1+3\/(1+2))), or  [0; 1, 1, 3, 2].<\/p>\n<p> One incredibly nifty thing about this way of writing numbers is: what&#8217;s the reciprocal of 2.3456, aka [2; 2, 3, 1, 3, 4, 5, 6, 4]? It&#8217;s [0; 2, 2, 3, 1, 3, 4, 5, 6, 4]. We just add a zero to the front as the integer part, and push everything else one place to the right. If it was a zero in front, then we would have removed the zero and pulled everything else one place to the left.<\/p>\n<p> Using continued fractions, we can represent <em>any<\/em> rational number in a finite-length continued fraction. We still can&#8217;t directly represent irrational numbers &#8211; they&#8217;re going to be infinite continued fractions. But we can usually write some expression to show how to continue the number, and for a given precision, we can approximate irrational numbers with a much smaller representation. <\/p>\n<p> So, as I just said, we represent irrational numbers using <em>infinite<\/em> continued fractions. So there&#8217;s an infinite series of correction fractions. You can understand it as a series of every-improving approximations of the value of the number. And you can define it using a recurrence relation (that is, a recursive formula) for how to get to the next digit &#8211; that recurrence relation is the real key &#8211; we can include a simple equation that describes how, given the current finite continued fraction, to generate the next step.<\/p>\n<p> For example, &pi; = [3; 7, 15, 1, 292, 1, &#8230;]. If we work that out, the first six places of the continued fraction for pi work out in decimal form to 3.14159265392. That&#8217;s correct to the first 11 places in decimal. So 9 numerals of continued fractions gives us 11 decimal places. In general, the conciseness savings of continued fractions is even better than that.<\/p>\n<p><p> I&#8217;ll close with a very cool property of continued fractions. The square root of most numbers (excepting the perfect squares) is irrational. In continued fractions, they&#8217;re still irrational &#8211; that is, they&#8217;re infinite continued fractions. But all irrational square roots form repeating continued fractions. The coolest example of this? The square root of 2 in continued fractions is [1; 2, 2, 2, 2, &#8230;].<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m away on vacation this week, taking my kids to Disney World. Since I&#8217;m not likely to have time to write while I&#8217;m away, I&#8217;m taking the opportunity to re-run an old classic series of posts on numbers, which were first posted in the summer of 2006. These posts are mildly revised. One of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[13,43],"tags":[],"class_list":["post-725","post","type-post","status-publish","format-standard","hentry","category-classics","category-numbers"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-bH","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=725"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/725\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=725"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}