Probability of getting a perfect square when a random number is chosen in a given range | Python3 implementation to find the probability of getting a perfect square number ; Function to return the probability of getting a perfect square number in a range ; Count of perfect squares ; Total numbers in range l to r ; Calculating probability ; Driver code ; Function Call
import math NEW_LINE def findProb ( l , r ) : NEW_LINE INDENT countOfPS = ( math . floor ( math . sqrt ( r ) ) - math . ceil ( math . sqrt ( l ) ) + 1 ) NEW_LINE total = r - l + 1 NEW_LINE prob = countOfPS / total NEW_LINE return prob NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 16 NEW_LINE R = 25 NEW_LINE print ( findProb ( L , R ) ) NEW_LINE DEDENT