Given an integer n. Find politeness of number n. Politeness of a number is defined as the number of ways it can be expressed as the sum of consecutive integers.
Input: n = 15 Output: 3 Explanation: There are only three ways to express 15 as sum of consecutive integers i.e., 15 = 1 + 2 + 3 + 4 + 5 15 = 4 + 5 + 6 15 = 7 + 8 Hence answer is 3 Input: n = 9; Output: 2 There are two ways of representation: 9 = 2 + 3 + 4 9 = 4 + 5
Naive approach is to run a loop one inside another and find the sum of every consecutive integers up to n. Time complexity of this approach will be O(n2) which will not be sufficient for large value of n.
Efficient approach is to use factorization. We factorize the number n and count the number of odd factors. Total number of odd factors (except 1) is equal to politeness of the number. Refer this for proof of this fact. In general if a number can be represented as ap * bq * cr … where a, b, c, … are prime factors of n. If a = 2 (even) then discard it and count total number of odd factors which can be written as [(q + 1) * (r + 1) * …] – 1 (Here 1 is subtracted because single term in representation is not allowed).
How does above formula work? The fact is, if a number is expressed as ap * bq * cr … where a, b, c, … are prime factors of n, then number of divisors is (p+1)*(q+1)*(r+1) ……
To simplify, let there be one factor and number is expressed as ap. Divisors are 1, a, a2, …. ap. The count of divisors is p+1. Now let us take a slightly more complicated case apbp. The divisors are :
1, a, a2, …. ap
b, ba, ba2, …. bap
b2, b2a, b2a2, …. b2ap
…………….
…………….
bq, bqa, bqa2, …. bqap
The count of above terms is (p+1)*(q+1). Similarly, we can prove for more prime factors.
Illustration : For n = 90, decomposition of prime factors will be as follows:-
=> 90 = 2 * 32 * 51. The power of odd prime factors 3, 5 are 2 and 1 respectively. Apply above formula as: (2 + 1) * (1 + 1) -1 = 5. Hence 5 will be the answer. We can crosscheck it. All odd factors are 3, 5, 9, 15 and 45.
Below is the program of above steps:-
C++
// C+ program to find politeness of number #include <iostream> using namespace std; // A function to count all odd prime factors // of a given number n int countOddPrimeFactors(int n) { int result = 1; // Eliminate all even prime factor of number of n while (n % 2 == 0) n /= 2; // n must be odd at this point, so iterate for only // odd numbers till sqrt(n) for (int i = 3; i * i <= n; i += 2) { int divCount = 0; // if i divides n, then start counting of // Odd divisors while (n % i == 0) { n /= i; ++divCount; } result *= divCount + 1; } // If n odd prime still remains then count it if (n > 2) result *= 2; return result; } int politness(int n) { return countOddPrimeFactors(n) - 1; } // Driver program to test above function int main() { int n = 90; cout << "Politness of " << n << " = " << politness(n) << "\n"; n = 15; cout << "Politness of " << n << " = " << politness(n) << "\n"; return 0; } |
Java
// Java program to find politeness of a number public class Politeness { // A function to count all odd prime factors // of a given number n static int countOddPrimeFactors(int n) { int result = 1; // Eliminate all even prime factor of number of n while (n % 2 == 0) n /= 2; // n must be odd at this point, so iterate // for only odd numbers till sqrt(n) for (int i = 3; i * i <= n; i += 2) { int divCount = 0; // if i divides n, then start counting of // Odd divisors while (n % i == 0) { n /= i; ++divCount; } result *= divCount + 1; } // If n odd prime still remains then count it if (n > 2) result *= 2; return result; } static int politness(int n) { return countOddPrimeFactors(n) - 1; } public static void main(String[] args) { int n = 90; System.out.println("Politness of " + n + " = " + politness(n)); n = 15; System.out.println("Politness of " + n + " = " + politness(n)); } } // This code is contributed by Saket Kumar |
Python
# Python program to find politeness of number # A function to count all odd prime factors # of a given number n def countOddPrimeFactors(n) : result = 1; # Eliminate all even prime factor of # number of n while (n % 2 == 0) : n /= 2 # n must be odd at this point, so iterate # for only odd numbers till sqrt(n) i = 3 while i * i <= n : divCount = 0 # if i divides n, then start counting # of Odd divisors while (n % i == 0) : n /= i divCount = divCount + 1 result = result * divCount + 1 i = i + 2 # If n odd prime still remains then count it if (n > 2) : result = result * 2 return result def politness( n) : return countOddPrimeFactors(n) - 1; # Driver program to test above function n = 90print "Politness of ", n, " = ", politness(n) n = 15print "Politness of ", n, " = ", politness(n) # This code is contributed by Nikita Tiwari. |
C#
// C# program to find politeness of a number. using System; public class GFG { // A function to count all odd prime // factors of a given number n static int countOddPrimeFactors(int n) { int result = 1; // Eliminate all even prime factor // of number of n while (n % 2 == 0) n /= 2; // n must be odd at this point, so // iterate for only odd numbers // till sqrt(n) for (int i = 3; i * i <= n; i += 2) { int divCount = 0; // if i divides n, then start // counting of Odd divisors while (n % i == 0) { n /= i; ++divCount; } result *= divCount + 1; } // If n odd prime still remains // then count it if (n > 2) result *= 2; return result; } static int politness(int n) { return countOddPrimeFactors(n) - 1; } // Driver code public static void Main() { int n = 90; Console.WriteLine("Politness of " + n + " = " + politness(n)); n = 15; Console.WriteLine("Politness of " + n + " = " + politness(n)); } } // This code is contributed by nitin mittal. |
PHP
<?php // PHP program to find // politeness of number // A function to count all // odd prime factors of a // given number n function countOddPrimeFactors($n) { $result = 1; // Eliminate all even prime // factor of number of n while($n % 2 == 0) $n /= 2; // n must be odd at this // point, so iterate for only // odd numbers till sqrt(n) for ($i = 3; $i * $i <= $n; $i += 2) { $divCount = 0; // if i divides n, then // start counting of // Odd divisors while($n % $i == 0) { $n /= $i; ++$divCount; } $result *= $divCount + 1; } // If n odd prime still // remains then count it if ($n > 2) $result *= 2; return $result; } function politness($n) { return countOddPrimeFactors($n) - 1; } // Driver Code $n = 90; echo "Politness of " , $n , " = " , politness($n), "\n"; $n = 15; echo "Politness of " , $n , " = " , politness($n) ,"\n"; // This code is contributed by nitin mittal. ?> |
Output: Politness of 90 = 5 Politness of 15 = 3
Time complexity: O(sqrt(n))
Auxiliary space: O(1)
Reference: Wikipedia
This article is contributed by Shubham Bansal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find minimum number to be divided to make a number a perfect square
- Find the largest good number in the divisors of given number N
- Number of factors of very large number N modulo M where M is any prime number
- Find all divisors of a natural number | Set 1
- Find sum of divisors of all the divisors of a natural number
- Find minimum sum of factors of number
- Queries to find whether a number has exactly four distinct factors or not
- Find largest prime factor of a number
- Find sum of odd factors of a number
- Find sum of even factors of a number
- Find maximum power of a number that divides a factorial
- Find a number that divides maximum array elements
- Super Ugly Number (Number whose prime factors are in given set)
- Number with maximum number of prime factors
- Total number of divisors for a given number
- Check if a number is divisible by all prime divisors of another number
- k-Rough Number or k-Jagged Number
- Queries on sum of odd number digit sums of all the factors of a number
- Number which has the maximum number of distinct prime factors in the range M to N
- Check if a number is an Unusual Number or not

