Count all perfect divisors of a number | Java code to count total perfect Divisors ; Utility function to check perfect square number ; Returns count all perfect divisors of n ; Initialize result ; Consider every number that can be a divisor of n ; If i is a divisor ; Driver code
import java . io . * ; class GFG { static boolean isPerfectSquare ( int n ) { int sq = ( int ) Math . sqrt ( n ) ; return ( n == sq * sq ) ; } static int countPerfectDivisors ( int n ) { int count = 0 ; for ( int i = 1 ; i * i <= n ; ++ i ) { if ( n % i == 0 ) { if ( isPerfectSquare ( i ) ) ++ count ; if ( n / i != i && isPerfectSquare ( n / i ) ) ++ count ; } } return count ; } public static void main ( String [ ] args ) { int n = 16 ; System . out . print ( " Total ▁ perfect ▁ " + " divisors ▁ of ▁ " + n ) ; System . out . println ( " ▁ = ▁ " + countPerfectDivisors ( n ) ) ; n = 12 ; System . out . print ( " Total ▁ perfect ▁ " + " divisors ▁ of ▁ " + n ) ; System . out . println ( " ▁ = ▁ " + countPerfectDivisors ( n ) ) ; } }