{"id":6093,"date":"2023-11-08T00:01:42","date_gmt":"2023-11-08T08:01:42","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6093"},"modified":"2023-11-04T08:49:18","modified_gmt":"2023-11-04T15:49:18","slug":"numbers-with-unique-digits-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6093","title":{"rendered":"Numbers with Unique Digits &#8211; Solution"},"content":{"rendered":"<p>I hope you came up with an interesting solution for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6085\">this month&#8217;s Exercise<\/a>, one different from my own. The goal is to output unique values from zero through ten billion, values where no two digits repeat.<br \/>\n<!--more--><br \/>\nThe core of my solution is to convert the number into a string. I then scan the string for repeating digits. This process takes place in a function, <em>digit_scan()<\/em>, which returns TRUE or FALSE based on the presence of repeating digits. I tried putting this scan in the <em>main()<\/em> function, but the code became overly complex. The <em>digit_scan()<\/em> function resolved many issues.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_11-Exercise.c\" rel=\"noopener\" target=\"_blank\">2023_11-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n#define MAX 11\r\n#define TRUE 1\r\n#define FALSE 0\r\n\r\nint digit_scan(long a)\r\n{\r\n    int size,x,y;\r\n    char digits[MAX];\r\n\r\n    <span class=\"comments\">\/* convert to a string *\/<\/span>\r\n    snprintf(digits,MAX,\"%ld\",a);\r\n    <span class=\"comments\">\/* obtain value size *\/<\/span>\r\n    size = strlen(digits) + 1;\r\n    <span class=\"comments\">\/* scan the digits *\/<\/span>\r\n    for( x=0; x&lt;size-2; x++ )\r\n    {\r\n        for( y=x+1; y&lt;size-1; y++ )\r\n        {\r\n            if( digits[x] == digits[y] )\r\n            {\r\n                <span class=\"comments\">\/* not unique *\/<\/span>\r\n                return(FALSE);\r\n            }\r\n        }\r\n    }\r\n    return(TRUE);\r\n}\r\n\r\nint main()\r\n{\r\n    int unique;\r\n    long v;\r\n\r\n    unique = 0;\r\n    for( v=0; v&lt;10000000000; v++ )\r\n    {\r\n        if( digit_scan(v) )\r\n        {\r\n            printf(\"%ld\\n\",v);\r\n            unique++;\r\n        }\r\n    }\r\n    printf(\"%d unique values\\n\",unique);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Three defined constants are declared: <code>MAX<\/code>, which represents the maximum number of digits in the largest value, ten billion. Then <code>TRUE<\/code> and <code>FALSE<\/code> for return values from the <em>digit_scan()<\/em> function, which make the code more readable.<\/p>\n<p>The <em>digit_scan()<\/em> function uses the <em>snprintf()<\/em> function to convert the value passed (<em>long int<\/em> variable <code>a<\/code>) to a string. Placeholder <code>%ld<\/code> is required to deal with long integers. The string&#8217;s length is saved in variable <code>size<\/code>, which is used in the nested <em>for<\/em> loops to scan the value.<\/p>\n<p>The nested <em>for<\/em> loops work like a bubble sort, comparing each digit in the number with every other digit. Here is where I&#8217;m certain a better solution is available, though I couldn&#8217;t think of one. As soon as two digits match in the inner loop, <code>FALSE<\/code> is returned. Otherwise, if the scan completes with no matches, <code>TRUE<\/code> is returned meaning the value contains unique digits.<\/p>\n<p>The <em>main()<\/em> function features a loop that processes values zero through ten billion. An <em>if<\/em> test calls the <em>digit_scan()<\/em> function. When true, the unique value is output and the <code>unique<\/code> counter incremented.<\/p>\n<p>The sample run outputs 8,877,691 lines of text and takes my computer 26 minutes to complete, but it works. I&#8217;m eager to see other solutions, specifically a method to scan the number for unique digits. I shall be delighted to review such solutions because I assume they exist.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hope you came up with an interesting solution for this month&#8217;s Exercise, one different from my own. The goal is to output unique values from zero through ten billion, values where no two digits repeat.<\/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-6093","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\/6093","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=6093"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6093\/revisions"}],"predecessor-version":[{"id":6102,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6093\/revisions\/6102"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}