Given a number n, find n’th smart number (1<=n<=1000). Smart number is a number which has at least three distinct prime factors. We are given an upper limit on value of result as MAX
For example 30 is 1st smart number because it has 2, 3, 5 as it’s distinct prime factors. 42 is 2nd smart number because it has 2, 3, 7 as it’s distinct prime factors.
Input : n = 1 Output: 30 // three distinct prime factors 2, 3, 5 Input : n = 50 Output: 273 // three distinct prime factors 3, 7, 13 Input : n = 1000 Output: 2664 // three distinct prime factors 2, 3, 37
The idea is based on Sieve of Eratosthenes. We use an array to use an array prime to keep track of prime numbers. We also use the same array to keep track of the count of prime factors seen so far. Whenever the count reaches 3, we add the number to result.
- Take an array primes and initialize it with 0.
- Now we know that first prime number is i = 2 so mark primes = 1 i.e; primes[i] = 1 indicates that ‘i’ is prime number.
- Now traverse the primes array and mark all multiples of ‘i’ by condition primes[j] -= 1 where ‘j’ is multiple of ‘i’, and check the condition primes[j]+3 = 0 because whenever primes[j] become -3 it indicates that previously it had been multiple of three distinct prime factors. If condition primes[j]+3=0 becomes true that means ‘j’ is a Smart Number so store it in a array result.
- Now sort array result and return result[n-1].
Below is the implementation of above idea.
This article is contributed by Shashak Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Number of factors of very large number N modulo M where M is any prime number
- Find the largest number smaller than integer N with maximum number of set bits
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count number of triplets with product equal to given number with duplicates allowed
- Find minimum number to be divided to make a number a perfect square
- Find smallest possible Number from a given large Number with same count of digits
- Minimum divisor of a number to make the number perfect cube
- Number of times the largest perfect square number can be subtracted from N
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Number of ways to split a binary number such that every part is divisible by 2
- Smallest number dividing minimum number of elements in the Array
- Smallest number dividing minimum number of elements in the array | Set 2
- Largest number dividing maximum number of elements in the array
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Given number of matches played, find number of teams in tournament
- Minimum number of swaps required to make a number divisible by 60
- Previous perfect square and cube number smaller than number N
- Find the minimum number to be added to N to make it a prime number
- Number which has the maximum number of distinct prime factors in the range M to N
- Number of distinct ways to represent a number as sum of K unique primes