Hello Programmers, In this post, you will know how to solve the HackerRank Sherlock and Squares Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank Sherlock and Squares Solution
Task
Watson likes to challenge Sherlock’s math ability. He will provide a starting and ending value that describe a range of integers, inclusive of the endpoints. Sherlock must determine the number of square integers within that range.
Note: A square integer is an integer which is the square of an integer, e.g. 1, 4, 9, 16, 25.
Example
a = 24
b = 49
There are three square integers in the range: 25, 36 and 49. Return 3.
Function Description
Complete the squares function in the editor below. It should return an integer representing the number of square integers in the inclusive range from a to b.
squares has the following parameter(s):
- int a: the lower range boundary
- int b: the upper range boundary
Returns
- int: the number of square integers in the range
Input Format
The first line contains q, the number of test cases.
Each of the next q lines contains two space-separated integers, a and b, the starting and ending integers in the ranges.
Constraints
- 1 <= q <= 100
- 1 <= a <= b <= 109
Sample Input
2
3 9
17 24
Sample Output
2
0
Explanation
Test Case #00: In range [3, 9], 4 and 9 are the two square integers.
Test Case #01: In range [17, 24], there are no square integers.
HackerRank Sherlock and Squares Solution
Sherlock and Squares Solution in C
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int t; int a,b,sa,sb; scanf("%d",&t); while(t--) { scanf("%d %d",&a,&b); sa = sqrt(a), sb = sqrt(b); if(sa*sa == a)sa--; printf("%d\n",sb - sa); } /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; }
Sherlock and Squares Solution in Cpp
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int n,m; cin>>n; while (cin>>n>>m) { cout<<(int)(sqrt(m)+0.0000001)-(int)(sqrt(n-1)+0.0000001)<<endl; } return 0; }
Sherlock and Squares Solution in Java
//package contest; import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try{ String line = br.readLine(); int num = Integer.parseInt(line); for(int i = 0; i < num; i++){ String[] inp = br.readLine().split(" "); int a = Integer.parseInt(inp[0]); int b = Integer.parseInt(inp[1]); int count = 0; for(int j = 1; j*j <= b; j++){ if(j*j >= a){ count++; } } System.out.println(count); } } catch(Exception e) { } } }
Sherlock and Squares Solution in Python
from math import sqrt T = int(input()) for i in range(T): count = 0 inp = [int(a) for a in input().split()] c = inp[0] while int(sqrt(c)) != sqrt(c) and c<inp[1]: c+=1 while c <= inp[1] and int(sqrt(c)) == sqrt(c): count += 1 c += 2*sqrt(c)+1 print(count)
Sherlock and Squares Solution using JavaScript
function processData(input) { lines = input.trim().split("\n"); for(var i=1;i<lines.length && lines[i].length > 0;i++){ start = parseInt(lines[i].trim().split(" ")[0]); end = parseInt(lines[i].trim().split(" ")[1]); squares =0; min = Math.sqrt(start); if(min != Math.floor(min)){ min = Math.floor(min) + 1; } max = Math.sqrt(end); if(max != Math.floor(max)){ max = Math.floor(max); } if(max < min){ console.log("0"); } else { console.log((max-min+1)); } } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });
Sherlock and Squares Solution in Scala
object Solution { def main(args: Array[String]) { val t = readLine.toInt; for(i <- 1 to t) println(f(readLine.split(" "))); } def f(n:Array[String]):Long = { return Math.sqrt(n(1).toInt).toLong - Math.sqrt(n(0).toInt-1).toLong; } }
Sherlock and Squares Solution in Pascal
program PSherlockandSquares; Uses math; var T,A,B,i:Longint; begin ReadLn(T); for i:=1 to T do begin Read(A); ReadLn(B); WriteLn(floor(sqrt(B))-ceil(sqrt(A))+1); end; end.
Disclaimer: This problem (Sherlock and Squares) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.