【问题标题】:Finding a prime number using recursion in Java在Java中使用递归查找素数
【发布时间】:2017-01-19 10:56:06
【问题描述】:

我正在为学校写这个问题,但我遇到了一些问题。我无法让它实际计算素数。显然,当我从 main 用数字 4 运行测试时,它说 4 是质数,而我们都知道它不是。我需要如何写出方程式?

说明如下。

使用 RECURSION 编写此函数(或使此函数成为另一个递归函数的包装器)

 * a number is prime if it is divisible only by itself and 1 
 * (that is, if it is not divisible by any number between * itself and 1; 
 * we can optimize and just check between 2 and the square root of the number).
 * by convention, 1 is NOT prime
 * this function returns true if its parameter is prime, false otherwise.
 * One way to do this is to test all numbers between 2 and n; if any of them     
 * divides it, then it is not prime. If you reach the end, then it is.
 * Examples:
 * isPrime(1) => false
 * isPrime(2) => true
 * isPrime(3) => true
 * isPrime(4) => false
 */



public static boolean isPrime(int n)
{
    if (n == 0 || n == 1) { 
        return false; 
    } if (n == 2 || n == 3) { 
        return true; 
    } if (Math.sqrt(n) % 2 == 0) { 
        return true;
    }else
        return isPrime(n);
    }

下面的代码来自我的教授用来给程序评分的grader.java。 isprime 方法有几个调用。它似乎总是挂在 4 上(我明白为什么...... 4 squared % 2 == 0)并且 4 不是素数 #。

         public void testIsPrime()
{
    Assert.assertEquals("1 is not prime", false,Assignment4.isPrime(1));
    Assert.assertEquals("2 is prime", true,Assignment4.isPrime(3));
    Assert.assertEquals("4 is not prime", false,Assignment4.isPrime(4));
    Assert.assertEquals("7 is prime", true,Assignment4.isPrime(7));
    Assert.assertEquals("9 is not prime", false,Assignment4.isPrime(9));
    Assert.assertEquals("35 is not prime", false,Assignment4.isPrime(35));
    Assert.assertEquals("37 is prime", true,Assignment4.isPrime(37));        
}

【问题讨论】:

  • 我的错。已编辑。
  • 你写过任何有效的递归代码吗?
  • 我上周刚开始。我还在学习,经常犯错误。只是想获得一些关于我做对/错的反馈。谢谢。
  • 在这种情况下不需要使用递归,因为它只会执行一次指令块 isPrime 方法作为最后一个数字作为参数值永远不会改变总是得到相同的结果。
  • 我明白你在说什么。也许循环会更好。我只是想按照说明进行操作。 (见编辑过的问题)

标签: java recursion


【解决方案1】:

在素数中,2 不能是素数,因为您可以将其除以 2,但它不应该能够除以 2。因此,如果 number%2 为 0 或数字为 2,则您需要返回 false。

当一个数字不是 2 或不能除以 0 时,您可以在函数内使用 for 循环检查其他数字。

看看下面的代码,它可以帮助你理解发生了什么:

public boolean isPrime (int number){
    if ((number%2)==0 && number != 2) {
        return false;
    }
    else {
        for (int i =3; i*i<number; i++ )
        {
            if (number%i ==0)
                return false;
        }
        return true;
    }

【讨论】:

    【解决方案2】:

    作业给了你一个重要的提示:

    或者使这个函数成为另一个递归函数的包装器

    public static boolean isPrime_helper(int number, int divisor)
    {
        /* write code to return true if divisor is > square root of number */
        /* which can also be expressed divisor squared is > number */
    
        /* write code here to test if divisor divides number without remainder */
        /* and return false if it does.  Otherwise: */
    
        return isPrime_helper(number, divisor + 2);
    }
    
    public static boolean isPrime(int number)
    {
        /* deal with the special cases 2, < 2, and even numbers here */
        /* Otherwise: */
    
        return isPrime_helper(number, 3);
    }
    

    【讨论】:

      【解决方案3】:

      cdlane 的回答有基本的更正。我只是想确保你知道你的尝试在哪里不起作用。你有两个致命的问题:

      1. 您的递归没有简化。如果您没有达到基本情况 (0-3),您会以 相同的 数字重复,从而使您陷入无限循环。
      2. 您的 sqrt 子句既不必要又错误。如果一个数的 sqrt 是偶数,它不能是素数。这应该是停止递归的测试吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多