使用递归在 Java 中打印素数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24893049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing prime numbers in Java using recursion
提问by Farveaz
I wrote a similar function in C, and was able to achieve the required result unlike java. Below is the code, which checks if a number is prime recursively. Compilation says, i'm missing a return statement. The number to be checked if a prime is x. The variable i is the divisor.(ie) x/2, (x/2)-1,...0.
我用C写了一个类似的函数,和java不同,能够达到要求的结果。下面是代码,它递归地检查一个数字是否是素数。编译说,我缺少一个返回语句。如果素数是 x,则要检查的数字。变量 i 是除数。(即) x/2, (x/2)-1,...0。
public int primes(int x, int i)
{
if(i==0)
return 1;
if(x%i==0)
return 0;
else
primes(x, i-1);
}
What is the complexity of this code if I had to print the first 1000 prime numbers.
如果我必须打印前 1000 个素数,这段代码的复杂度是多少。
采纳答案by rgettman
In this case:
在这种情况下:
else
primes(x, i-1);
You aren't returning anything. However, the compiler must ensure that something is returned in all cases. Just return whatever the recursive method call returns:
你什么都不回。但是,编译器必须确保在所有情况下都返回某些内容。只需返回递归方法调用返回的任何内容:
else
return primes(x, i-1);
Also, modify the first case's condition to i == 1
so it returns 1
on primes correctly.
此外,将第一个案例的条件修改为i == 1
,使其1
正确返回素数。
回答by Christian Wilkie
At first glance, it appears that you're missing a return in your else statement:
乍一看,您似乎在 else 语句中缺少返回:
public int primes(int x, int i)
{
if(i==1)
return 1;
if(x%i==0)
return 0;
else
return primes(x, i-1);
}
edit: Also, as said in rgettman's answer, there is a logical bug in the first conditional if(i==0)
. It should be if(i==1)
. After testing the code with the above edit here is my result:
编辑:另外,正如在 rgettman 的回答中所说,第一个 conditional 中有一个逻辑错误if(i==0)
。应该是if(i==1)
。在使用上述编辑测试代码后,这是我的结果:
List of primes under 100:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
回答by krillgar
You just need a return
inside your else
.
你只需要return
在你的else
.
else
return primes(x, i-1);
The return
there will return the results of the recursive calls, and they'll work their way up the stack. Though, this might lead to StackOverflowException
s.
在return
那里将返回递归调用的结果,他们将自己的工作方式堆栈。虽然,这可能会导致StackOverflowException
s。
回答by kirti
You can apply the logic as:
您可以将逻辑应用为:
for (i = 1; i <= 100; i++) {
int counter=0;
for (num = i; num>=1; num--) {
if (i%num==0) {
counter = counter + 1;
}
}
if (counter == 2) {
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
Then display primeNumbers
.
然后显示primeNumbers
。
回答by dummy
public class PrintPrime {
public static void main(String args[]) {
for (int i = 2; i < 1000; i++) {
primes(i, Math.ceil(Math.sqrt(i)));
}
}
public static int primes(int x, double i) {
if (i == 1)
System.out.println(x);
if (x % i == 0)
return 0;
else
return primes(x, i - 1);
}
}
回答by user8492094
import java.util.Scanner;
public class Primenumm {
static int limit,flag;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter the Number :-");
Scanner s=new Scanner(System.in);
limit=s.nextInt();
int i=0,j=limit;
printNum(i,j);
}
private static int printNum(int i, int j) {
// TODO Auto-generated method stub
if(j==0){
return 1;
}
if(i%j==0){
return 0;
}
else{
return printNum(i,j-1);
}
}
回答by Tanvir Arafat
import java.util.Scanner;
public class Primenumm {
public static void main(String[] args) {
System.out.println("Enter the Number :-");
Scanner s = new Scanner(System.in);
int limit = s.nextInt();
for (int i = limit; i >= 1; i--) {
if (primes(i, i - 1) == 1) {
System.out.println(i + " is a prime no");
} else {
System.out.println(i + " is not a prime no");
}
}
}
private static int primes(int x, int i) {
if (i == 1) {
return 1;
}
try {
if (x % i == 0) {
return 0;
} else {
return primes(x, i - 1);
}
} catch (Exception e) {
return 1;
}
}
}