java 如何打印素数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15081844/
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
How to print prime numbers?
提问by user2098510
I am a beginner to java.And i am trying to print 2,3,5,7,11,13,17,19
我是 java 的初学者。我正在尝试打印 2,3,5,7,11,13,17,19
This is my thought process.the above numbers i want to print are prime numbers which means they can only be divided by themselves or the value 1.So i will need to have a condition which is if(i%i==0 || %1==0){
这是我的思考过程。我要打印的上述数字是质数,这意味着它们只能被自己或值 1 整除。所以我需要有一个条件 if(i%i==0 || %1==0){
import java.util.*;
public class PrimePrinter{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.print("Enter num> ");
int input=sc.nextInt();
for(int i=2;i<=19;i++){
if(i%i==0&&i%1==0){
System.out.print(i);
}else {
System.out.print(",");
}
}
}
}
I try to think through my codes but i wonder why it will print out 2,3,4,5...and up till 19 when i already have a condition.I will appreciate if someone will give me hints for me to work out instead of posting the solutions.
我试着仔细考虑我的代码,但我想知道为什么它会打印出 2,3,4,5 ......直到 19 当我已经有条件时。如果有人能给我提示让我锻炼,我将不胜感激而不是发布解决方案。
回答by Ian McMahon
You're only checking if they're divisible by 1 and themselves. Every number is divisible by 1 and itself. Prime numbers are ONLY divisible by 1 and themselves, so the naive way would be to test whether they're divisible by every other number between 1 and i.
您只是在检查它们是否可以被 1 和它们自己整除。每个数都可以被 1 和它本身整除。质数只能被 1 和它们自己整除,所以最简单的方法是测试它们是否可以被 1 和 i 之间的所有其他数字整除。
For a more efficient way to do it, look into the Sieve of Eratosthenes.
要获得更有效的方法,请查看埃拉托色尼筛。
回答by Ian McMahon
A number is prime when it is divided by itself and by 1. And 1 is not a prime number. In your program you are dividing the number by 1 so that's wrong
一个数在被它本身除以 1 时是素数。而 1 不是素数。在您的程序中,您将数字除以 1,所以这是错误的
import java.util.*;
public class PrimePrinter{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.print("Enter num> ");
int input=sc.nextInt();
// Instead of 19 you should use "input"
// So the program will print all the numbers which are prime till input
for(int i=2;i<=19;i++){
if(isPrime(i))
System.out.print(i+", ");
}
}
}
public static boolean isPrime(int number){
for(int i=2; i<number; i++){
if(number%i == 0){
return false;//number is divisible so its not prime
}
return true; //number is prime
}
}
回答by Stephen C
but i wonder why it will print out 2,3,4,5...and up till 19 when i already have a condition.
但我想知道为什么它会打印出 2,3,4,5 ......直到 19 时我已经有了一个条件。
Your code is testing if a number is divisible by itself or one. But everynumber (apart from zero) is divisible by itself and one. So (naturally) every number that you tried passes "the test".
您的代码正在测试一个数字是否可以被自身整除或被 1 整除。但是每个数字(除了零)都可以被它自己和 1 整除。因此(自然地)您尝试的每个数字都通过了“测试”。
The problem is that "the test" is wrong as a test for prime numbers. You actually need to find numbers that are onlydivisible by themselves and one. The simple way to do this is to test your number against every othernumber that it could be divisible by. You can restrict this to a finite set of numbers with some simple high-school math ... based on the definition of division.
问题是“测试”作为素数测试是错误的。您实际上需要找到只能被它们自己和 1 整除的数字。执行此操作的简单方法是将您的数字与可以被它整除的所有其他数字进行比较。您可以使用一些简单的高中数学将其限制为一组有限的数字……基于除法的定义。
回答by Joe2013
A number is considered as prime if it is only divisible by 1 and itself. It can be identified by checking the divisibility against 2 -> SQRT(n) (instead of n-1 , SQRT of (n) will be sufficient). There are other algorithms also available with better efficiency, but for a beginner this will be sufficient as well as efficient. The entire code snippet is provided below:
如果一个数只能被 1 和它本身整除,则该数被认为是素数。它可以通过检查对 2 -> SQRT(n) 的可分性来识别(而不是 n-1 , (n) 的 SQRT 就足够了)。还有其他算法效率更高,但对于初学者来说,这将足够有效。下面提供了整个代码片段:
public class PrimeNumberTest {
public static void main(String args[]) {
boolean isPrime = true;
int num = Integer.parseInt(args[0]);
for (int i = 2; i < Math.sqrt(num); i++) {
if(num != i && num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime)
System.out.println(num + " is prime");
else
System.out.println(num + " is not prime");
}
}