- 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

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,**

n = 5

1

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

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,**

#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; }

Politeness of 13 is 1

- Related Questions & Answers
- Find politeness of a number in java
- Find cubic root of a number in C++
- Find number of cavities in a matrix in C++
- Find power of a number using recursion in C#
- Find sum of digits in factorial of a number in C++
- Find row number of a binary matrix having maximum number of 1s in C++
- Find sum of even factors of a number using C++.
- Find sum of odd factors of a number using 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 the frequency of a digit in a number using C++.
- Find LCM of rational number in C++
- Find number of endless points in C++
- Find number of Positional Elements in C++
- Find most significant set bit of a number in C++

Advertisements