2017-10-13 54 views
0

此程序詢問用戶的最小數大於1,最大數大於最小數。然後,它通過數什麼的整除打印出號,如果它的主要或複合,如果它在這個格式的完美一些:使我的主要/完美/複合數字檢查器更高效更清潔

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


} 

}

+4

如果你的代碼作品,然後我建議張貼在[代碼評論](http://codereview.stackexchange.com/) –

+0

好吧,會感謝! – rmcknst2

+0

既然是作業,我不想破壞學習的機會,但是你認爲你可以減少for循環的迭代次數嗎? 具體而言,對於(i = 2; i <= count; i ++)。你真的需要從2重複計數嗎?如果count爲144,那麼我的值幾乎總是不能成爲144的約數。如果我是133,爲什麼需要迭代? – JustinDanielson

回答

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.