找出素数的四种方法:
复杂度 | 主要区别 |
---|---|
1. 时间复杂度:O(n2) | divisor <= Math.sqrt(number) |
2. 时间复杂度:O(n√n) | divisor <= squareRoot,其中squareRoot2 |
3. 时间复杂度:O(n√n / logn) | for (int k=0; k |
4. 时间复杂度:O(n√n / logn) | for (int i=k; i<= n/k; i++){ //执行了 n/k-(k-1) primes[k * i] = false; |
public class PremeNumber {
public static void main(String[] args) {
final int NUMBER_OF_PRIMES = 50 ;
final int NUMBER_OF_PRIMES_PRE_LINE = 10;
int count = 0;
int number = 2;
System.out.println("The first 50 prime numbers are : ");
while (count < NUMBER_OF_PRIMES){
boolean isPrime = true;
for (int divisor = 2; divisor <= Math.sqrt(number); divisor++){
if (number % divisor == 0){
isPrime = false;
break;
}
}
if (isPrime){
count++;
if (count % NUMBER_OF_PRIMES_PRE_LINE == 0){
System.out.println(number);
}
else System.out.print(number+" ");
}
number++;
}
}
}
1. 主要区别:
int squareRoot = 0;
if (squareRoot * squareRoot < number) squareRoot++;
for (int divisor = 2; divisor <= squareRoot; divisor++){
2.完整代码:
public class PrimeNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Find all prime numbers <= n, enter n: ");
int n = input.nextInt();
final int NUMBERS_PRE_LINE = 10;
int count = 0;
int number = 2;
System.out.println("The prime numbers are: ");
int squareRoot = 0;
while (number <= n){
boolean isPrime = true;
//时间复杂度√n。
// for (int divisor = 2; divisor <= (int) (Math.sqrt(number)); divisor++){
//时间复杂度n√n。
if (squareRoot * squareRoot < number) squareRoot++;
for (int divisor = 2; divisor <= squareRoot; divisor++){
if (number % divisor == 0){
isPrime = false;
break;
}
}
if (isPrime){
count++;
if (count % NUMBERS_PRE_LINE == 0)
System.out.printf("%7d\n",number);
else System.out.printf("%7d",number);
}
number++;
}
System.out.println("\n"+ count+" prime less than or equal to "+ n);
}
}
1. 主要区别:
//A list to hold prime numbers
java.util.ArrayList<Integer> list = new java.util.ArrayList<Integer>();
//循环体内
for (int k=0; k<list.size() && list.get(k) <= squareRoot ;k++){...}
2. 完整代码:
public class EfficientPrimeNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Find all prime numbers <= n, enter n: ");
int n = input.nextInt();
//A list to hold prime numbers
java.util.ArrayList<Integer> list = new java.util.ArrayList<Integer>();
final int NUMBERS_PRE_LINE = 10;
int count = 0;
int number = 2;
int squareRoot = 0;
System.out.println("The prime numbers are: ");
while (number <= n){
boolean isPrime = true;
// for (int divisor = 2; divisor <= (int) (Math.sqrt(number)); divisor++){
if (squareRoot * squareRoot < number) squareRoot++;
for (int k=0; k<list.size() && list.get(k) <= squareRoot ;k++){
if (number%list.get(k) == 0){
isPrime = false;
break;
}
}
if (isPrime){
count++;
list.add(number);
if (count % NUMBERS_PRE_LINE == 0)
System.out.printf("%7d\n",number);
else System.out.printf("%7d",number);
}
number++;
}
System.out.println("\n"+ count+" prime less than or equal to "+ n);
}
}
1.主要区别:
//Initial primes[i] to true
for (int i=0; i<primes.length; i++){
primes[i] = true;
}
for (int k=2; k<= n/k; k++){
if (primes[k]){
for (int i=k; i<= n/k; i++){ //执行了 n/k-(k-1)次
primes[k * i] = false;
}
}
}
2.完整代码:
public class SieveOfEratosthenes {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Find all prime numbers <= n, enter n: ");
int n = input.nextInt();
boolean[] primes = new boolean[n+1];
//Initial primes[i] to true
for (int i=0; i<primes.length; i++){
primes[i] = true;
}
for (int k=2; k<= n/k; k++){
if (primes[k]){
for (int i=k; i<= n/k; i++){ //执行了 n/k-(k-1)次
primes[k * i] = false;
}
}
}
int count = 0;
for (int i=2;i<primes.length;i++){
if (primes[i]){
count++;
if (count % 10 == 0)
System.out.printf("%7d\n",i);
else System.out.printf("%7d",i);
}
}
System.out.println("\n"+ count+" prime less than or equal to "+ n);
}
}