What is wrong with my implementation of this algorithm for calculating the first N primes?

I think the constructor is logically correct, I just can’t figure out what to name it basically! :) Can anyone help? If someone just looked quickly through my code, it would be nice :) Thanks a lot!

In addition, I use arrayLists in this implementation, and I have to do it this way, so I don't want to change it, although it is much easier to implement using only arrays.

import java.util.*;
public class PrimeNumberss { 
    public static void main(String args []){    
      PrimeNumberss PrimeNumbers = new PrimeNumberss(10);
    }

    public PrimeNumberss (int initialCapacity) {
        ArrayList<Integer> listOfPrimeNumbers = new ArrayList<Integer>(initialCapacity);
        long numberOfPrimes = 0; //Initialises variable numberOfPrimes to 0
        int start = 2;
        boolean[] isPrimeNumber = new boolean[initialCapacity + 1];

        for (int i=0;i==initialCapacity;i++) {//setting all values in array of booleans to true
        isPrimeNumber[i] = true;
        }

        while (start != initialCapacity)
        {
          if (isPrimeNumber[start])
          {
            listOfPrimeNumbers.add(start);
            //add to array list
            numberOfPrimes++;

            for (int i = start; start < initialCapacity; i+=start)
            {
              isPrimeNumber[i] = false;
            }

          }

          start++;
        }
    }
}
+3
source share
3 answers
  • Your algorithm is incorrect; you will find only primes less than N (your initial capacity), and not the first N primes.
  • , , , . , .
  • , getter, .
  • .
+3

i == initialCapacity .

+1

, . , N, , N , . N = 50, 10 .

public class PrimeNumberss { 

    private List listOfPrimeNumbers;  //add a member variable for the ArrayList

    public static void main(String args []){    
      PrimeNumberss PrimeNumbers = new PrimeNumberss(50);
      PrimeNumbers.print();  //use our new print method
    }

public PrimeNumberss (int initialCapacity) {
    listOfPrimeNumbers = new ArrayList<Integer>(initialCapacity/2);  //initialCapacity/2 is an easy (if not tight) upper bound
    long numberOfPrimes = 0; //Initialises variable numberOfPrimes to 0
    int start = 2;
    boolean[] isPrimeNumber = new boolean[initialCapacity + 1];

    for (int i=0;i==initialCapacity;i++) {//setting all values in array of booleans to true
    isPrimeNumber[i] = true;
    }

  //.... complete the constructor method as you have it.  honestly, i didnt even read it all

 public void print()  //add this printout function
 {
     int i = 1;
     it = listOfPrimeNumbers.listIterator();
     while (it.hasNext())
     {
          System.out.println("the " + i + "th prime is: " + it.next());
          i++;
     }
     //or just System.out.println(listOfPrimeNumbers);, letting ArrayList toString do the work.  i think it will be in [a,b,c,..,z] format
 }

 public List getPrimes() {return listOfPrimeNumbers;} //a simple getter isnt a bad idea either, even though we arent using it yet
}

On the other hand, you could be a little better with naming (PrimeNumberss and PrimeNumbers ??), but I haven't changed anything. Also, intiialCapacity does not reflect what it really means. Maybe something similar to the "top".

0
source

Source: https://habr.com/ru/post/1777356/


All Articles