为什么我的算法没有给出预期的输出?
Why is my algorithm not giving the expected output?
我正在尝试创建 Sieve of Eratosthenes 算法的 Java 实现。
我有以下代码,它运行了,但输出不正确。
import java.util.ArrayList;
public class sieveOfEratosthenes {
private static final ArrayList<Integer> test = new ArrayList<>();
public static void main (String [] args) {
java.util.Scanner tempInput = new java.util.Scanner(System.in);
System.out.println("What number would you like the prime numbers to be generated to?");
int maxPrime = tempInput.nextInt();
for(int i = 2; i <= maxPrime; i++) {
test.add(i);
}
getPrimeList(maxPrime);
}
private static void getPrimeList(int maxNumber) {
int sqrtOfNum = (int) Math.sqrt(maxNumber);
int temp = 0, i = 0;
int currentPrime = test.get(i);
boolean completed = false;
i++;
//do {
while((completed == false) && (i < test.size())) {
if(i >= test.size()) {
completed = true;
} else if((temp <= sqrtOfNum) ) {
removeMultiples(currentPrime);
}
i++;
if (i < test.size()) {
currentPrime = test.get(i);
}
}
//}while(completed == false && (i < test.size()));
System.out.println("Prime numbers upto: " + maxNumber + ": " + test);
}
private static void removeMultiples(int primeToTest) {
ArrayList<Integer> temp = new ArrayList<>();
for (Integer toTest : test) {
if (!(((toTest) % primeToTest) == 0)) {
temp.add(toTest);
}
}
test.clear();
test.addAll(temp);
}
}
程序给出的输出示例如下:
What number would you like the prime numbers to be generated to?
10
Prime numbers upto: 10: [3, 5, 9]
显然上面例子的输出应该是:
Prime numbers upto: 10: [2, 3, 5, 7]
您将 test
初始化为 [2,3,4,5...]
,将 currentPrime
设置为 2 (test[0]
),删除它的倍数(删除 2)。我相信当 i
变为 2 并且 test[2]
= 7 时会发生类似的事情。
这不会发生在 3 和 5 上,因为您正在使用 i
来推进 test
,但也从 test
中删除项目,以便值 i
引用更改(因为该位置的值已更改)。所以在第一次通过 while
循环结束时,i
已经提前到 2 而没有消除 3 或 5 的倍数(你会看到如果你使用更大的 maxNumber
).
Eratosthenes 筛法算法说,当您考虑素数时 currentPrime
您必须将除自身以外的所有倍数标记为非素数。在您的 removeMultiples
函数中,您还删除了 currentPrime
。
你在 getPrimeList
中迭代的方式对我来说也有点奇怪。我认为您可以摆脱 completed
变量和一些 i >= test.size()
测试。
尝试类似的东西:
import java.util.ArrayList;
public class sieveOfEratosthenes {
private static final ArrayList<Integer> test = new ArrayList<>();
public static void main (String [] args) {
java.util.Scanner tempInput = new java.util.Scanner(System.in);
System.out.println("What number would you like the prime numbers to be generated to?");
int maxPrime = tempInput.nextInt();
for(int i = 2; i <= maxPrime; i++) {
test.add(i);
}
getPrimeList(maxPrime);
}
private static void getPrimeList(int maxNumber) {
int sqrtOfNum = (int) Math.sqrt(maxNumber);
int temp = 0, i = 0, current_prime = 0;
//do {
while(current_prime <= sqrtOfNum && i < test.size()) {
current_prime = test.get(i);
removeMultiples(current_prime);
i++;
}
//}while(completed == false && (i < test.size()));
System.out.println("Prime numbers upto: " + maxNumber + ": " + test);
}
private static void removeMultiples(int primeToTest) {
ArrayList<Integer> temp = new ArrayList<>();
tmp.add(primeToTest);
for (Integer toTest : test) {
if (toTest%primeToTest != 0) {
temp.add(toTest);
}
}
test.clear();
test.addAll(temp);
}
}
我正在尝试创建 Sieve of Eratosthenes 算法的 Java 实现。
我有以下代码,它运行了,但输出不正确。
import java.util.ArrayList;
public class sieveOfEratosthenes {
private static final ArrayList<Integer> test = new ArrayList<>();
public static void main (String [] args) {
java.util.Scanner tempInput = new java.util.Scanner(System.in);
System.out.println("What number would you like the prime numbers to be generated to?");
int maxPrime = tempInput.nextInt();
for(int i = 2; i <= maxPrime; i++) {
test.add(i);
}
getPrimeList(maxPrime);
}
private static void getPrimeList(int maxNumber) {
int sqrtOfNum = (int) Math.sqrt(maxNumber);
int temp = 0, i = 0;
int currentPrime = test.get(i);
boolean completed = false;
i++;
//do {
while((completed == false) && (i < test.size())) {
if(i >= test.size()) {
completed = true;
} else if((temp <= sqrtOfNum) ) {
removeMultiples(currentPrime);
}
i++;
if (i < test.size()) {
currentPrime = test.get(i);
}
}
//}while(completed == false && (i < test.size()));
System.out.println("Prime numbers upto: " + maxNumber + ": " + test);
}
private static void removeMultiples(int primeToTest) {
ArrayList<Integer> temp = new ArrayList<>();
for (Integer toTest : test) {
if (!(((toTest) % primeToTest) == 0)) {
temp.add(toTest);
}
}
test.clear();
test.addAll(temp);
}
}
程序给出的输出示例如下:
What number would you like the prime numbers to be generated to?
10
Prime numbers upto: 10: [3, 5, 9]
显然上面例子的输出应该是:
Prime numbers upto: 10: [2, 3, 5, 7]
您将 test
初始化为 [2,3,4,5...]
,将 currentPrime
设置为 2 (test[0]
),删除它的倍数(删除 2)。我相信当 i
变为 2 并且 test[2]
= 7 时会发生类似的事情。
这不会发生在 3 和 5 上,因为您正在使用 i
来推进 test
,但也从 test
中删除项目,以便值 i
引用更改(因为该位置的值已更改)。所以在第一次通过 while
循环结束时,i
已经提前到 2 而没有消除 3 或 5 的倍数(你会看到如果你使用更大的 maxNumber
).
Eratosthenes 筛法算法说,当您考虑素数时 currentPrime
您必须将除自身以外的所有倍数标记为非素数。在您的 removeMultiples
函数中,您还删除了 currentPrime
。
你在 getPrimeList
中迭代的方式对我来说也有点奇怪。我认为您可以摆脱 completed
变量和一些 i >= test.size()
测试。
尝试类似的东西:
import java.util.ArrayList;
public class sieveOfEratosthenes {
private static final ArrayList<Integer> test = new ArrayList<>();
public static void main (String [] args) {
java.util.Scanner tempInput = new java.util.Scanner(System.in);
System.out.println("What number would you like the prime numbers to be generated to?");
int maxPrime = tempInput.nextInt();
for(int i = 2; i <= maxPrime; i++) {
test.add(i);
}
getPrimeList(maxPrime);
}
private static void getPrimeList(int maxNumber) {
int sqrtOfNum = (int) Math.sqrt(maxNumber);
int temp = 0, i = 0, current_prime = 0;
//do {
while(current_prime <= sqrtOfNum && i < test.size()) {
current_prime = test.get(i);
removeMultiples(current_prime);
i++;
}
//}while(completed == false && (i < test.size()));
System.out.println("Prime numbers upto: " + maxNumber + ": " + test);
}
private static void removeMultiples(int primeToTest) {
ArrayList<Integer> temp = new ArrayList<>();
tmp.add(primeToTest);
for (Integer toTest : test) {
if (toTest%primeToTest != 0) {
temp.add(toTest);
}
}
test.clear();
test.addAll(temp);
}
}