{"id":2447,"date":"2010-03-31T19:38:05","date_gmt":"2010-03-31T18:38:05","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=2447"},"modified":"2010-04-14T22:52:34","modified_gmt":"2010-04-14T21:52:34","slug":"polygonal-numbers-on-quadratic-number-spirals","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=2447","title":{"rendered":"Polygonal numbers on quadratic number spirals"},"content":{"rendered":"<p>Over at Mathrecreation, Dan Mackinnon has been plotting <a href=\"http:\/\/mathrecreation.blogspot.com\/2010\/03\/polygonal-wanderings.html\">two dimensional polygonal numbers on quadratic number spirals<\/a> using a piece of software called <a href=\"http:\/\/www.keypress.com\/x5656.xml\">Fathom<\/a>.  Today&#8217;s train-journey home project for me was to reproduce this work using Mathematica and Python.<\/p>\n<p>I&#8217;ll leave the mathematics to <a href=\"http:\/\/mathrecreation.blogspot.com\/2009\/05\/quadratic-number-spirals-and-polygonal.html\">Mathrecreation<\/a> and <a href=\"http:\/\/www.numberspiral.com\/\">Numberspiral.com<\/a> and just get straight to the code.  Here&#8217;s the Mathematica version:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/images\/mathematica7\/polygonal_quadratic_number_spirals.nb\">Download the Mathematica Notebook<\/a><\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/images\/mathematica7\/polygonal_quadratic_number_spirals.nbp\">Download a fully interactive version<\/a> that will run on the free <a href=\"http:\/\/www.wolfram.com\/products\/player\/\">Mathematica Player<\/a><\/li>\n<\/ul>\n<pre>p[k_, n_] := ((k - 2) n (n + 1))\/2 - (k - 3) n;\r\nManipulate[\r\n nums = Table[p[k, n], {n, 1, numpoints}];\r\n If[showpoints,\r\n  ListPolarPlot[Table[{2*Pi*Sqrt[n], Sqrt[n]}, {n, nums}]\r\n   , Joined -&gt; joinpoints, Axes -&gt; False,\r\n   PlotMarkers -&gt; {Automatic, Small}, ImageSize -&gt; {400, 400}],\r\n  ListPolarPlot[Table[{2*Pi*Sqrt[n], Sqrt[n]}, {n, nums}]\r\n   , Joined -&gt; joinpoints, Axes -&gt; False, ImageSize -&gt; {400, 400}\r\n   ]\r\n  ]\r\n , {k, 2, 50, Appearance -&gt; \"Labeled\"}\r\n , {{numpoints, 100, \"Number of points\"}, {100, 250, 500}}\r\n , {{showpoints, True, \"Show points\"}, {True, False}}\r\n , {{joinpoints, True, \"Join points\"}, {True, False}}\r\n , SaveDefinitions :&gt; True\r\n ]<\/pre>\n<p>A set of 4 screenshots is shown below (click the image for a higher resolution version).  The formula for <a href=\"http:\/\/mathworld.wolfram.com\/PolygonalNumber.html\">polygonal numbers<\/a>, p(kn,n),  is only really valid for integer k and so I made a mistake when I allowed k to be any real value in the code above.  However, I liked some of the resulting spirals so much that I decided to leave the mistake in.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.walkingrandomly.com\/images\/mathematica7\/quadratic_spirals_big.png\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/www.walkingrandomly.com\/images\/mathematica7\/quadratic_spirals_small.png\" alt=\"\" \/><\/a><\/p>\n<p>When Dan showed the images he produced from Fathom, he mentioned that he would one day like to implement these systems in a more open platform.  Well, you can&#8217;t get much more open than Python so here&#8217;s a piece of code that makes use of <a href=\"http:\/\/www.python.org\/\">Python<\/a> and the <a href=\"http:\/\/matplotlib.sourceforge.net\/\">matplotlib<\/a> package (download the file &#8211; <a title=\"polygonal numbers plotted on a quadratic number spiral\" href=\"https:\/\/www.walkingrandomly.com\/images\/python\/python_polygonal.py\">polygonal_spiral.py<\/a>).<\/p>\n<pre lang=\"Python\">\r\n#!\/usr\/bin\/env python\r\nimport matplotlib.pyplot as plt\r\nfrom math import *\r\n\r\ndef p(k,n):\r\n   return(((k-2)*n*(n+1))\/2 -(k-3)*n)\r\n\r\nk=13\r\n\r\npolygonal_nums = [p(k,n) for n in range(100)]\r\ntheta = [2*pi*sqrt(n) for n in polygonal_nums]\r\nr = [sqrt(n) for n in polygonal_nums]\r\n\r\nmyplot = plt.polar(theta,r,'.-')\r\nplt.gca().axis('off')\r\nplt.show()<\/pre>\n<p>Now, something that I haven&#8217;t figured out yet is why the above python code works just fine for 100 points but if you increase it to 101 then you just get a single point.  Does anyone have any ideas?<\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/www.walkingrandomly.com\/images\/python\/python_quadratic_spiral.png\" alt=\"Polygonal numbers on a number spiral\" \/><\/p>\n<p>Unlike my Mathematica version, the above Python program does not give you an interactive GUI but I&#8217;ll leave that as an exercise for the reader.<\/p>\n<p><strong>If you enjoyed this post then you might also like:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=151\">Simulating Harmonographs<\/a><\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=626\">Quadraflakes,Pentaflakes,Hexaflakes and more<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Over at Mathrecreation, Dan Mackinnon has been plotting two dimensional polygonal numbers on quadratic number spirals using a piece of software called Fathom. Today&#8217;s train-journey home project for me was to reproduce this work using Mathematica and Python. I&#8217;ll leave the mathematics to Mathrecreation and Numberspiral.com and just get straight to the code. Here&#8217;s the [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":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":[6,8,31],"tags":[],"class_list":["post-2447","post","type-post","status-publish","format-standard","hentry","category-general-math","category-mathematica","category-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-Dt","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2447"}],"version-history":[{"count":16,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2447\/revisions"}],"predecessor-version":[{"id":2460,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2447\/revisions\/2460"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}