
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Lucky Numbers
Lucky numbers are some special integer numbers. From basic numbers, some special numbers are eliminated by their position. Instead of their value, for their position, the numbers are eliminated. The numbers which are not deleted, they are the lucky numbers.
The number deletion follows some rule. At first, every second number are deleted, after that, all 3rd numbers are deleted and so on.
Here is some example −
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 (1 – 25 all) 1 3 5 7 9 11 13 15 17 19 21 23 25 (deleting all 2nd numbers) 1 3 7 9 13 15 19 21 25 (All 3rd numbers are deleted, starting from 5) 1 3 7 9 13 15 21 25 (All 7th numbers are deleted starting from 19)
Input and Output
Input: Put a number to check whether it is lucky or not. Let the number is 13 Output: 13 is a lucky number.
Algorithm
isLuckyNumber(number)
Input − A number.
Output − Check the number is lucky or not.
Begin counter := 2 (It is static data, not be initialized again in recursion call) if counter > n, then return 1 if n mod counter = 0, then return 0 n := n – (n / counter) counter := counter + 1 isLuckyNumber(n) End
Example
#include <iostream> using namespace std; int counter = 2; //used during recursion bool isLuckyNumber(int n) { if(counter > n) return 1; if(n%counter == 0) return 0; n -= n/counter; //n will be next position for recursion counter++; return isLuckyNumber(n); } int main() { int x = 13; if(isLuckyNumber(x)) cout << x<<" is a lucky number."; else cout << x<<" is not a lucky number."; }
Output
13 is a lucky number.
- Related Articles
- Can we distinguish numbers as lucky and unlucky numbers?
- Lucky Numbers in a Matrix in JavaScript
- Finding Lucky Numbers in a Matrix in JavaScript
- C++ code to count number of lucky numbers with k digits
- Why are the numbers 9, 99, 999, 9999 considered 'Lucky Numbers' by the Chinese and many others?
- What is the number 7 considered to be lucky in Britain?
- What is the origin of the superstition in Britain that touching wood is lucky?
- C++ Program to check if a given number is Lucky (all digits are different)
- What is the origin of superstition in Britain that finding a penny on the floor is a lucky sign?
- What Are Whole Numbers? Are All The Numbers Whole Numbers?
- Why are natural numbers called counting numbers?
- Difference between composite numbers and prime numbers.
- What are prime numbers and coprime numbers?
- Counting smaller numbers after corresponding numbers in JavaScript
- Are natural numbers and positive numbers the same?

Advertisements