Sherlock and Squares

 Sherlock and Squares


Problem Description


Watson gives two integers ( A and B ) to Sherlock and asks if he can count the number of square integers between A and B (both inclusive).


Note: A square integer is an integer which is the square of any integer. For example, 1, 4, 9, and 16 are some of the square integers as they are squares of 1, 2, 3, and 4, respectively.


Input Format


The first line contains T , the number of test cases. T test cases follow, each in a new line.


Each test case contains two space-separated integers denoting A and B.


Constraints

1<=T<=100

1<=A<=B<=109


Output Format

For each test case, print the required answer in a new line.


Explanation for First Test Case:


In the range 3,9 there are two square numbers which are 4 & 9

In the range 17, 24 there are no square numbers

Logic Test Case 1


Input (stdin)

2


3 9


17 24


Expected Output


2


0

Logic Test Case 2


Input (stdin)

5


10 200


300 400


200 300


1 100


2 40


Expected Output


11


3


3


10


5





CODE AREA


import java.io.*;

import java.util.*;

public class TestClass {

public static void main(String[] args) { 

Scanner s=new Scanner(System.in);

long T=s.nextLong();


for(long i=0;i<T;i++)

{

long count=0;

long A=s.nextLong();

long B=s.nextLong();

int Sqrt=(int)Math.sqrt(B);

for(int j=1;j<=Sqrt;j++)

{

if(((j*j)>=A) &&((j*j)<=B))

{

count++;

}

else

{

count=count;

}

}

System.out.println(""+count);

}

}

}

Post a comment

0 Comments