Lucky numbers are subset of integers. Rather than going into much theory, let us see the process of arriving at lucky numbers,
Take the set of integers
1,2,3,4,5,6,7,8,9,10,11,12,14,15,16,17,18,19,……
First, delete every second number, we get following reduced set.
1,3,5,7,9,11,13,15,17,19,…………
Now, delete every third number, we get
1, 3, 7, 9, 13, 15, 19,….….
Continue this process indefinitely……
Any number that does NOT get deleted due to above process is called “lucky”.
Therefore, set of lucky numbers is 1, 3, 7, 13,………
Now, given an integer ‘n’, write a function to say whether this number is lucky or not
#include<iostream> using namespace std; bool solve(int n) { static int deleteNums=2; //第一轮删除第2i个数字(i=1,2,3...n) if(n<deleteNums) return true; if(n%deleteNums==0) return false; n-=n/deleteNums; //在n的前面为2的倍数的数字减掉,因为他们已经被删除了,n的值改变 ++deleteNums; //下一轮递归再删除第3i个数字(i=1,2,3...n),并以此类推到4i,5i... solve(n); } int main(int argc,char *argv[]) { if(1==argc) return 1; if(solve(atoi(argv[1]))) cout<<"Yes"<<endl; else cout<<"No"<<endl; return 0; }