Probability of getting a perfect square when a random number is chosen in a given range | 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
function findProb ( l , r ) { var countOfPS = ( Math . floor ( Math . sqrt ( r ) ) - Math . ceil ( Math . sqrt ( l ) ) + 1 ) ; var total = r - l + 1 ; var prob = countOfPS / total ; return prob ; } var L = 16 , R = 25 ; document . write ( findProb ( L , R ) ) ;