{"id":1587,"date":"2015-10-08T00:01:50","date_gmt":"2015-10-08T07:01:50","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1587"},"modified":"2015-10-03T09:10:45","modified_gmt":"2015-10-03T16:10:45","slug":"the-hailstone-sequence-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1587","title":{"rendered":"The Hailstone Sequence &#8211; Solution"},"content":{"rendered":"<p>My solution to this month&#8217;s Exercise is split into two main parts. The first part generates the random value in the range of 1 to 100. The second part performs the Hailstone sequence on that value.<br \/>\n<!--more--><br \/>\nFor the second part of my solution, I use a <em>while<\/em> loop, which spins until the value equals 1 &mdash; the end of the Hailstone sequence. As the loop spins, a test is made to determine if the value is odd. If so, the <code>(n*3)+1<\/code> calculation is made, otherwise the value is divided by two. For each iteration, a <code>length<\/code> counter tracks the values generated.<\/p>\n<p>Here&#8217;s the code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    int starting,length;\r\n\r\n    <span class=\"comments\">\/* initialize starting value *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n    starting = (rand() % 100) + 1;\r\n    length = 1;\r\n\r\n    printf(\"Sequence: %3d \",starting);\r\n    while(starting != 1)\r\n    {\r\n        if( starting % 2 )  <span class=\"comments\">\/* true if odd *\/<\/span>\r\n            starting = (starting*3)+1;\r\n        else                <span class=\"comments\">\/* even *\/<\/span>\r\n            starting \/= 2;\r\n        printf(\"%3d \",starting);\r\n        length++;\r\n    }\r\n    printf(\"\\nSequence length: %d\\n\",length);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 11 generates the random number in the range of 1 to 100, saving the value in the <code>starting<\/code> variable.<\/p>\n<p>The <code>length<\/code> variable is initialized at Line 12. It&#8217;s set to 1 because the <code>starting<\/code> value could be 1, in which case the length is 1. One and done.<\/p>\n<p>The <em>while<\/em> loop begins at Line 15. If the starting value isn&#8217;t equal to 1, the loop repeats.<\/p>\n<p>At Line 17 a test is made to determine whether the value is odd; the remainder of the mod 2 operation is 1 or TRUE when the value of <code>starting<\/code> is odd. Line 18 performs the odd calculation, resetting the value of variable <code>starting<\/code>.<\/p>\n<p>If the value isn&#8217;t odd (meaning that it&#8217;s even), Line 20 divides <code>starting<\/code> by two.<\/p>\n<p>The value of <code>length<\/code> is incremented at Line 22, and the loop repeats.<\/p>\n<p>Most programming puzzles involving the Hailstone sequence do more than simply display and count individual values. For example, your code could run on all values in a range, say 1 to 100, and report which number has the longest or shortest Hailstone sequence in that range. Feel free to attempt that exercise on your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My solution to this month&#8217;s Exercise is split into two main parts. The first part generates the random value in the range of 1 to 100. The second part performs the Hailstone sequence on that value.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1587","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1587"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1587\/revisions"}],"predecessor-version":[{"id":1600,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1587\/revisions\/1600"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}