因为质因子恰好出现两次,所以对x开根号求解,然后枚举x附近的数,根据素数定理,素数的平均距离为logn
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef long long LL;
const int M=1000000007;
const int N=34000+10;
const int ALL=10000000;
int h,p[N];
LL n;
void prime_table()
{
memset(p,0,sizeof p);
h=0;
for (int i=2;i<N;i++)if (!p[i]){
h++;p[h]=i;
for (int j=i;j<N;j+=i)p[j]=1;
}
}
void work()
{
cin>>n;
LL m=LL(sqrt(n+0.5)),MIN=max(2LL,m-ALL/h),MAX=min(2000000000LL,m+ALL/h);
LL Ans=1000000000000000000LL;
for (LL i=m;i>=MIN;i--){
int Flag=1;LL k=i;
for (int j=1;j<=h && k>=p[j];j++)if (k%p[j]==0){
int cnt=0;while (k%p[j]==0){
k/=p[j];cnt++;if (cnt>=2){Flag=0;break;}
}
if (cnt>=2){Flag=0;break;}
}
if (!Flag)continue;
Ans=min(Ans,abs(i*i-n));break;
}
for (LL i=m+1;i<=MAX;i++){
int Flag=1;LL k=i;
for (int j=1;j<=h && k>=p[j];j++)if (k%p[j]==0){
int cnt=0;while (k%p[j]==0){
k/=p[j];cnt++;if (cnt>=2)break;
}
if (cnt>=2){Flag=0;break;}
}
if (!Flag)continue;
Ans=min(Ans,abs(i*i-n));break;
}
cout<<Ans<<endl;
}
int main()
{
prime_table();
int Case;scanf("%d",&Case);
while (Case--)work();
return 0;
}