{"id":25621,"date":"2017-10-15T20:01:14","date_gmt":"2017-10-15T14:31:14","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25621"},"modified":"2017-10-15T20:01:14","modified_gmt":"2017-10-15T14:31:14","slug":"c-programming-lucky-numbers","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-lucky-numbers\/","title":{"rendered":"C programming-Lucky Numbers"},"content":{"rendered":"<p>Lucky numbers are subset of integers. Rather than going into much theory, let us see the process of arriving at lucky numbers,<br \/>\n<span id=\"more-542\"><\/span><br \/>\nTake the set of integers<br \/>\n1,2,3,4,5,6,7,8,9,10,11,12,14,15,16,17,18,19,\u2026\u2026<\/p>\n<p>First, delete every second number, we get following reduced set.<br \/>\n1,3,5,7,9,11,13,15,17,19,\u2026\u2026\u2026\u2026<\/p>\n<p>Now, delete every third number, we get<br \/>\n1, 3, 7, 9, 13, 15, 19,\u2026.\u2026.<\/p>\n<p>Continue this process indefinitely\u2026\u2026<br \/>\nAny number that does NOT get deleted due to above process is called \u201clucky\u201d.<\/p>\n<p>Therefore, set of lucky numbers is 1, 3, 7, 13,\u2026\u2026\u2026<\/p>\n<p>Now, given an integer \u2018n\u2019, write a function to say whether this number is lucky or not.<\/p>\n<pre>    bool isLucky(int n)\r\n<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Algorithm:<\/strong><br \/>\nBefore every iteration, if we calculate position of the given no, then in a given iteration, we can determine if the no will be deleted. Suppose calculated position for the given no. is P before some iteration, and each Ith no. is going to be removed in this iteration, if P &lt; I then input no is lucky, if P is such that P%I == 0 (I is a divisor of P), then input no is not lucky.<\/p>\n<p><strong>Recursive Way:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdio.h&gt;<br\/>#define bool int<br\/> <br\/>\/* Returns 1 if n is a lucky no. ohterwise returns 0*\/<br\/>bool isLucky(int n)<br\/>{<br\/>  static int counter = 2;<br\/> <br\/>  \/*variable next_position is just for readability of<br\/>     the program we can remove it and use n only *\/<br\/>  int next_position = n;<br\/>  if(counter &gt; n)<br\/>    return 1;<br\/>  if(n%counter == 0)<br\/>    return 0;      <br\/> <br\/> \/*calculate next position of input no*\/<br\/>  next_position -= next_position\/counter;<br\/>   <br\/>  counter++;<br\/>  return isLucky(next_position);<br\/>}<br\/> <br\/>\/*Driver function to test above function*\/<br\/>int main()<br\/>{<br\/>  int x = 5;<br\/>  if( isLucky(x) )<br\/>    printf(&quot;%d is a lucky no.&quot;, x);<br\/>  else<br\/>    printf(&quot;%d is not a lucky no.&quot;, x);<br\/>  getchar();<br\/>}<\/code><\/pre> <\/div>\n<p>In next step every 6th no .in sequence will be deleted. 19 will not be deleted after this step because position of 19 is 5th after this step. Therefore, 19 is lucky.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C programming-Lucky Numbers &#8211; Mathematical algorithms &#8211; Lucky numbers are subset of integers. let us see the process of arriving at lucky numbers.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,1,74058],"tags":[74305,74301,74309,74302,74296,74288,74311,74321,74293,74295,74299,74304,74329,74310,74316,74323,74333,74332,74289,74322],"class_list":["post-25621","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-coding","category-mathematical-algorithms","tag-chaldean-numerology-calculator","tag-free-random-number-generator","tag-generate-random-number","tag-give-me-a-random-number","tag-give-me-five-lucky-numbers","tag-how-to-calculate-name-number","tag-how-to-get-lucky-number","tag-lottery-number-generator","tag-luck-generator","tag-luck-india-result-chart","tag-luckindia-result-chart","tag-lucky-draw-generator","tag-may-lucky-number","tag-number-randomizer","tag-online-random-number-generator","tag-pythagorean-numerology-calculator","tag-random-calculator","tag-random-number-selection","tag-recursive-function","tag-this-month-lucky-number"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25621"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25621\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}