# N-th polite number

A polite number is a positive integer that can be written as the sum of two or more consecutive positive integers. Given N, find the N-th polite number.

Examples:

Input : 4 Output : 7 Explanation: The first 3 are 3(1+2), 5(2+3), 6(1+2+3). Input : 7 Output : 11 Explanation: 3, 5, 6, 7, 9, 10, 11.

There exist an interesting pattern that only powers of 2 are not present in series of Polite numbers. Based on this fact, there exist below formula (Lambek–Moser theorem) for N-th polite number.

Here to find Nth polite number we have to take **n as n+1** in the above equation

The inbuilt log function computes log base-e, so dividing it by log base-e 2 will give log base-2 value.

Given below is the implementation of the above approach:

## C++

`// CPP program to find Nth polite number ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to evaluate Nth polite number ` `double` `polite(` `double` `n) ` `{ ` ` ` `n += 1; ` ` ` `double` `base = 2; ` ` ` `return` `n + (` `log` `((n + (` `log` `(n) / ` ` ` `log` `(base))))) / ` `log` `(base); ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `double` `n = 7; ` ` ` ` ` `cout << (` `int` `)polite(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for finding N-th polite number ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find N-th polite number ` ` ` `static` `double` `polite(` `double` `n) ` ` ` `{ ` ` ` `n += ` `1` `; ` ` ` `double` `base = ` `2` `; ` ` ` `return` `n + (Math.log((n + (Math.log(n) / ` ` ` `Math.log(base))))) / Math.log(base); ` ` ` `} ` ` ` ` ` `// driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `double` `n = ` `7` `; ` ` ` `System.out.println((` `int` `)polite(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`import` `math ` `# function to find Nth polite number ` `def` `Polite(n): ` ` ` `n ` `=` `n ` `+` `1` ` ` `return` `(` `int` `)(n` `+` `(math.log((n ` `+` `math.log(n, ` `2` `)), ` `2` `))) ` ` ` `# Driver code ` `n ` `=` `7` `print` `Polite(n) ` |

*chevron_right*

*filter_none*

## C#

`// Java program for finding ` `// N-th polite number ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find N-th polite number ` ` ` `static` `double` `polite(` `double` `n) ` ` ` `{ ` ` ` `n += 1; ` ` ` `double` `base1 = 2; ` ` ` `return` `n + (Math.Log((n + (Math.Log(n) / ` ` ` `Math.Log(base1))))) / ` ` ` `Math.Log(base1); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` `double` `n = 7; ` ` ` `Console.Write((` `int` `)polite(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find ` `// Nth polite number ` ` ` `// function to evaluate ` `// Nth polite number ` `function` `polite(` `$n` `) ` `{ ` ` ` `$n` `+= 1; ` ` ` `$base` `= 2; ` ` ` `return` `$n` `+ (log((` `$n` `+ (log(` `$n` `) / ` ` ` `log(` `$base` `))))) / log(` `$base` `); ` `} ` ` ` `// Driver code ` `$n` `= 7; ` `echo` `((int)polite(` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

11

Reference: Wikipedia

## Recommended Posts:

- Count number of triplets with product equal to given number with duplicates allowed
- Count number of trailing zeros in Binary representation of a number using Bitset
- Find minimum number to be divided to make a number a perfect square
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Number of ways to split a binary number such that every part is divisible by 2
- Number of times the largest perfect square number can be subtracted from N
- Given number of matches played, find number of teams in tournament
- Count Number of animals in a zoo from given number of head and legs
- Program to Convert Octal Number to Binary Number
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Count number of ways to divide a number in 4 parts
- Querying maximum number of divisors that a number in a given range has
- Count number of digits after decimal on dividing a number
- Build Lowest Number by Removing n digits from a given number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.