need help doing Eratosthenes siev

kzjlres

Neues Mitglied
my friend(who is better than me in codding) and I are working on a university homework, but before I get to the point I want to mention that I'm no expert and a total noob in comparison to you guys. (2-weeks coding journey).
he had an operation and can't help me so I'm by my own that's why I'm writing here to get some help. okay, that being said lets hit to the point. the exercise is to make an Eratosthenes-sieve without any multiplication, division nor for loops. Now the problem is that I've written before the professor tell us not to use those things and now I don't know what to do. changing the for loops to while is no big deal but I have no clue how to get rid of the last multiplication at the end of the code.
so I wrote the code and sent it to my fellow he did some changes that I didn't well understand //I will comment on the changes that he did and I would be really thankful if someone did explain some point for me.
1)so first we need to get rid of the for loops and replace it with while which is no big deal and then get rid of the multiplication at the end of the code which is my main problem.
2)help to understand some points.
note I already used some internet references for help because I'm a starter. it's not a crime hihihi.
so here is the code:


Java:
import java.util.Scanner;
public class primesieb {
public static void main(String[] args) {
    //so we declare values and used the scanner to make an input.
    int input = 0;
    Scanner in = new Scanner(System.in);

    System.out.println("give an int bigger that 1:  ");
    input = in.nextInt();

    while (input <= 1)
    {
     //in case the number is smaller than 1.
            System.out.println("the Int must be bigger than 1!.");
            System.out.println("write an Int bigger than 1: ");
            input = in.nextInt();
    }
    in.close();
    //now first i wrote it without "+1" but my friend changed it to +1
       boolean[] x = new boolean[input + 1];
    //here I just wanted to add this to set that the position 0 in the string is not a prime number but after thinking I don't see its necessary

           x[0]=false;

    //he did write this commented for loop to assume that all of them are false but for me, it doesn't make sense, pls explain if it's necessary.
    //for (int i = 2; i < x.length; i++) {
      //  x[i] = false;}

    for (int i = 2; i < x.length; i++) {

        while (x[i]) 
        {
            i++;
        }
     // here how can we get rid of the multiplication.
    //the next for loop and if my friend did and a better explain would be nice helping me to represent my code better.
    for (int j = i; j*i < input; j++) 
        {
            x[i * j] = true;
        }
        if (! x[i] && i < input) 
        {
            System.out.println("the number" + i + "is prime. ");
        }
    }
}
 
Zuletzt bearbeitet von einem Moderator:

kzjlres

Neues Mitglied
I tried now to use the (int)Math.sqrt(i) and I got an Error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11 at primesiebselber.main(primesiebselber.java:31)
I have no idea what is this. and i did try this for (int j = i, z= (int)Math.pow (i,j); z < eingabe; j++) { x[z] = true;
but the code is showing nothing after my input!!!
 

mihe7

Top Contributor
Please post your code using code-tags: [code=Java]your code[/code] Otherwise important parts of your code will get lost, e. g. [i] will be interpreted as italic.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
F Need Help mit String :/ Java Basics - Anfänger-Themen 8
M ArrayQueue need HELP! Java Basics - Anfänger-Themen 5
N need help in panel Java Basics - Anfänger-Themen 8
G Help me in the Java Program Java Basics - Anfänger-Themen 2
L Erste Schritte Help with websocket protocol implementation Java Basics - Anfänger-Themen 5
Z User/passwort eingabe...Help! Java Basics - Anfänger-Themen 4
1 Mein erstes Programm, HELP!! Java Basics - Anfänger-Themen 2
E string.match[regex] - Help ! Java Basics - Anfänger-Themen 2
A Frame schließen!? Help! Java Basics - Anfänger-Themen 5
Screen Ein logischer Fehler im Code, Help pls Java Basics - Anfänger-Themen 6
S Help Java Basics - Anfänger-Themen 5
Developer_X !!!Help with Applets!!! PLEASE Java Basics - Anfänger-Themen 14
D ZweiWoerter.java HELP ! Java Basics - Anfänger-Themen 8
I Selection-Sort // Array *help* Java Basics - Anfänger-Themen 2
M Java Schleife Help plz! Java Basics - Anfänger-Themen 12
M Help! ActionEvent bei Button Klick Java Basics - Anfänger-Themen 9
R FileInputStream.read() != FileReader.read(). Pls Help Java Basics - Anfänger-Themen 5
D Wert muss sich pro sek ändern aber wie? HELP! Java Basics - Anfänger-Themen 9
T Help! Kann Bilder nach Packen in ein jar-File nimmer finden! Java Basics - Anfänger-Themen 17
N Finde Fehler nicht! Help plz! Java Basics - Anfänger-Themen 3
S call by reference vs. call by value - pls help Java Basics - Anfänger-Themen 7
O Java - "Learning by doing" - Übungsaufgaben gesucht. Java Basics - Anfänger-Themen 5

Ähnliche Java Themen

Neue Themen


Oben