i have to have an array of 1000 numbers (1- 1000)and sort out the prime numbers and print just those from the array. i have to use 4 methods while doing this:
我必须有一个1000个数字(1- 1000)的数组,并整理素数并打印出数组中的数字。这样做时我必须使用4种方法:
//publicstaticvoidmain(String[] args) //calls on int[] a
// publicstaticvoidmain(String [] args)//调用int [] a
//publicstaticvoid init(int[] a)
// publicstaticvoid init(int [] a)
//publicstaticvoid findPrimes(int[] a)
// publicstaticvoid findPrimes(int [] a)
//publicstaticvoid printPrimes(int[] a)
// publicstaticvoid printPrimes(int [] a)
i have it almost done i think but i just cant quite get it to search and print the prime numbers, i keep printing the wrong values, sorry for the mess of code in advance i try to put side notes to keep track. ps. im very new and very bad at java
我觉得它几乎完成了我想但我只是不能让它搜索并打印素数,我一直打印错误的值,对不起提前代码的混乱,我试着把旁注留意。 PS。我非常新,非常糟糕的java
class findPrimes {
public static void main(String[] args) {
int[]a = new int [1001];
init(a);
findPrimes(a);
printPrimes(a);
}
public static void init(int[]a){
for(int i = 2; i < a.length; i++) {
a[i] = 1;
boolean isprime = true;
// System.out.println(i);
}
}
public static void findPrimes(int[] a){
for (int i = 2; i < a.length; i++){
}
}
for (int j = 2*i; j <= a.length; j = j+i){
int ind = j * i;
if (ind>= a.length)
a[i] = 0 ;
}
}
}
public static void printPrimes(int[] a){
for(int i = 2; i < a.length; i++){
if(a[i] == 1) {
if (i%j == 0){
prime = false;
break;
System.out.print(i + ", ");
}
}
} }
1 个解决方案
#1
1
Looks like you are trying to implement Sieve of Eratosthenes algorithm, but your code is a bit messy.
看起来你正在尝试实现Sieve of Eratosthenes算法,但你的代码有点乱。
Use this link >> please for explanations around the algorithm, it also includes implementation in Java, so solution will look like this:
使用此链接>>请关于算法的解释,它还包括Java实现,因此解决方案将如下所示:
public class FindPrimes {
public static void main(String[] args) {
runEratosthenesSieve(1000);
}
private static void runEratosthenesSieve(int upperBound) {
int upperBoundSquareRoot = (int) Math.sqrt(upperBound);
boolean[] isComposite = new boolean[upperBound + 1];
for (int m = 2; m <= upperBoundSquareRoot; m++) {
if (!isComposite[m]) {
System.out.print(m + " ");
for (int k = m * m; k <= upperBound; k += m) {
isComposite[k] = true;
}
}
}
for (int m = upperBoundSquareRoot; m <= upperBound; m++) {
if (!isComposite[m]) {
System.out.print(m + " ");
}
}
}
}
If you want explanations around the mistakes in your code, we can discuss them too.
如果您想要解释代码中的错误,我们也可以讨论它们。
#1
1
Looks like you are trying to implement Sieve of Eratosthenes algorithm, but your code is a bit messy.
看起来你正在尝试实现Sieve of Eratosthenes算法,但你的代码有点乱。
Use this link >> please for explanations around the algorithm, it also includes implementation in Java, so solution will look like this:
使用此链接>>请关于算法的解释,它还包括Java实现,因此解决方案将如下所示:
public class FindPrimes {
public static void main(String[] args) {
runEratosthenesSieve(1000);
}
private static void runEratosthenesSieve(int upperBound) {
int upperBoundSquareRoot = (int) Math.sqrt(upperBound);
boolean[] isComposite = new boolean[upperBound + 1];
for (int m = 2; m <= upperBoundSquareRoot; m++) {
if (!isComposite[m]) {
System.out.print(m + " ");
for (int k = m * m; k <= upperBound; k += m) {
isComposite[k] = true;
}
}
}
for (int m = upperBoundSquareRoot; m <= upperBound; m++) {
if (!isComposite[m]) {
System.out.print(m + " ");
}
}
}
}
If you want explanations around the mistakes in your code, we can discuss them too.
如果您想要解释代码中的错误,我们也可以讨论它们。