Sherlock and Squares Hackerrank Problem Solution

Sherlock and Squares Hackerrank

I think you might go through this problem but here I’m going to make it little more interesting. So the problem is like: Watson gives two integers (A and B) to Sherlock and asks if he can be able to count the number of square integers between A and B(both inclusive) to test his intelligence.

To make it simple Watson also explains: A square integer is an integer which is the square of an 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. 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.

Sample Input

2
3 9
17 24

Sample Output

2
0

Explanation

Test Case #00: In range (3,9)m], 4 and 9 are the two square numbers.
Test Case #01: In range (17,24), there are no square numbers.

solution

Sherlock and Squares Hackerrank

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main(){
int a,b;
scanf("%d");while(scanf("%d%d",&a,&b)>0){
    printf("%d\n",(int)(floor(sqrt(b))-ceil(sqrt(a))+1));}
return 0;}

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

int main() {
    int t;
    cin >> t;
    while(t--) {
        long long int a, b, ans = 0, i;
        cin >> a >> b;
        for(i=1; ; i++) {
            if (i * i > b) break;
            if (i * i <= b && i * i >= a) ans++;
        }
        cout << ans << endl;
    }
    return 0;
}
from math import *
t = input()
for _ in range(t):
    a, b = map(int, raw_input().split())
    a = ceil(sqrt(a))
    b = floor(sqrt(b))
    print int(b-a) + 1

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.