Count of perfect squares of given length | Python3 Program to count perfect squares of given length ; Function to check if a number is perfect square ; Find floating point value of square root of x . ; If square root is an integer ; Function to return the count of n digit perfect squares ; Initialize result ; Traverse through all numbers of n digits ; Check if current number ' i ' is perfect square ; Driver code
import math ; NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sr = math . sqrt ( x ) ; NEW_LINE return ( ( sr - math . floor ( sr ) ) == 0 ) ; NEW_LINE DEDENT def countSquares ( n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( int ( math . pow ( 10 , ( n - 1 ) ) ) , int ( math . pow ( 10 , n ) ) ) : NEW_LINE INDENT if ( i != 0 and isPerfectSquare ( i ) ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( countSquares ( n ) ) ; NEW_LINE