开发者

What is wrong with my implementation of this algorithm to compute the first N prime numbers?

开发者 https://www.devze.com 2023-01-27 21:29 出处:网络
I think the constructor is logically correct, I just can\'t figure out how to call it in the main ! :) Can anyone help please ? If someone would just have a quick look over my code it would be nice :)

I think the constructor is logically correct, I just can't figure out how to call it in the main ! :) Can anyone help please ? If someone would just have a quick look over my code it would be nice :) Thanks a lot !

Also, I am using arrayLists in this implementation and I have to do it th开发者_StackOverflow社区is way so I don't wish to change it, even though it is far more easily implemented 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++;
        }
    }
}


  1. Your algorithm is not correct; you will only find the primes less than N (your initial capacity), not the first N primes.
  2. If you're going to store each prime, you should store them in a class variable not a variable local to the constructor. You won't be able to access them outside the constructor if you do.
  3. You should expose the list using a getter method to provide access to them.
  4. You're not printing anything in the constructor.


i==initialCapacity is clearly wrong.


Everything important is there, its small changes. Right now you are getting primes less than N, so if you want to change it to first N primes that's going to be a real functional difference. For now lets just make N=50 so you'll get well over 10 primes.

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's 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 a side note, you could probably d oa little better with the naming (PrimeNumberss and PrimeNumbers??), but I didnt change any of that. Also, intiialCapacity does not reflect what it really means. Maybe something along the lines of 'top'.

0

精彩评论

暂无评论...
验证码 换一张
取 消