我需要代码来在不到 10 分钟的时间内确定第 1000000 个原始数。我已经写了下面的课程,但它没有我需要的那么快。如何优化这个类来提高它的计算速度?您可以通过调用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;
}
}