HackerRank Algorithm Implementaton Problems Solutions.
We will solve Today Sherlock's home hackerrank solution in Java And Python.
Problem :
Watson likes to challenge Sherlock’s math ability. He will provide a starting and ending value describing a range of integers. Sherlock must determine the number of square integers within that range, inclusive of the endpoints.
Note: A square integer is an integer which is the square of an integer, e.g. 1, 4, 9, 16, 25.
For example, the range is a = 24 and b = 49, inclusive. There are three square integers in the range: 25, 36 and 49.
Read full problem : Sherlock and Squares.
Solution :
JAVA 7 Solution
public static int squares(int a, int b) {
int count = 0;
int i = a;
while (i <= b) {
int sqrt = (int) Math.sqrt(i);
if (Math.pow(sqrt, 2) == i) {
count++;
}
i = (int) Math.pow(sqrt + 1, 2);
}
return count;
}
python Solution
def squares(a, b):
return int(b**0.5)-int((a-1)**0.5)