Find politeness of a number | Java program to find politeness of a number ; A function to count all odd prime factors of a given number n ; Eliminate all even prime factor of number of n ; n must be odd at this point , so iterate for only odd numbers till sqrt ( n ) ; if i divides n , then start counting of Odd divisors ; If n odd prime still remains then count it ; Driver code
public class Politeness { static int countOddPrimeFactors ( int n ) { int result = 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 ; } result *= divCount + 1 ; } 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 ) ) ; } }