使我的素数/完美/复合数字检查器更有效/更清洁

时间:2017-10-13 06:33:28

标签: java primes perfect-numbers

此程序要求用户输入大于1的最小数字,并且最大数字大于min。然后它按数字打印出可被整除的数字,如果它的素数或复合数,以及它是否是这种格式的完美数字:

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.

最后它会显示素数和完美数字。该程序有效,但我想知道是否有任何方法来清理代码/使其更有效(或者如果有任何我做错了)

代码:

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);


}

}

1 个答案:

答案 0 :(得分:0)

我有一个让我的python完美数字代码尽可能高效的爱好所以我知道一些额外的效率,但不幸的是我不编码Java所以这些只是一些通用的效率改进。

首先,正如评论中暗示的那样,你只需要检查数字的平方根来获得所有除数,但你要做的就是将数字/你的除数加到你的除数列表中得到每个除数

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.

另一个好的效率改进是所有完美的数字将以6或28结束,因此您可以快速检查。

我知道的最后一次效率提升是对主要计算位的一个重大改变。

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.