{"id":138,"date":"2014-08-11T09:08:15","date_gmt":"2014-08-11T16:08:15","guid":{"rendered":"http:\/\/willclausen.com\/?p=138"},"modified":"2014-08-11T09:09:12","modified_gmt":"2014-08-11T16:09:12","slug":"kaprekar-numbers","status":"publish","type":"post","link":"http:\/\/willclausen.com\/?p=138","title":{"rendered":"Kaprekar Numbers"},"content":{"rendered":"<p>Below is my solution to the Programming Praxis problem from Sept. 21, 2010. The problem was to find all Kaprekar numbers less than 1000. I wrote my solution in C++ to refresh my skills a little. For more info, see\u00a0<a href=\"http:\/\/programmingpraxis.com\/2010\/09\/21\/kaprekar-numbers\/\">this link<\/a>.<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\r\n\/\/ Author: William Clausen\r\n\/\/ Date: Aug. 10, 2014\r\n\/\/ Description: Programming Praxis problem from Sep. 21, 2010\r\n\r\n\/\/ Write a program to determine Kaprekar numbers less than a given input\r\n\r\n#include &lt;vector&gt;\r\n#include &lt;iostream&gt; \/\/ for testing in main\r\n#include &lt;cmath&gt; \/\/ for pow\r\n\r\nusing namespace std;\r\n\r\n\/\/ Helper function that returns a power of 10 where the exponent is equal\r\n\/\/ to the number of digits in number\r\nint numDigits(int number) {\r\n\tint result = 1;\r\n\r\n\t\/\/ Next, add the appropriate number of zeros to the result.\r\n\twhile (result &lt; number) {\r\n\t\tresult *= 10;\r\n\t}\r\n\r\n\treturn result;\r\n}\r\n\r\n\/\/ Helper function that returns true if the input is a Kaprekar number and\r\n\/\/ false otherwise.\r\nbool isKaprekar(int number) {\r\n\tint square = pow(number, 2);\r\n\r\n\t\/\/ We need to know the number of digits in the input so figure that out.\r\n\t\/\/ Get a power of ten where the exponent equals the number of digits\r\n\tint modulo = numDigits(number);\r\n\r\n\t\/\/ Get the last n digits of the square\r\n\tint lastNDigits = square % modulo;\r\n\r\n\t\/\/ Get the first n or n-1 digits\r\n\tint firstDigits = square \/ modulo;\r\n\r\n\t\/\/ Add these numbers to determine if it's a Kaprekar number\r\n\tif (lastNDigits + firstDigits == number) {\r\n\t\treturn true;\r\n\t}\r\n\r\n\treturn false;\r\n}\r\n\r\n\/\/ Function that returns a vector of all Kaprekar numbers less than the input.\r\nvector&lt;int&gt; kaprekar(int limit) {\r\n\t\/\/ Create the vector of Kaprekar numbers to be returned\r\n\tvector&lt;int&gt; result;\r\n\r\n\t\/\/ Loop through all the numbers and if they are kaprekar numbers, add them\r\n\t\/\/ to the vector.\r\n\tfor (int i = 0; i &lt; limit; ++i) {\r\n\t\tif (isKaprekar(i)) {\r\n\t\t\tresult.push_back(i);\r\n\t\t}\r\n\t}\r\n\r\n\treturn result;\r\n}\r\n\r\nint main() {\r\n\t\/\/ Determine the Kaprekar numbers less than 1000.\r\n\tvector&lt;int&gt; answer = kaprekar(1000);\r\n\r\n\tcout &lt;&lt; &quot;The Kaprekar numbers less than 1000 are: &quot;;\r\n\tfor (int i = 0; i &lt; answer.size(); ++i) {\r\n\t\tcout &lt;&lt; answer&#x5B;i] &lt;&lt; &quot;, &quot;;\r\n\t}\r\n\tcout &lt;&lt; &quot;\\n&quot;;\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Below is my solution to the Programming Praxis problem from Sept. 21, 2010. The problem was to find all Kaprekar numbers less than 1000. I wrote my solution in C++ to refresh my skills a little. For more info, see\u00a0this link. \/\/ Author: William Clausen \/\/ Date: Aug. 10, 2014 \/\/ Description: Programming Praxis problem [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[37,11],"class_list":["post-138","post","type-post","status-publish","format-standard","hentry","category-programming-praxis","tag-kaprekar-numbers","tag-programming-praxis-2"],"_links":{"self":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=138"}],"version-history":[{"count":2,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":140,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/138\/revisions\/140"}],"wp:attachment":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}