{"id":6365,"date":"2018-09-23T10:54:44","date_gmt":"2018-09-23T10:54:44","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6365"},"modified":"2018-09-23T10:54:44","modified_gmt":"2018-09-23T10:54:44","slug":"triangular-numbers","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/triangular-numbers\/","title":{"rendered":"Triangular numbers"},"content":{"rendered":"<p><strong>Introduction<\/strong><\/p>\n<p>I was listening to a <a href=\"https:\/\/www.bbc.co.uk\/programmes\/b09gbnfj\" rel=\"noopener\" target=\"_blank\">podcast on Gauss<\/a> where they talked about a story that, when as a youngster Gauss (and his classmates) were asked to calculate the sum of 1 to 100, Gauss came back with the following solution.<\/p>\n<p><em>If we take the first and last number and add them, we get 101, if we take the 2nd and 2nd last number (2 + 99) we also get the result 101, hence if we do continue this process for half of the numbers we&#8217;ll get the result of the sum of 1 to 100<\/em><\/p>\n<p><em>Please note, I&#8217;m not attributing the discovery of this formula to Gauss, so much as it just inspired me to look into this a little further.<\/em><\/p>\n<p>So he we have [1, 100] and what we want to do is 1 + 2 + 3 + 4 &#8230; + 100, using Gauss&#8217;s observation, we can actually calculate this as (1 + n) * n \/ 2, i.e. the last number added to the first gives us 101 then multiply by 50 (i.e. 100\/2) which gives us the result 5050.<\/p>\n<p>The formula for this summation process is usually given as<\/p>\n<p>n(n + 1)\/2<\/p>\n<p>which is obviously the same as we&#8217;ve listed, just rearranged.<\/p>\n<p><strong>Triangular numbers<\/strong><\/p>\n<p>What n(n + 1)\/2 gives us, is a list of, what&#8217;s known as, triangular numbers. <\/p>\n<p><em>If we think about having marbles and with those, start from 1 marble creating equilateral triangles, we will need three marbles to create the next equilateral triangle, then 6 marbles then 10 and so on, this pattern can be calculated by 1, 1 + 2, 1 + 2 + 3, 1 + 2 + 3 + 4&#8230; and hence is the same as n(n + 1)\/2.<\/em><\/p>\n<p><strong>Is x a triangular number<\/strong><\/p>\n<p>To determine if a number is triangular we need to rearrange our equation as follows<\/p>\n<p>n(n + 1) \/ 2 = x<br \/>\n(n^2 + n) \/ 2 = x<br \/>\nn^2 + n = 2x<\/p>\n<p>This can be rewritten as<\/p>\n<p>n = (sqrt(8x + 1) &#8211; 1)\/2<\/p>\n<p>Now if n is a perfect square (i.e. no decimal points) then the number is triangular. We can use the following in code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar n = (Math.Sqrt(8 * x + 1) - 1) \/ 2;\r\nreturn Math.Floor(n) == n;\r\n<\/pre>\n<p>to test if the result is a perfect square.<\/p>\n<p><em>A perfect square can be seen to be a squared number, i.e. 0^2, 1^2, 2^2 etc. are squared integers, hence if we square root a value we expect a non decimal number for it to be a perfect square.<\/em><\/p>\n<p><strong>References<\/strong><\/p>\n<p>http:\/\/mathforum.org\/library\/drmath\/view\/57162.html<br \/>\nhttp:\/\/www.maths.surrey.ac.uk\/hosted-sites\/R.Knott\/runsums\/triNbProof.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction I was listening to a podcast on Gauss where they talked about a story that, when as a youngster Gauss (and his classmates) were asked to calculate the sum of 1 to 100, Gauss came back with the following solution. If we take the first and last number and add them, we get 101, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[222],"tags":[],"class_list":["post-6365","post","type-post","status-publish","format-standard","hentry","category-number-theory"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6365"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6365\/revisions"}],"predecessor-version":[{"id":6479,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6365\/revisions\/6479"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}