Finding The Nth Fortunate Number In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Finding The Nth Fortunate Number In C++

Finding The Nth Fortunate Number In C++

BLUF: Mastering Finding The Nth Fortunate Number In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Finding The Nth Fortunate Number In C++

C++ is renowned for its efficiency. Learn how Finding The Nth Fortunate Number In C++ enables low-level control and high-performance computing in the tutorial below.

Introduction:

Fortunate numbers are defined as positive integers that possess a unique connection with prime factorization. To be classified as a fortunate number, a number must go through a process where its smallest prime factor is continuously eliminated until it reaches the value of 1. Examples of fortunate numbers encompass 3, 5, 7, 13, 17, 19, 23, and various others following the same pattern.

Problem Statement:

This assignment entails creating a C++ application to identify the Nth lucky number. This necessitates efficiently generating lucky numbers and pinpointing the specific number that corresponds to N.

Objective:

The aim of this tutorial is to elucidate a systematic method in C++ programming language for determining the Nth Lucky Number. We will explore the process of generating lucky numbers and furnish a detailed walkthrough on implementing our algorithm.

Methodology:

There is a straightforward method available for determining the nth fortunate number in C++. This can be accomplished by traversing through a sequence of numbers beginning from two and verifying if they fulfill specific criteria that classify them as 'fortunate'. Upon identifying a fortunate number, we increment a counter until we reach the nth fortunate number. When a number is deemed fortunate, we decrease its smallest prime factor until it reaches one.

Algorithm:

  • Initialize a counter with 0 and an index with 2.
  • While the counter is less than N.
  • Check whether the current index is fortunate.
  • If it is fortunate, increment the counter.
  • Increment the index.
  • When the number of counts becomes N, this is our current index, which will be an nth fortunate number.
  • Implementation in C++:

Here is an example of how the algorithm can be implemented using C++:

Example

#include <iostream>
#include <vector>

bool isFortunate(int n) {
    while (n % 2 == 0) {
        n /= 2;
    }
    return n == 1;
}

int findNthFortunate(int N) {
    int count = 0;
    int index = 2;
    while (count < N) {
        if (isFortunate(index)) {
            count++;
        }
        index++;
    }
    return index - 1;
}

int main() {
    int N;
    std::cout << "Enter the value of N: ";
    std::cin >> N;
    std::cout << "The " << N << "th fortunate number is: " << findNthFortunate(N) << std::endl;
    return 0;
}

Output:

Output

Enter the value of N: 10
The 10th fortunate number is: 1024

Explanation:

  • isFortunate(int n): This function receives integer n as input and returns a Boolean value indicating if n happens to be a fortunate number. It performs this by repeatedly dividing n by two until it becomes odd or one. If n equals 1, it means that it was a fortunate number hence true; otherwise, false.
  • findNthFortunate(int N): This function accepts an integer N and returns the Nth fortunate number. In order to do this, it initializes two variables: i) count - to maintain a tally of numbers of luck numbers identified ii) index - through which integers starting from 2 are traversed. After that, while the count is less than or equal to N, using the above misfortunate condition, check for luckiness at each given instant corresponding to every value of the index, including all possible values up to thacpp tutorial in time. If there is any fortunate, increase the count. After leaving the loop, return (index-1), because we have added 1 additional value into the index when calculating the nth fortunate number.
  • main: In this function, the program displays a message prompting the user to input N's value. After that, it calls findNthFortunate(N) to get and print out the Nth fortunate number on the screen.
  • Time and Space Complexities:

  1. Time Complexity:
  • The time complexity of isFortunate function is O(log(n)) because it keeps dividing n by 2 until either odd or 1.
  • In the worst-case scenario, we can have as many as all integers less than or equal to Nth fortune number that need to be checked. Hence, it takes O(N * log(N)) time complexity for this function.
  1. Space Complexity:
  • The space complexity of isFortunate function is O(1) because it only uses a limited amount of extra memory for variables.
  • On the other hand, findNthFortunate has no additional data structures but two integer variables (count and index). It implies that its space complexity is also O(1) .
  • In summary, the time complexity of this code is O(N * log(N)) , while its space complexity is O(1) , where N represents the position of the Nth fortune number.
  • Conclusion:

In summary, determining the nth lucky number requires verifying if a number is lucky through a process of iterating over all integers. By coding the algorithm in C++, we can effectively locate any specified lucky number within the sequence.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience