A polite number is a positive number that can be written as the sum of 2 or more consecutive positive numbers.
The series of polite numbers are
3 5 6 7 9 10 11 12 13 14...
There exists a formula to find the n-th polite number. The formula is n + log2(n + log2(n)). The default log computes with base e. We need to compute using base 2. Divide the default log result with log(2) to get the value of log with base e.
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; double getNthPoliteNumber(double n) { n += 1; return n + (log((n + (log(n) / log(2.0))))) / log(2.0); } int main() { double n = 10; cout << (int)getNthPoliteNumber(n) << endl; return 0; }
If you run the above code, then you will get the following result.
14