logo

짝수 피보나치 수의 합

GfG Practice에서 사용해 보세요. ' title= #practiceLinkDiv { 표시: 없음 !중요; }

주어진 극한에서 주어진 한계 아래의 피보나치 수열에서 모든 짝수 항의 합을 구합니다.
처음 몇 항은 피보나치 수열 1 1 이다 2 3 5 8 13 21 34 55 89 144 233 ... (짝수는 강조 표시됩니다).
예:  
 

Input : limit = 8 Output : 10 Explanation : 2 + 8 = 10 Input : limit = 400; Output : 188. Explanation : 2 + 8 + 34 + 144 = 188.


 

권장 실습 짝수 피보나치 수의 합 시도해 보세요!


간단한 해결책은 다음 숫자가 주어진 제한보다 작거나 같은 동안 모든 피보나치 숫자를 반복하는 것입니다. 모든 숫자에 대해 짝수인지 확인하세요. 숫자가 짝수라면 결과에 추가하세요.
효율적인 솔루션은 아래를 기반으로 합니다. 짝수 피보나치 수에 대한 재귀 공식
 



Recurrence for Even Fibonacci sequence is: EFn = 4EFn-1 + EFn-2 with seed values EF0 = 0 and EF1 = 2.   EFn   represents n'th term in Even Fibonacci sequence.


나타내다 이것 위 공식에 대한 자세한 내용.
따라서 피보나치 수를 반복하는 동안 우리는 짝수 피보나치 수만 생성합니다. 
 

C++
// Find the sum of all the even-valued terms in // the Fibonacci sequence which do not exceed // given limit. #include   using namespace std; // Returns sum of even Fibonacci numbers which are // less than or equal to given limit. int evenFibSum(int limit) {  if (limit < 2)  return 0;  // Initialize first two even Fibonacci numbers  // and their sum  long long int ef1 = 0 ef2 = 2;  long long int sum = ef1 + ef2;  // calculating sum of even Fibonacci value  while (ef2 <= limit)  {  // get next even value of Fibonacci sequence  long long int ef3 = 4*ef2 + ef1;  // If we go beyond limit we break loop  if (ef3 > limit)  break;  // Move to next even number and update sum  ef1 = ef2;  ef2 = ef3;  sum += ef2;  }  return sum; } // Driver code int main() {  int limit = 400;  cout << evenFibSum(limit);  return 0; } 
Java
// Find the sum of all the even-valued terms in // the Fibonacci sequence which do not exceed // given limit. import java.io.*; class GFG  {  // Returns sum of even Fibonacci numbers which are  // less than or equal to given limit.  static int evenFibSum(int limit)  {  if (limit < 2)  return 0;    // Initialize first two even Fibonacci numbers  // and their sum  long ef1 = 0 ef2 = 2;  long sum = ef1 + ef2;    // calculating sum of even Fibonacci value  while (ef2 <= limit)  {  // get next even value of Fibonacci sequence  long ef3 = 4 * ef2 + ef1;    // If we go beyond limit we break loop  if (ef3 > limit)  break;    // Move to next even number and update sum  ef1 = ef2;  ef2 = ef3;  sum += ef2;  }    return(int) sum;  }    // Driver code  public static void main (String[] args)   {  int limit = 400;  System.out.println(evenFibSum(limit));    } } // This code is contributed by vt_m. 
Python3
# Find the sum of all the even-valued  # terms in the Fibonacci sequence which  # do not exceed given limit. # Returns sum of even Fibonacci numbers which # are less than or equal to given limit. def evenFibSum(limit) : if (limit < 2) : return 0 # Initialize first two even Fibonacci numbers # and their sum ef1 = 0 ef2 = 2 sm= ef1 + ef2 # calculating sum of even Fibonacci value while (ef2 <= limit) : # get next even value of Fibonacci  # sequence ef3 = 4 * ef2 + ef1 # If we go beyond limit we break loop if (ef3 > limit) : break # Move to next even number and update # sum ef1 = ef2 ef2 = ef3 sm = sm + ef2 return sm # Driver code limit = 400 print(evenFibSum(limit)) # This code is contributed by Nikita Tiwari. 
C#
// C# program to Find the sum of all // the even-valued terms in the  // Fibonacci sequence which do not // exceed given limit.given limit. using System; class GFG {    // Returns sum of even Fibonacci   // numbers which are less than or  // equal to given limit.  static int evenFibSum(int limit)  {  if (limit < 2)  return 0;    // Initialize first two even  // Fibonacci numbers and their sum  long ef1 = 0 ef2 = 2;  long sum = ef1 + ef2;    // calculating sum of even   // Fibonacci value  while (ef2 <= limit)  {    // get next even value of   // Fibonacci sequence  long ef3 = 4 * ef2 + ef1;    // If we go beyond limit  // we break loop  if (ef3 > limit)  break;    // Move to next even number  // and update sum  ef1 = ef2;  ef2 = ef3;  sum += ef2;  }    return(int) sum;  }    // Driver code  public static void Main ()   {  int limit = 400;  Console.Write(evenFibSum(limit));    } } // This code is contributed by Nitin Mittal. 
PHP
 // Find the sum of all the  // even-valued terms in the  // Fibonacci sequence which  // do not exceed given limit. // Returns sum of even Fibonacci // numbers which are less than or  // equal to given limit. function evenFibSum($limit) { if ($limit < 2) return 0; // Initialize first two even  // Fibonacci numbers and their sum $ef1 = 0; $ef2 = 2; $sum = $ef1 + $ef2; // calculating sum of // even Fibonacci value while ($ef2 <= $limit) { // get next even value of // Fibonacci sequence $ef3 = 4 * $ef2 + $ef1; // If we go beyond limit // we break loop if ($ef3 > $limit) break; // Move to next even number // and update sum $ef1 = $ef2; $ef2 = $ef3; $sum += $ef2; } return $sum; } // Driver code $limit = 400; echo(evenFibSum($limit)); // This code is contributed by Ajit. ?> 
JavaScript
<script> // Javascript program to find the sum of all the even-valued terms in // the Fibonacci sequence which do not exceed // given limit.  // Returns sum of even Fibonacci numbers which are  // less than or equal to given limit.  function evenFibSum(limit)  {  if (limit < 2)  return 0;    // Initialize first two even Fibonacci numbers  // and their sum  let ef1 = 0 ef2 = 2;  let sum = ef1 + ef2;    // calculating sum of even Fibonacci value  while (ef2 <= limit)  {  // get next even value of Fibonacci sequence  let ef3 = 4 * ef2 + ef1;    // If we go beyond limit we break loop  if (ef3 > limit)  break;    // Move to next even number and update sum  ef1 = ef2;  ef2 = ef3;  sum += ef2;  }    return sum;  }   // Function call    let limit = 400;  document.write(evenFibSum(limit));   </script> 

출력 :  
 

188

시간 복잡도: 에)

보조 공간: 오(1)

java 배열을 반환하는 방법