text: Find politeness of a number | Java program for the above approach ; Function to find politeness ; sqrt ( 2 * n ) as max length will be when the sum starts from 1 which follows the equation n ^ 2 - n - ( 2 * sum ) = 0 ; Driver Code
code: import java . lang . Math ; public class Main { static int politness ( int n ) { int count = 0 ; for ( int i = 2 ; i <= Math . sqrt ( 2 * n ) ; i ++ ) { int a ; if ( ( 2 * n ) % i != 0 ) continue ; a = 2 * n ; a /= i ; a -= ( i - 1 ) ; if ( a % 2 != 0 ) continue ; a /= 2 ; if ( a > 0 ) { count ++ ; } } return count ; } 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 ) ) ; } }