Java 打印前 N 个素数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33725505/
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 the first N prime numbers
提问by Kj45
The statement is: Write a program that reads an integer N and prints the first N prime numbers.
语句是:编写一个程序,读取一个整数 N 并打印前 N 个素数。
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int x = 2;
for(int i = 0; i <= N; i++)
{
int count = 0;
for(int j = 1; j <= x; j++)
if(x%j == 0)
count++;
if(count == 2)
System.out.print(x + " ");
x++;
}
}
When I run this code it's not giving me the exact N numbers. For example for N=1 & 2 it's printing the first 2 prime numbers, for N = 3 & 4, it's printing the first 3 prime numbers, for N = 5 & 6, it's printing the first 4 prime numbers, and so on. What is the problem with this code?
当我运行这段代码时,它没有给我确切的 N 个数字。例如,对于 N=1 & 2,它打印前 2 个素数,对于 N = 3 & 4,它打印前 3 个素数,对于 N = 5 & 6,它打印前 4 个素数,依此类推。这段代码有什么问题?
回答by QuakeCore
I think that there are many flaws in your program to fix, so I decided to write a more simple, elegant program.
我认为你的程序有很多缺陷需要修复,所以我决定写一个更简单、更优雅的程序。
Scanner scan = new Scanner(System.in);
int N = Integer.parseInt( scan.nextLine());
int count = 0;
int num = 2;
while(count != N) { // while count!= number of prime numbers entered keep searching..
boolean prime = true;// to determine whether the number is prime or not
for (int i = 2; i <= Math.sqrt(num); i++) { //efficiency matters
if (num % i == 0) {
prime = false; // if number divides any other number its not a prime so set prime to false and break the loop.
break;
}
}
if (prime) {
count++;
System.out.println(num);
}
num++; see if next number is prime or not.
}
回答by Pratik Patil
// Java Program to generate first 'N' Prime Numbers
public class prime
{
public static void main(String[]args)
{
int count = 0, max_count = 100, i;
System.out.println("First "+max_count+" Prime Numbers:");
for(int num=1; count<max_count; num++)
{
for(i=2; num%i != 0; i++);
if(i == num)
{
System.out.print(" "+num);
count++;
}
}
}
}
回答by Asoub
Others have writen better code than yours, I'll explain why your code doesn't work.
其他人编写的代码比您的好,我将解释为什么您的代码不起作用。
You're not searching the first N prime number, you're searching the prime number inferieur to N+2. In your code, x
starts at 2, and goes up to N+2.
您不是在搜索前 N 个质数,而是在搜索 N+2 的质数 inferieur。在您的代码中,x
从 2 开始,一直到 N+2。
If you want to find the N first prime numbers, you must loop while N is not equals 0, and decrease N everytime you found a prime number.
如果你想找到前N个素数,你必须在N不等于0时循环,每次找到一个素数就减少N。
(wait this question is from 2015 ... damn)
(等等这个问题是从 2015 年开始的……该死)
回答by Tim
Using dynamic programming:
使用动态规划:
Any number that is not prime is always divisible by at least one prime that comes before it in the number series. If any number doesn't divide itself with all the prime numbers before it then that number is also prime
任何不是素数的数总是可以被数列中至少一个位于它前面的素数整除。如果任何数不能与它之前的所有素数整除,则该数也是素数
void printPrimeNumbers(int n) {
ArrayList<Integer> primeNumbers = new ArrayList<>();
primeNumbers.add(2);
for (int i = 3; i < n; i+=2) { // skip over even numbers since they are not prime
boolean isPrime = true;
for (Integer prime : primeNumbers) { // check current prime numbers to see if it evenly divides into number
if (i % prime == 0) { // when number is not prime
isPrime = false;
break; // optimization: stop checking when number is already not prime
}
}
if (isPrime) {
primeNumbers.add(i);
}
}
System.out.println(primeNumbers);
}
回答by Sachin Shah
public static void main(String... commandline_args)
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the limit of prime nos");
int limit=Integer.parseInt(br.readLine());
int checkLimit=0;
int check=0;
outer: for(int i=2;;i++){
check=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
check++;
}
}
if(check==2)
{
checkLimit++;
System.out.println(i);
if(checkLimit==limit) break outer;
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}