/*Square integer is a integer which is a perfect square of any integer. In this , I have tried this with 2 possible solutions*/
1st solution (brute force method):-
#include<stdio.h>
#include<math.h>
int main(){
int n;
long t,a,b;
int i,count=0;
printf("Enter number of test cases\n");//number of ranges
scanf("%d",&n);
while(n>0){
count=0;
scanf("%d%d",&a,&b);
for(i=a;i<=b;i++){
t=sqrt(i);/*function to determine square root of any given number;*/
if(t*t==i)/*checking if this particular number is perfect square*/
count++;
}
n--;
printf("%d\n",count);
}
}
2nd solution (one of the best(optimized way) to determine if number is perfect square or not):-
#include<stdio.h>
#include<math.h>
int main(){
int n,a,b;
int count=0;
printf("Enter number of test cases\n");//number of ranges
scanf("%d",&n);
while(n>0){
scanf("%d%d",&a,&b);
count=floor(sqrt(b))-ceil(sqrt(a))+1;/*optimized way to calculate all perfect squares b/w a &&b;*/
printf("%d\n",count);
n--;
}
o/p:-
Enter number of test cases
2
3 9
2 /*between 3 and 9 (inclusive) only 2 numbers are perfect squares--4 and 9*/
17 24
0
1st solution (brute force method):-
#include<stdio.h>
#include<math.h>
int main(){
int n;
long t,a,b;
int i,count=0;
printf("Enter number of test cases\n");//number of ranges
scanf("%d",&n);
while(n>0){
count=0;
scanf("%d%d",&a,&b);
for(i=a;i<=b;i++){
t=sqrt(i);/*function to determine square root of any given number;*/
if(t*t==i)/*checking if this particular number is perfect square*/
count++;
}
n--;
printf("%d\n",count);
}
}
2nd solution (one of the best(optimized way) to determine if number is perfect square or not):-
#include<stdio.h>
#include<math.h>
int main(){
int n,a,b;
int count=0;
printf("Enter number of test cases\n");//number of ranges
scanf("%d",&n);
while(n>0){
scanf("%d%d",&a,&b);
count=floor(sqrt(b))-ceil(sqrt(a))+1;/*optimized way to calculate all perfect squares b/w a &&b;*/
printf("%d\n",count);
n--;
}
o/p:-
Enter number of test cases
2
3 9
2 /*between 3 and 9 (inclusive) only 2 numbers are perfect squares--4 and 9*/
17 24
0