Count Squares
PROBLEM :
Given a sample space S consisting of all perfect squares starting from 1, 4, 9 and so on. You are given a number N, you have to print the number of integers less than N in the sample space S.
Input :
The first line contains an integer T, denoting the number of test cases.Then T test cases follow. The first line of each test case contains an integer N, denoting the number.
Output :
Print the answer of each test case in a new line.
Constraints :
1<=T<=100
1<=N<=10^18
Example
Input :
2
9
3
Output :
2
1
--------------------------------------------------------------------------------
SIMPLE c IMPLEMENTATION :
--------------------------------------------------------------------------------
#include <stdio.h>
int square(int n)
{
int i=1,temp=0,count=0;
while(temp<n)
{
temp=i*i;
count=count+1;
i++;
}
return count-1;
}
int main() {
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n",square(n));
}
return 0;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
long long t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<floor(sqrt(no-1))<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Given a sample space S consisting of all perfect squares starting from 1, 4, 9 and so on. You are given a number N, you have to print the number of integers less than N in the sample space S.
Input :
The first line contains an integer T, denoting the number of test cases.Then T test cases follow. The first line of each test case contains an integer N, denoting the number.
Output :
Print the answer of each test case in a new line.
Constraints :
1<=T<=100
1<=N<=10^18
Example
Input :
2
9
3
Output :
2
1
--------------------------------------------------------------------------------
SIMPLE c IMPLEMENTATION :
--------------------------------------------------------------------------------
#include <stdio.h>
int square(int n)
{
int i=1,temp=0,count=0;
while(temp<n)
{
temp=i*i;
count=count+1;
i++;
}
return count-1;
}
int main() {
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n",square(n));
}
return 0;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
long long t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<floor(sqrt(no-1))<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Comments
Post a Comment