Count all perfect divisors of a number | Python3 implementation of Naive method to count all perfect divisors ; Utility function to check perfect square number ; function to count all perfect divisors ; Initialize result ; Consider every number that can be a divisor of n ; If i is a divisor ; Driver program to test above function
import math NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sq = ( int ) ( math . sqrt ( x ) ) NEW_LINE return ( x == sq * sq ) NEW_LINE DEDENT def countPerfectDivisors ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 1 , ( int ) ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if isPerfectSquare ( i ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE DEDENT if n / i != i and isPerfectSquare ( n / i ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT print ( " Total ▁ perfect ▁ divisor ▁ of ▁ 16 ▁ = ▁ " , countPerfectDivisors ( 16 ) ) NEW_LINE print ( " Total ▁ perfect ▁ divisor ▁ of ▁ 12 ▁ = ▁ " , countPerfectDivisors ( 12 ) ) NEW_LINE