{"id":3559,"date":"2019-04-08T00:01:57","date_gmt":"2019-04-08T07:01:57","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3559"},"modified":"2019-04-06T11:26:19","modified_gmt":"2019-04-06T18:26:19","slug":"interesting-numbers-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3559","title":{"rendered":"Interesting Numbers &#8211; Solution"},"content":{"rendered":"<p>Interesting numbers have a mathematical property that makes them handy in a certain way. For programming, these numbers present useful binary patterns that may be obvious in hexadecimal but meaningless in decimal. For non-programmers and non-mathematicians, interesting numbers offer visual patterns that most likely have no useful application beyond their look.<br \/>\n<!--more--><br \/>\nFor this month&#8217;s Exercise, the challenge is to write code that loops from 100 to 1000 and outputs the values 111, 222, 333, 444, 555, 666, 777, 888, and 999.<\/p>\n<p>For my first attempt, I chose to use character comparisons. Each value is passed to the <em>interesting()<\/em> function, where it&#8217;s converted to a string. Each character in the string is compared with each other and if they all match, the function returns 1 (TRUE):<\/p>\n<pre class=\"screen\">\r\nint interesting(int v)\r\n{\r\n    char buffer[4];\r\n\r\n    sprintf(buffer,\"%d\",v);\r\n\r\n    if( buffer[0]==buffer[1] &amp;&amp; buffer[1]==buffer[2] )\r\n        return(1);\r\n    else\r\n        return(0);\r\n}<\/pre>\n<p>By the way, the <em>sprintf()<\/em> function in this solution is considered unsafe because the buffer can hold only three characters. For my solution, no value larger than 3 characters is passed, so it works. In the wild, however, values must be checked for overflow.<\/p>\n<p>My second solution uses math, so it&#8217;s straightforward:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for(x=100;x&lt;1000;x++)\r\n    {\r\n        if( (x % 111) == 0 )\r\n            printf(\"%d is interesting\\n\",x);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>When <code>x % 111<\/code> is equal to zero, the number contains three of the same digits and that value is output. Simple.<\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_04-Exercise-a.c\" rel=\"noopener noreferrer\" target=\"_blank\">Click here<\/a> to view the full source code for my first solution on Github.<\/p>\n<p>The second solution is shown above, but you can <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_04-Exercise-b.c\" rel=\"noopener noreferrer\" target=\"_blank\">click here<\/a> to view it on Github as well.<\/p>\n<p>I hope you devised an interesting solution to the puzzle, which I confess is a rather simple challenge. In the larger programming picture, however, pattern-matching is a worthy field of study with various interesting applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Interesting numbers have a mathematical property that makes them handy in a certain way. For programming, these numbers present useful binary patterns that may be obvious in hexadecimal but meaningless in decimal. For non-programmers and non-mathematicians, interesting numbers offer visual &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3559\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/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-3559","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\/3559","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=3559"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3559\/revisions"}],"predecessor-version":[{"id":3566,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3559\/revisions\/3566"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}