【问题标题】:Errors in Java program that is supposed to show the 10,000th prime number? [closed]应该显示第 10,000 个素数的 Java 程序中的错误? [关闭]
【发布时间】:2014-10-07 00:58:24
【问题描述】:

这是我的代码,不知道出了什么问题!我犯了什么错误?

顺便说一句:此代码中的第一个程序旨在计算小于 1,000 的素数的数量,以避免任何混淆。

public class AnalyzingPrime
{

    public static boolean isPrime (int n)
    {
        if (n==1)
        return false;
        for (int i = 2; i < n; i++)
        {
            if (n%i==0)
            return false;
        }
        return true;

    }

    public static void main (String [] args)
    {
        int numberofPrimes = 0;
        int counter = 0;
        int number = 10000;
        for (int i = 2; i <= number; i++)
        {
            if (isPrime(i) == true)
            {
                counter++;
            }
        }
        System.out.println("There are "+counter+" prime numbers.");
        System.out.println("And if you care, the 10,000nth prime number is "+ nthPrime(10000)+".");
    }

    public static int nthPrime (int n)
    {
        int i = 0;
        int nthcounter = 0;

        while (nthcounter <= n)
        {
            i++;
            if(isPrime(i) == true)
            {
                nthcounter++;
            }
        }

        return nthcounter;
    }
}

【问题讨论】:

  • 介意分享错误是什么?
  • 对于 10000 个素数,您认为 int 最大值就足够了吗?把它改成长!

标签: java loops primes


【解决方案1】:

我认为没有错误,是您的方法不正确。

public static int nthPrime (int n)
{
    int i = 0;
    int nthcounter = 0;

    while (nthcounter != n) //when total prime number equal to 10000, stop loop
    {

        if(isPrime(i) == true)
        {
            nthcounter++;
        }
        i++;
    }
    return i;
}

您应该返回 i 而不是 nthcounter

【讨论】:

    【解决方案2】:

    你的 while 循环检查和返回值有问题。

    public class AnalyzingPrime
    {
    
        public static boolean isPrime (int n)
        {
            if (n==1)
            return false;
            for (int i = 2; i < n; i++)
            {
                if (n%i==0)
                return false;
            }
            return true;
    
        }
    
        public static void main (String [] args)
        {
            int numberofPrimes = 0;
            int counter = 0;
            int number = 10000;
            //add the variable to control which prime number you want
            int primeCount = 4;
            for (int i = 2; i <= number; i++)
            {
                if (isPrime(i) == true)
                {
                    counter++;
                }
            }
            System.out.println("There are "+counter+" prime numbers.");
            System.out.println("And if you care, the " + primeCount + "th prime number is "+ nthPrime(primeCount)+".");
        }
    
        public static int nthPrime (int n)
        {
            //if you know 1 is not prime number then why start with 0
            int i = 1;
            int nthcounter = 0;
    
            //should not be <= but < because when equal should not go inside the while loop
            while (nthcounter < n)
            {
                i++;
                if(isPrime(i) == true)
                {
                    nthcounter++;
                }
            }
    
            //should not return the counter but the prime value you want
            return i;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 2011-09-28
      • 2010-11-01
      相关资源
      最近更新 更多