简体   繁体   中英

Making my prime/perfect/composite number checker more efficient/cleaner

This program asks the user for a minimum number greater than 1 and a maximum number grater than the min. It then prints out number by number what its divisible by, if its prime or composite, and if its a perfect number in this format:

2 is divisible by 1
2 is prime.
2 is not perfect

3 is divisible by 1
3 is prime.
3 is not perfect

4 is divisible by 1 2 
4 is composite.
4 is not perfect.

5 is divisible by 1
5 is prime.
5 is not perfect

6 is divisible by 1 2 3 
6 is composite.
6 is perfect.

At the end it displays the number of prime and perfect numbers. The program works but I am wondering if there are any ways to clean up the code/make it more efficient(or if there is anything im doing wrong)

Code:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);

    int min;
    int max;

    //declaring min and max values

    System.out.println("Enter minimum value to check (an integer greater than 1:)");
    min=input.nextInt();

    while(!(min>1)) {
        System.out.println("The entry is valid. Please be sure to enter an integer greater than 1");
        System.out.println();
        System.out.println("Enter minimum value to check (an integer greater than 1:)");
        min=input.nextInt();
    }

    System.out.println("Enter maximum value to check (an integer greater than your min value:)");
    max=input.nextInt();

    while(!(max>min)) {
        System.out.println("The entry is valid. Please be sure to enter an integer greater than the min value");
        System.out.println();
        System.out.println("Enter maximum value to check (an integer greater than min:)");
        max=input.nextInt();
    }

    //declaring count and tracking variables

    int count;
    int numPrime=0;
    int numPerfect=0;
    int temp=1;
    String result=" ";
    boolean isPrime=true;
    boolean isPerfect=false;
    int i;
    //main loop

    for(count=min;count<=max;count++) {

        for(i=2;i<=count;i++) {
            if(count%i==0&&i!=count) {
                isPrime=false;
                result=result+i+" ";
                temp+=i;
            }
            else
                isPrime=true;
        }
        //Perfect counter
        if(temp==count) {
            isPerfect=true;
            numPerfect=numPerfect+1;
        }
        else
            isPerfect=false;
        //Composite print
        if(!(result.equals(" "))) {
            System.out.println(count+" is divisible by 1"+result);
            System.out.println(count+" is composite.");
            if(isPerfect==true)
                System.out.println(count+" is perfect.");
            else
                System.out.println(count+ " is not perfect.");
            System.out.println();
        }
        //Prime print
        else {
            numPrime=numPrime+1;
            System.out.println(count+" is divisible by 1");
            System.out.println(count+" is prime.");
            System.out.println(count+" is not perfect");
            System.out.println();
        }           
        //reset values
        result=" ";
        temp=1;
    }
    System.out.println("Primes found: "+numPrime);
    System.out.println("Perfect numbers found: "+numPerfect);


}

}

I have a hobby of making my python perfect number code as efficient as possible so I know a few extra efficiency things but unfortunately I don't code Java so these will just be some generic efficiency improvements.

Firstly, as has been hinted in the comments you only have to check to the square root of the number to get all the divisors but what you do have to do is then add the number / your divisors to your list of divisors to get every divisor

e.g. Find the divisors of 20
The square root of 20 is 4.47 so we choose 4.
20 mod 1 == 0 so we add 1 and 20/1 a.k.a. 20 to our list of divisors
20 mod 2 == 0 so we add 2 and 20/2 a.k.a 10 to our list of divisors
20 mod 3 == 2 so we ignore this one.
20 mod 4 == 0 so we add 4 and 20/4 a.k.a. 5 to our list of divisors
Therefore the divisors are 1, 2, 4, 5, 10 and 20.

Another good efficiency improvement is that all perfect numbers will end in 6 or 28 so you can quickly check that.

The last efficiency improvement I know is a big change to the prime calculation bit.

If a number is smaller than or equal to 1 it is not prime.
Else if a number is smaller than or equal to 3 it is prime.
Else if a number mod 2 or a number mod 3 == 0 it is not prime.
Set i to 5
While i squared is smaller than or equal to a number:
    If a number mod i or a number mod (i + 2) == 0:
        It is not prime
    6 is added to i
If nothing has said otherwise then the number is prime.

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