From daa83082ce167dafcc9b1f976f6d472e1c14bdd7 Mon Sep 17 00:00:00 2001 From: ashishkore <43812466+ashishkore@users.noreply.github.com> Date: Thu, 31 Oct 2019 22:04:20 +0530 Subject: [PATCH] Create Lucky Numbers --- Lucky Numbers | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Lucky Numbers diff --git a/Lucky Numbers b/Lucky Numbers new file mode 100644 index 0000000..4a4b040 --- /dev/null +++ b/Lucky Numbers @@ -0,0 +1,39 @@ +// C++ program for Lucky Numbers +#include +using namespace std; +#define bool int + +/* Returns 1 if n is a lucky no. +otherwise returns 0*/ +bool isLucky(int n) +{ + static int counter = 2; + + /*variable next_position is just for + readability of the program we can + remove it and use n only */ + int next_position = n; + if(counter > n) + return 1; + if(n % counter == 0) + return 0; + + /*calculate next position of input no*/ + next_position -= next_position / counter; + + counter++; + return isLucky(next_position); +} + +// Driver Code +int main() +{ + int x = 5; + if( isLucky(x) ) + cout << x << " is a lucky no."; + else + cout << x << " is not a lucky no."; +} + +// This code is contributed +// by rathbhupendra