Sherlock and Squares
/*
Formula to find number of perfect square between two numbers:
[ floor(sqrt(b)) - ceil(sqrt(a)) + 1; ]
why floor b ?
----- Because we have to take before b, i mean <=b.
why ceil a ?
----- Because we have to take after a, i mean >=a.
[ for(i=a; i<=b;i++) ]
Therefore, the result will be [ floor and ceil square root difference between two numbers plus 1]
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
int t;
cin >> t;
while(t--)
{
LL a,b,i,cnt=0;
cin >> a >> b;
LL res = floor(sqrt(b)) - ceil(sqrt(a)) + 1;
cout << res << "\n";
}
return 0;
}
Formula to find number of perfect square between two numbers:
[ floor(sqrt(b)) - ceil(sqrt(a)) + 1; ]
why floor b ?
----- Because we have to take before b, i mean <=b.
why ceil a ?
----- Because we have to take after a, i mean >=a.
[ for(i=a; i<=b;i++) ]
Therefore, the result will be [ floor and ceil square root difference between two numbers plus 1]
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
int t;
cin >> t;
while(t--)
{
LL a,b,i,cnt=0;
cin >> a >> b;
LL res = floor(sqrt(b)) - ceil(sqrt(a)) + 1;
cout << res << "\n";
}
return 0;
}
Comments
Post a Comment