N’th Smart Number in C++

A smart number is a number that contains at least three distinct prime factors. You are given a number N. Find the n-th smart number.

The smart number series are

30, 42, 60, 66, 70, 78...

Algorithm

  • Initialise the number N.
  • Initialise the count to 0.
  • Write a function that checks whether the given number is prime or not.
  • Write a function that checks whether the number is smart or not.
  • Write a loop that iterates from 30 as first smart number is 30.
    • Check whether the current number is smart number or not using the prime number function.
    • Increment the count by 1 when you find a smart number.
    • Return the smart number when you count is equals to N.

Implementation

Following is the implementation of the above algorithm in C++

#include
using namespace std;
bool isPrime(int n) {
   if (n 

Output

If you run the above code, then you will get the following result.

174
Updated on: 2021-10-23T06:31:40+05:30

256 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements