Probability of getting a perfect square when a random number is chosen in a given range | Java 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
class GFG { static float findProb ( int l , int r ) { float countOfPS = ( float ) ( Math . floor ( Math . sqrt ( r ) ) - Math . ceil ( Math . sqrt ( l ) ) + 1 ) ; float total = r - l + 1 ; float prob = ( float ) countOfPS / ( float ) total ; return prob ; } public static void main ( String [ ] args ) { int L = 16 , R = 25 ; System . out . print ( findProb ( L , R ) ) ; } }