1000000 番目のプリミティブ数を 10 分以内に決定するコードが必要です。以下のクラスを作成しましたが、必要なほど高速ではありません。このクラスを最適化して計算速度を改善するにはどうすればよいですか? を呼び出すことで、このクラスを使用できますget_thPrimitive(int number)
。それは長い時間を返します。
public class PrimitiveF
{
// this is our class named PrimitiveF
private static boolean isPrimitive(long n)
{
//isPrimitive method is a private static method that is used in get_thPrimitive method
//this method tells us if the number is primitive or not
long p=(long)Math.sqrt(n);//here we get square of the input number
int i=2;
while(i<=p)//now we check the nember is primitive or not if it is primitive we'll return true
{
int k=0;
for(int j=2;j<=i;j++)
if(i%j==0)
k++;
if(k==1)
{
if(n%i == 0)
return false;
}
i++;
}
return true;
}
public static long get_thPrimitive(int n)//get_thPrimitive method is a public static //method
{
//this method would give us the Xth primitive number
int count=0,i=2;
while(count < n)
{
if(isPrimitive(i))
count++;
i++;
}
return --i;
}
}