Given two integers **L** and **R** that denote a range, the task is to find the probability of getting a perfect square number when a random number is chosen in the range **L to R**.

**Examples:**

Input:L = 6, R = 20

Output:0.133333

Explanation:

Perfect squares in range [6, 20] = {9, 16} => 2 perfect squares

Total numbers in range [6, 20] = 15

Probability = 2 / 15 = 0.133333

Input:L = 16, R = 25

Output:0.2

**Approach:** The key observation in this problem is the count of the perfect squares in the range from 0 to a number can be computed with the given formulae:

// Count of perfect squares in the range 0 to N is given as

Count of perfect squares = Floor(sqrt(N))

Similarly, the count of the perfect squares in the given range can be computed with the help of the above formulae as follows:

Count of perfect Squares[L, R] = floor(sqrt(R)) – ceil(sqrt(L)) + 1

Total numbers in the range = R – L + 1

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// probability of getting a ` `// perfect square number ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the probability ` `// of getting a perfect square ` `// number in a range ` `float` `findProb(` `int` `l, ` `int` `r) ` `{ ` ` ` `// Count of perfect squares ` ` ` `float` `countOfPS = ` `floor` `(` `sqrt` `(r)) - ` `ceil` `(` `sqrt` `(l)) + 1; ` ` ` ` ` `// Total numbers in range l to r ` ` ` `float` `total = r - l + 1; ` ` ` ` ` `// Calculating probability ` ` ` `float` `prob = (` `float` `)countOfPS / (` `float` `)total; ` ` ` `return` `prob; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `L = 16, R = 25; ` ` ` `cout << findProb(L, R); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// probability of getting a ` `// perfect square number ` ` ` `class` `GFG{ ` ` ` `// Function to return the probability ` `// of getting a perfect square ` `// number in a range ` `static` `float` `findProb(` `int` `l, ` `int` `r) ` `{ ` ` ` ` ` `// Count of perfect squares ` ` ` `float` `countOfPS = (` `float` `) (Math.floor(Math.sqrt(r)) - ` ` ` `Math.ceil(Math.sqrt(l)) + ` `1` `); ` ` ` ` ` `// Total numbers in range l to r ` ` ` `float` `total = r - l + ` `1` `; ` ` ` ` ` `// Calculating probability ` ` ` `float` `prob = (` `float` `)countOfPS / (` `float` `)total; ` ` ` `return` `prob; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `L = ` `16` `, R = ` `25` `; ` ` ` `System.out.print(findProb(L, R)); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find ` `# the probability of getting a ` `# perfect square number ` `import` `math ` ` ` `# Function to return the probability ` `# of getting a perfect square ` `# number in a range ` `def` `findProb(l, r): ` ` ` ` ` `# Count of perfect squares ` ` ` `countOfPS ` `=` `(math.floor(math.sqrt(r)) ` `-` ` ` `math.ceil(math.sqrt(l)) ` `+` `1` `) ` ` ` ` ` `# Total numbers in range l to r ` ` ` `total ` `=` `r ` `-` `l ` `+` `1` ` ` ` ` `# Calculating probability ` ` ` `prob ` `=` `countOfPS ` `/` `total ` ` ` ` ` `return` `prob ` ` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` ` ` `L ` `=` `16` ` ` `R ` `=` `25` ` ` ` ` `print` `(findProb(L, R)) ` ` ` `# This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the probability ` `// of getting a perfect square number ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to return the probability ` `// of getting a perfect square ` `// number in a range ` `static` `float` `findProb(` `int` `l, ` `int` `r) ` `{ ` ` ` ` ` `// Count of perfect squares ` ` ` `float` `countOfPS = (` `float` `)(Math.Floor(Math.Sqrt(r)) - ` ` ` `Math.Ceiling(Math.Sqrt(l)) + 1); ` ` ` ` ` `// Total numbers in range l to r ` ` ` `float` `total = r - l + 1; ` ` ` ` ` `// Calculating probability ` ` ` `float` `prob = (` `float` `)countOfPS / (` `float` `)total; ` ` ` `return` `prob; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `L = 16, R = 25; ` ` ` ` ` `Console.Write(findProb(L, R)); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

**Output:**

0.2

**Time Complexity:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Array range queries to find the number of perfect square elements with updates
- Find smallest perfect square number A such that N + A is also a perfect square number
- Check if a number is perfect square without finding square root
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Probability for three randomly chosen numbers to be in AP
- Random number generator in arbitrary probability distribution fashion
- Largest number that is not a perfect square
- Check if given number is perfect square
- Perfect Square factors of a Number
- Find minimum number to be divided to make a number a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Previous perfect square and cube number smaller than number N
- Largest factor of a given number which is a perfect square
- Find the Next perfect square greater than a given number
- Largest perfect square number in an Array
- Largest Divisor of a Number not divisible by a perfect square
- Check whether the number can be made perfect square after adding K
- Least number to be added to or subtracted from N to make it a Perfect Square
- Largest N digit Octal number which is a Perfect square

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.