{"id":6248,"date":"2016-05-04T12:23:07","date_gmt":"2016-05-04T16:23:07","guid":{"rendered":"http:\/\/blog.originlab.com\/?p=6248"},"modified":"2018-11-15T12:46:41","modified_gmt":"2018-11-15T17:46:41","slug":"creating-ulam-spirals-with-originc-part-ii","status":"publish","type":"post","link":"https:\/\/blog.originlab.com\/ja\/creating-ulam-spirals-with-originc-part-ii","title":{"rendered":"Creating Ulam Spirals with Origin C &#8211; Part II"},"content":{"rendered":"<p>In a <a href=\"http:\/\/blog.originlab.com\/programming\/creating-ulam-spirals-with-originc-part-i\">previous blog post<\/a> we covered how to generate the basemap of a Ulam spiral by using a C++ script in Code Builder. In this post we&#8217;ll be appending that script with some basic functions allowing you to display prime numbers, odd numbers, and numbers divisible by specific integers. We&#8217;ll then plot the resulting matrices and show you how to create a basic GUI for calling these different functions. You can view the end result of this blog by downloading the .cpp and .opj files <a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/UlamSpiralBlog_post_pt2.zip\">here<\/a>.<\/p>\n<p>With the way this script has been structured it is very easy for us to add additional functions that rely on our CSpiral function but can be added without modifying it.<\/p>\n<p>Between the \/\/CSpiral and \/\/UlamSpiral sections of our basic script, add the following<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/----------------------------------------------------------------------------\r\n\/\/ CPrimeSpiral\r\n\/\/ This class does a spiral and checks if the cell index is a prime number.\r\n\/\/ If it is prime the cell is set to the index else it is set to zero.\r\n\/\/----------------------------------------------------------------------------\r\nclass CPrimeSpiral : public CSpiral\r\n{\r\npublic:\r\n int Equation()\r\n {\r\n m_M[m_row][m_col] = IsPrime(m_index) ? m_index : NANUM;\r\n return 0;\r\n\r\n\/\/ If the boolean function IsPrime is true, this function returns its value to the matrix\r\n\/\/ If the function is false, it returns the missing value variable \"NANUM\"\r\n }\r\n bool IsPrime(int n)\r\n {\r\n double sr = sqrt(n);\r\n for( int i = 2; i &lt;= sr; i++ )\r\n\r\n\r\n\/\/ This takes the square root of the number in question, then runs through the numbers between 2 \r\n\/\/ and this square root in whole integer increments, represented by i++\r\n\r\n {\r\n if( (n % i) == 0 )\r\n return false;\r\n }\r\n return true;\r\n }\r\n\r\n\/\/ The values between 2 and the square of the number are then returned and divided by the\r\n\/\/ number itself. For those not familiar with the use of the percentage symbol in C++,\r\n\/\/ it serves as a division operator, returning the remainder of an expression.\r\n\r\n};\r\n\r\n<\/pre>\n<p>Then we&#8217;ll need to update the UlamSpiral function to be able to call this script as one of our options; for this reason the end of our script will now appear as such, note the comments in CAPS-<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/----------------------------------------------------------------------------\r\n\/\/ UlamSpiral\r\n\/\/ The main function for calling the different types of spirals.\r\n\/\/----------------------------------------------------------------------------\r\n\r\nenum {\r\n UlamSpiral_Help = 0,\r\n UlamSpiral_Index,\r\n UlamSpiral_Prime, \/\/ ADD THIS SINGLE LINE AFTER _Index\r\n UlamSpiral_Unknown\r\n};\r\n\r\nint UlamSpiral(int type = 0)\r\n{\r\n switch( type )\r\n {\r\n case UlamSpiral_Help:\r\n printf(\"%d = Help\\n\", UlamSpiral_Help);\r\n printf(\"%d = Index\\n\", UlamSpiral_Index);\r\n printf(\"%d = Prime Numbers\\n\", UlamSpiral_Prime);\r\n break;\r\n case UlamSpiral_Index:\r\n {\r\n CSpiral spiral;\r\n spiral.Run();\r\n }\r\n break;\r\n case UlamSpiral_Prime: \/\/ ADD THIS CASE SECTION AFTER \"BREAK;\" STARTING FROM THIS LINE\r\n {\r\n CPrimeSpiral spiral;\r\n spiral.Run();\r\n }\r\n break;\r\n }\r\n return 0; \/\/ TO THIS LINE\r\n}<\/pre>\n<p>Once this updated script has been built, create a new matrix or open an existing one. Then in the main menu go to Matrix&gt;Set Matrix Dimension\/Labels&#8230;, and set its\u00a0dimensions to 100&#215;100 or 200&#215;200. This will allow us to better view the frequency of prime numbers in our spiral. Next open the Script Window and type &#8220;UlamSpiral(2)&#8221;. If the script works you should see that it generated a spiral that displays only those numbers which are primes, whereas those that\u00a0are not are displayed as missing numbers (&#8211;).<br \/>\n<a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_004.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6260\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_004.png\" alt=\"ulam_004\" width=\"616\" height=\"501\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_004.png 826w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_004-300x244.png 300w\" sizes=\"auto, (max-width: 616px) 100vw, 616px\" \/><\/a><\/p>\n<p>Now that we have our prime values, let&#8217;s plot them. Select the whole matrix, and go to Plot&gt;Contour\/Heat Map&gt;Heat Map. At first you should see a blank plot with &#8220;Speed Mode is On&#8221; emblazoned across the graph. To change this, simply click on the little running man icon that looks like this\u00a0<a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/speed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6287\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/speed.png\" alt=\"speed\" width=\"22\" height=\"22\" \/><\/a>\u00a0to turn off Speed Mode. By default this button should appear on the upper right-hand side of your workspace, next to the rescale button. Your graph should now appear like so-<\/p>\n<p><a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_005.png\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6288\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_005.png\" alt=\"ulam_005\" width=\"902\" height=\"687\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_005.png 902w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_005-300x228.png 300w\" sizes=\"auto, (max-width: 902px) 100vw, 902px\" \/><br \/>\n<\/a><br \/>\nAs an aside&#8211;<br \/>\nWhile our matrix is 200 by 200 cells in size, it&#8217;s important to note that the size of the matrix represents an index that is not directly tied to the x and y values of the graph. If you go back into the matrix and then in the main menu select &#8220;Matrix&gt;Set Matrix Dimension\/Labels&#8230;&#8221; you&#8217;ll see that in columns are mapped for x values from 1 to 10 by default. There is however no reason to change the mapping values as the axes are meaningless in this context anyway.<\/p>\n<p>Next select and delete the axes, scale, titles, and color scale if you so choose. Since our series is being displayed as a spiral, these numbers bear no meaning. To make our graph more legible, double click on the heat map to open the Plot Details dialog and then in the Colormap\/Contours tab change the Missing Values color from the default to either White or None. After clicking okay, you may want to change the color palette to something that contrasts with the white background. For the purposes of our example, I would recommend selecting the &#8220;Blue&#8221;, &#8220;Green&#8221; or &#8220;Red&#8221; palettes that appear in Origin 2016&#8217;s new palette menu.<\/p>\n<p><a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_010.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6290\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_010.png\" alt=\"ulam_010\" width=\"829\" height=\"634\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_010.png 1057w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_010-300x229.png 300w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_010-1024x783.png 1024w\" sizes=\"auto, (max-width: 829px) 100vw, 829px\" \/><\/a>Now that we can visualize our spiral, let&#8217;s add one last feature to our script. Say we want to\u00a0design a basic input function, allowing users to view patterns generated by different denominators. In this case our function script, added between \/\/CPrimeSpiral and the \/\/UlamSpiral calling function, will look very simple-<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/----------------------------------------------------------------------------\r\n\/\/ CDivBySpiral\r\n\/\/\r\n\/\/----------------------------------------------------------------------------\r\nclass CDivBySpiral : public CSpiral\r\n{\r\npublic:\r\n int Equation()\r\n {\r\n m_M[m_row][m_col] = m_index % divisor ? NANUM : m_index;\r\n\/\/ For each cell, if the m_index returns a remainder when divided by the divisor, display a missing\r\n\/\/ number (NANUM), otherwise return the index number\r\n return 0;\r\n }\r\n\r\n int divisor;\r\n};<\/pre>\n<p>Our UlamSpiral calling function will then be updated to\u00a0place another line in our &#8220;enum&#8221; section, like so-<\/p>\n<pre class=\"lang:c++ decode:true \">enum {\r\n UlamSpiral_Help = 0,\r\n UlamSpiral_Index,\r\n UlamSpiral_Prime,\r\n UlamSpiral_DivBy, \/\/ ADD THIS LINE HERE\r\n UlamSpiral_Unknown\r\n};\r\n\r\n<\/pre>\n<p>and then adding the following-<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/\r\nprintf(\"%d = Divisible by\\n\", UlamSpiral_DivBy);\r\n\/\/<\/pre>\n<p>after the &#8220;printf UlamSpiral_Prime&#8221; line and before the &#8220;break&#8221;. Then after the &#8220;case UlamSpiral_Prime&#8221; &#8220;break;&#8221; and before the next bracket }, add-<\/p>\n<pre class=\"lang:c++ decode:true\"> case UlamSpiral_DivBy:\r\n {\r\n GETN_BOX(tr);\r\n GETN_NUM(Divisor, \"Divisor\", 1);\r\n\r\n\/\/ Triggers an input box which lists 1 by default, allows user to enter any integer\r\n if( GetNBox(tr) )\r\n {\r\n CDivBySpiral spiral;\r\n spiral.divisor = tr.Divisor.dVal;\r\n spiral.Run();\r\n }\r\n\/\/ If an integer is entered, this will call CDivBySpiral function based on the divisor given\r\n\r\n }\r\n break;<\/pre>\n<p>Compile the\u00a0updated script, and with the matrix window active (as opposed to the heat map), go to the script window and type UlamSpiral(3). You should see this dialog box-<br \/>\n<a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_012.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6297\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_012.png\" alt=\"ulam_012\" width=\"208\" height=\"131\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_012.png 208w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_012-80x50.png 80w\" sizes=\"auto, (max-width: 208px) 100vw, 208px\" \/><\/a><\/p>\n<p>Enter any integer, in this case we&#8217;ll type in 7, and then after the matrix changes\u00a0you should find that your heatmap has updated as well to show the distribution of those numbers which can be divided by 7. This example isn&#8217;t especially practical but rather shows how you can add user inputs to display different series on your spiral.<\/p>\n<p><a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_concept_013.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6298\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_concept_013.png\" alt=\"ulam_concept_013\" width=\"950\" height=\"728\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_concept_013.png 950w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_concept_013-300x230.png 300w\" sizes=\"auto, (max-width: 950px) 100vw, 950px\" \/><br \/>\n<\/a><\/p>\n<p>These are some simple examples of how OriginC scripting can be used to illustrate\u00a0different series of numbers. If you know C programming, you should be able to add your own functions to the script to show &#8220;<a href=\"http:\/\/www.numbersaplenty.com\/set\/odious_number\/\">odious<\/a>&#8221;\u00a0numbers or other series and call them from the UlamSpiral function as another option. UIam spirals are especially useful as a teaching tool for showing the behavior of other complex number series that may not illustrate clear patterns over a single number line. You can learn more about these and similar number spirals on many math reference sites all across the internet.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous blog post we covered how to generate &hellip; <\/p>","protected":false},"author":1070,"featured_media":6298,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_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},"jetpack_post_was_ever_published":false},"categories":[5],"tags":[],"class_list":["post-6248","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-oc"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7DvU2-1CM","jetpack_likes_enabled":true,"jetpack_featured_media_url":"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_concept_013.png","post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/6248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/users\/1070"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/comments?post=6248"}],"version-history":[{"count":45,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/6248\/revisions"}],"predecessor-version":[{"id":7434,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/6248\/revisions\/7434"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/media\/6298"}],"wp:attachment":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/media?parent=6248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/categories?post=6248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/tags?post=6248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}