
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
N-th polite number in C++
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.
Algorithm
- Algorithm to the n-th polite number is straightforward.
- Initialise the number N.
- Use the above formula to compute the n-th polite number.
- Make sure to increment the value of n by 1 before computing the n-th polite number.
Implementation
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; }
Output
If you run the above code, then you will get the following result.
14
- Related Articles
- N-th Tribonacci Number in C++
- N-th root of a number in C++
- C Program for n-th even number
- C Program for n-th odd number
- C/C++ Program for the n-th Fibonacci number?
- N-th Fibonacci number in Python Program
- n-th number whose sum of digits is ten in C++
- Python Program for n-th Fibonacci number
- Java Program for n-th Fibonacci number
- Find the k-th smallest divisor of a natural number N in C++
- n-th number with digits in {0, 1, 2, 3, 4, 5} in C++
- Find M-th number whose repeated sum of digits of a number is N in C++
- Find n-th node of inorder traversal in C++
- Finding n-th number made of prime digits (2, 3, 5 and 7) only in C++
- Find n-th element from Stern’s Diatomic Series in C++

Advertisements