2015-02-10 50 views
0

所以我已經通過並重新編寫我的代碼。我以前嘗試過的蠻力方法,但它花了大約20分鐘才能得到正確的答案。我現在的代碼通過找到每個數字1-20的素數分解,並在所有分解中取最大數量的素數,對於每個低於20的素數,幾乎可以立即做到這一點。它做的很好,但我如果我想找到不同範圍的數字之間的最小倍數,比如說1-30,或者更熱情的10-30,那麼我感興趣的是什麼?有了我現在的代碼,如果不改變我的許多代碼,這是不可能的。項目歐拉#5 Java -suggestions使我的代碼更乾淨一點

package number_5; 

public class Number5 { 

    public static void main(String[] args) { 
     long smallestnumber = 0; 
     boolean exitloop = false; 

     int finalTwo = 0; 
     int finalThree = 0; 
     int finalFive = 0; 
     int finalSeven = 0; 
     int finalEleven = 0; 
     int finalThirteen = 0; 
     int finalSeventeen = 0; 
     int finalNineteen = 0; 

     int two = 0; 
     int three = 0; 
     int five = 0; 
     int seven = 0; 
     int eleven = 0; 
     int thirteen = 0; 
     int seventeen = 0; 
     int nineteen = 0; 



     int temp = 0; 


     for(int num = 1; num<=20; num++) 
     { 
      temp = num; 
      for(int i=2; i<=temp; i++) 
      { 
       if(temp%i == 0) 
       { 
        temp = temp/i; 

        switch (i){ 
         case 2: 
          two++; 
          break; 
         case 3: 
          three++; 
          break; 
         case 5: 
          five++; 
          break; 
         case 7: 
          seven++; 
          break; 
         case 11: 
          eleven++; 
          break; 
         case 13: 
          thirteen++; 
          break; 
         case 17: 
          seventeen++; 
          break; 
         case 19: 
          nineteen++; 
          break; 
        } 

        i--; 
       } 


      } 
      if(two>finalTwo) 
       finalTwo = two; 
      if(three>finalThree) 
       finalThree = three; 
      if(five>finalFive) 
       finalFive = five; 
      if(seven>finalSeven) 
       finalSeven = seven; 
      if(eleven>finalEleven) 
       finalEleven = eleven; 
      if(thirteen>finalThirteen) 
       finalThirteen = thirteen; 
      if(seventeen>finalSeventeen) 
       finalSeventeen = seventeen; 
      if(nineteen>finalNineteen) 
       finalNineteen = nineteen; 

      two = 0; 
      three = 0; 
      five = 0; 
      seven = 0; 
      eleven = 0; 
      thirteen = 0; 
      seventeen = 0; 
      nineteen = 0; 

     } 


     int result = 1; 

     if(finalTwo>0) 
      result = (int) ((Math.pow(2, finalTwo)) * result); 
     if(finalThree>0) 
      result = (int) ((Math.pow(3, finalThree)) * result); 
     if(finalFive>0) 
      result = (int) ((Math.pow(5, finalFive)) * result); 
     if(finalSeven>0) 
      result = (int) ((Math.pow(7, finalSeven)) * result); 
     if(finalEleven>0) 
      result = (int) ((Math.pow(11, finalEleven)) * result); 
     if(finalThirteen>0) 
      result = (int) ((Math.pow(13, finalThirteen)) * result); 
     if(finalSeventeen>0) 
      result = (int) ((Math.pow(17, finalSeventeen)) * result); 
     if(finalNineteen>0) 
      result = (int) ((Math.pow(19, finalNineteen)) * result); 


     System.out.print(result); 

    } 

} 

回答

0

嘗試http://www.mathblog.dk/project-euler-problem-5/,以縮短你的代碼,使之更具可擴展性的參考。

下面是從頁的摘錄:

private int[] generatePrimes(int upperLimit) 
{ 
    List<Integer> primes = new ArrayList<Integer>(); 
    boolean isPrime; 
    int j; 

    primes.add(2); 

    for (int i = 3; i <= upperLimit; i += 2) 
    { 
     j = 0; 
     isPrime = true; 
     while (primes.get(j) * primes.get(j) <= i) 
     { 
      if (i % primes.get(j) == 0) 
      { 
       isPrime = false; 
       break; 
      } 
      j++; 
     } 
     if (isPrime) 
     { 
      primes.add(i); 
     } 
    } 

    return primes; 
} 

而且主要會是這個樣子:

int divisorMax = 20; 
List<Integer> p = generatePrimes(divisorMax); 
int result = 1; 

for (int i = 0; i < p.size(); i++) 
{ 
    int a = (int) Math.floor(Math.log(divisorMax)/Math.log(p.get(i))); 
    result = result * ((int)Math.pow(p.get(i),a)); 
} 

System.out.print(result); 

這應該允許您按比例1 - 通過改變divisorMax 30到30.我」米不確定約10 - 30.