Sherlock and Squares

Sherlock and Squares

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.

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):

  • a: an integer, the lower range boundary
  • b: an integer, the uppere range boundary

Input Format

The first line contains q, the number of test cases.

Each of the next q lines contains two space-separated integers denoting a and b, the starting and ending integers in the ranges.

Constraints

1 <= q <= 100

1 <= a <= b <= 109

Output Format

For each test case, print the number of square integers in the range on a new line.

Sample Input

1
2
3
2
3 9
17 24

Sample Output

1
2
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.


Solution

1
2
3
4
5
6
7
// Complete the squares function below.
function squares(a, b) {
let lower = Math.ceil(Math.sqrt(a));
let upper = Math.floor(Math.sqrt(b));

return lower > upper ? 0 : upper - lower + 1;
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×