1004 质方数



Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem Description


  小明天生对数字比较敏感,3岁的时候就能背诵圆周率一百位。

  现在,小明慢慢长大了,但依然很喜欢数字,最近,他迷上了质数和平方数,并且自己把质数的平方命名为“质方数”。
  现在,他在研究这样一个问题:距离一个正整数N最接近的质方数是多少?

Input


输入数据第一行是一个正整数T(T<=20),表示有T组输入数据。
接下来T行,每行输入一个正整数N(1<=N<=10^8)。


Output

对于每组数据,请输出距离N最接近的质方数,每组输出占一行。


 

Sample Input

2 1 10


 



Sample Output


4 9


 

#include<iostream>
#include<cmath>
using namespace std;
#define Max 10000
int a[Max+1]= {1,1,0};
int main()
{
for( int i=2; i<=5000; i++ )
for( int j=i<<1; j<Max+1; j+=i )
{
a[j]=1;
}
int T;
int N;
cin>>T;
while( T-- )
{
cin>>N;
int temp=sqrt(N);
if( temp*temp == N && a[temp]==0 )
cout<<N<<endl;
else if( temp==1 )
cout<<4<<endl;
else
{
int i, j;
for( i=temp; i>=2; i-- )
{
if( a[i]==0 )
break;
}
for( j=temp+1; j<=Max; j++ )
{
if( a[j]==0 )
break;
}
if( N-i*i <= j*j-N )
cout<<i*i<<endl;
else
cout<<j*j<<endl;
}
}
}


总是望着曾经的空间发呆,那些说好不分开的朋友不在了,转身,陌路。 熟悉的,安静了, 安静的,离开了, 离开的,陌生了, 陌生的,消失了, 消失的,陌路了。