/*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
If any problem, kindly leave the comment :)
ReplyDeleteMGM Grand Hotel and Casino Tickets - Joliet
ReplyDeleteBuy MGM Grand Hotel and 경기도 출장마사지 Casino tickets at Ticketmaster.com. Find MGM Grand Hotel and Casino 충청북도 출장마사지 venue concert and event schedules, venue information, directions, Dec 31, 2021 수원 출장안마 - Dec 1xbet 먹튀 31, 2021Joliet CasinoDec 31, 2021 - Dec 32, 2021Joliet CasinoDec 31, 2021 - Dec 32, 2021Joliet Casino 논산 출장샵