- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find politeness of a number in C++

In this problem, we are given a positive integer N. Our task is to find the politeness of a number.

**Polite Number** is a number which can be expressed as a sum of two or more consecutive numbers.

**Politeness of a number** is defined as the number of ways the number can be expressed as sum of consecutive integers.

**Let’s take an example to understand the problem,**

## Input

n = 5

## Output

1

## Explanation

2 + 3 = 5, is the only consecutive sum.

## Solution Approach

A simple solution to the problem is to check all consecutive numbers till N and if their sum is equal to N, increase count which is the politeness of the number.

This solution is not efficient but a complicated but efficient solution is using factorisation. Using the formula for politeness that happens to be product of count of odd factors i.e.

If the number is represented as N = ax * by * cz… Politeness = [(x + 1)*(y +1)*(z + 1)... ] - 1

**Program to illustrate the working of our solution,**

## Example

#include <iostream> using namespace std; int calcPolitenessNumber(int n){ int politeness = 1; while (n % 2 == 0) n /= 2; for (int i = 3; i * i <= n; i += 2) { int divCount = 0; while (n % i == 0) { n /= i; ++divCount; } politeness *= divCount + 1; } if (n > 2) politeness *= 2; return (politeness - 1); } int main(){ int n = 13; cout<<"Politeness of "<<n<<" is "<<calcPolitenessNumber(n); return 0; }

## Output

Politeness of 13 is 1

- Related Questions & Answers
- Find politeness of a number in java
- Find the maximum number of composite summands of a number in Python
- Find cubic root of a number in C++
- Find number of cavities in a matrix in C++
- Find row number of a binary matrix having maximum number of 1s in C++
- Find power of a number using recursion in C#
- Find all divisors of a natural number in java
- How to find power of a number in Python?
- Python to Find number of lists in a tuple
- Find number of spaces in a string using JavaScript
- Find sum of digits in factorial of a number in C++
- Find count of digits in a number that divide the number in C++
- Find the Number of 1 Bits in a large Binary Number in C++
- Find all prime factors of a number - JavaScript
- Java Program to Find Factorial of a number