简体   繁体   中英

how to get greatest prime number from 3 digits

I tried to get greatest prime number which is from 3 digits like from 100 to 999 but my code got me error

public static void main(String[] args) {

        int i ,counter;
        //int arr[] = null;
        ArrayList<Integer> arr = new ArrayList<Integer>();
        for(counter=101 ; counter <999;counter++){
            boolean check = true ;
            for(i=2;i<counter;i++){             
                if(counter%i == 0){
                    check = false;
                    break;
                }

            }
            if(check){

                for(int index = 0;index<=arr.size();index++){
                    arr.add(counter);
                }
        }

        }


        System.out.println(arr.get(arr.size()-1));
        }

I Put numbers in array list and try to get last number index

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Unknown Source)
    at java.util.Arrays.copyOf(Unknown Source)
    at java.util.ArrayList.grow(Unknown Source)
    at java.util.ArrayList.ensureExplicitCapacity(Unknown Source)
    at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
    at java.util.ArrayList.add(Unknown Source)
    at Prime.main(Prime.java:23)

what's the problem ? and sorry for bad english

The problem is due to the following code segment.

for(int index = 0;index<=arr.size();index++){
    arr.add(counter);
}

It will loop indefinitely and results in adding so many objects in your array and hence OutOfMemory will occur.

I think this part doesn't need the for loop, ie, change it to

arr.add(counter);

instead.

The second for loop is not necessary and keeps looping. For the highest number start at 999 and count downwards. Then the first result is the final one.

    for (int counter = 999; counter >= 100; --counter) {
        boolean check = true ;
        for (int i = 2; i < counter/2; i++) {             
            if (counter%i == 0) {
                check = false;
                break;
            }
        }
        if (check) {
            System.out.println(counter);
            break;
        }
    }

You probably don't need the last for :

for(int index = 0;index<=arr.size();index++){
  arr.add(counter);
}

Instead of that, you can add your prime number:

if(check){
  arr.add(counter);
}

and print it outside:

for(int i = 0; i<arr.size(); i++){
  System.out.println(arr.get(i);
}
public static void main(String[] args) {

        int i, counter;
        Integer greatestPrime = null;
        //int arr[] = null;
        /*ArrayList<Integer> arr = new ArrayList<Integer>();*/
        for (counter = 101; counter < 999; counter++) {
            boolean isPrime = true;
            for (i = 2; i < counter; i++) {
                if (counter % i == 0) {
                    isPrime = false;
                    break;
                }

            }
            if (isPrime) {
                /*for(int index = 0;index<=arr.size();index++){
                    arr.add(counter);
                }*/
                greatestPrime = counter;
            }

        }

        System.out.println(greatestPrime);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM