我的问题是我的程序找不到第 10001 个素数。所以它永远不会停止,而我仍然不知道第 10001 个素数。我很乐意通过我的实现来解决问题。谢谢:)
public class problem7 {
public static boolean sonuc=true;
public static void asalmi(int j)
{
int counter=0;
int asayac=0;
for(int k=1;k<=j;k++)
{
if(j%k==0)
{
counter++;
}
}
if(counter==2)
{
// Only factors are 1 and j, so j is prime
System.out.println(j);
asayac++;
counter=0;
if(asayac==10001)
{
System.out.println(j);
sonuc=false;
}
}
}
public static void main(String[] args)
{
int i=1;
while(sonuc)
{
asalmi(i);
i++;
}
}
}
最佳答案
每次调用asalmi时,都会将局部变量asayac设置为0。这应该是一个静态变量,而不是局部变量。
关于java - 为什么我的程序找不到第 10001 个素数? [项目欧拉问题7],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28175253/