2015-08-08 194 views
0

所以我還不是很擅長(輕描淡寫)。我正在努力解決歐拉項目中的問題,並且我已經陷入了困境2.歐拉項目2

斐波那契數列中的每個新項都是通過添加前面的2項生成的。通過用1和2開始,第一10項將是:

1,2,3,5,8,13,21,34,55,89,...

通過考慮中的條款斐波納契數列的值不超過四百萬,找到偶數項的和。

這裏是我的代碼,我曾多次試圖修復: (我覺得有一些錯誤的for循環邏輯)

public class tesy { 
    public static void main(String args[]) { 
     int fib = 0; 
     int tot = 0; 
     int total = 0; 

     for (fib = 0; tot < 4000000; fib++) { 
      tot = fib + (fib + 1); 

      if (tot % 2 == 0) { 
       total = tot + total; 
      } 
     } 
     System.out.println(total); 
    } 
} 
+1

對於每個人來說,閱讀代碼非常重要的一件事就是格式化(特別是縮進)。如果您使用IDE(例如IntelliJ IDEA),則會自動爲您完成。 –

+0

您不需要爲您的年齡或您在Java中的當前知識而道歉:)。但是,你能解釋你現在的代碼有什麼問題嗎? – Tom

+1

爲什麼你不遵循斐波那契數列的定義?你正在添加'fib +(fib + 1)',它只是'2 * fib + 1',而不是記住以前的斐波那契數字(不是它在序列中的位置!)並將其添加到當前的一箇中以檢索新的斐波那契數。 –

回答

0

你的邏輯是對夫婦的方式是錯誤的,

tot = fib + (fib + 1); /** This will always be `(2*fib + 1)` and `fib` is getting 
incremented by 1 each time. You have no reference to the previous two terms of the 
sequence. **/ 

嘗試下面的邏輯來代替。

class Fibonacci 
{ 
    public static void main (String[] args) 
    { 
     int fiboFirst = 1; 
     int fiboSecond =2; 
     int fib = 0; 
     int sum = 0; 

     while(fiboSecond < 4000000) 
      { 
      // This will calculate the current term of the sequence 
      fib = fiboFirst + fiboSecond; 

      // Below two lines will update fib[i] and fib[i - 1] terms 
      // for the next loop iteration. 
      fiboFirst = fiboSecond; // fib[i] 
      fiboSecond = fib; // fib[i -1] 
      if (fib % 2 == 0) 
       { 
       sum = sum + fib; 
       } 
      } 
     System.out.println(sum+2); 
    } 
} 

說明

這裏fiboFirst相當於F [n]和fiboSecond相當於 至F [N - 1]在Fibonacci序列定義。在每次迭代中, 這兩個值應該被替換,以便在下一次迭代中使用 。這就是爲什麼我有這兩條線,

fiboFirst = fiboSecond; // fib[i] 
fiboSecond = fib; // fib[i -1] 

HERE是上面的程序

+0

這個邏輯看起來非常好,比我自己的要好得多,但其中的一部分我沒有get - 比如fiboSecond = fib; - 你能評論一下代碼嗎?爲什麼每一行都在那裏?謝謝 –

+0

啊!現在我明白了 - 它很清楚。感謝和令人敬畏的代碼btw。 –

+0

謝謝! :)我也用解釋編輯了我的答案。希望能幫助到你。如果我的答案解決了你的問題,你也可以接受... – chayasan

0

你似乎並不成爲繼實際方程用於生成斐波那契數列,因此沒有(明顯)修復代碼的方法。

int fibA = 1, fibB = 2, total = 0; 

while(fibB <= 4000000) { 
    // Add to the total, set fibA to fibB and get the next value in the sequence. 
    if(fibB % 2 == 0) total += fibB; 
    int temp = fibA; 
    fibA = fibB; 
    fibB = fibB + temp; 
} 

上面的代碼應該找到的所有值的總和小於或等於400萬

0

這裏的執行是使用的BigInteger的解決方案。請驗證結果。

public class Fibonacci{ 

    public static void main(String[] args) { 
     BigInteger r = fibonacciEvenSum(); 
     System.out.println(r); 
    } 

    public static BigInteger fibonacciEvenSum(){ 
     int f = 1; 
     int s = 2; 
     int mn4 = 4000000; 
     BigInteger sum = BigInteger.valueOf(0); 

     while(s <= mn4){ 
      if(s % 2 == 0){ 
       sum = sum.add(BigInteger.valueOf(s)); 
      } 
      f = f + s; 
      s = s + f; 
     } 
     return sum; 
    } 

} 
0

在編寫這樣的程序之前,你應該首先考慮這個程序的基礎。你應該先了解如何在畢業之前生成一個斐波那契數列,然後再進行一系列的操作。我會給你我的解決方案,以便你能理解。

class euler2 { 
    public static void main(String[] args) { 
     int a = 0, b = 1; /* the first elements of Fibonacci series are generally 
          thought to be 0 and 1. Therefore the series is 0, 1, 1, 2, 3... . 
          I've initialized first and second elements such */ 
     double sum = 0; // The initial sum is zero of course. 
     while (b < 4000000) /* since b is the second term, it will be our control variable. 
          This wouldn't let us consider values above 4M. */ 
     { 
      int ob = b; // to swap the values of a and b. 
      b = a + b; // generating next in the series. 
      a = ob; // a is now the older value of b since b is now a + b. 
      if (b % 2 == 0) // if b is even 
       sum += b; // we add it to the sum 
     } 
     System.out.println(sum); // and now we just print the sum 
    } 
} 

希望這有助於!