[Euler's plan 2] Even Fibonacci numbers

Source: Internet
Author: User
Document directory
  • Original ENGLISH
  • Description
  • My Solutions
  • Direct Solution
  • Another method
  • Efficient Solution

This question comes from the second question of Project Euler, and calculates the sum of an even number less than 4 million. Continue with my Euler's plan today.

Problem description

Even Fibonacci numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

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

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Description

In the Fibonacci series, each item is generated by the sum of the first two items. From 1 to 2, the top 10 items are:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ,...
Consider the number of items in the Fibonacci series that cannot exceed 4 million, and obtain the sum of the items whose values are even numbers.

Solution my solution

This is a well-defined method. It uses the point loop queue idea, although it can be replaced by several variables. During the code writing process, I also made a silly mistake. Thanks to the help of the Forum friends.

# Include <stdio. h> # define bound 4000000int main (void) {int fib [3]; int I; long int sum; FIB [0] = 1; FIB [1] = 2; sum = 2; for (I = 2; FIB [(I-1) % 3] <bound; I ++) {fib [I % 3] = fib [(I-1) % 3] + fib [(I-2) % 3]; // queue idea if (FIB [I % 3] % 2 = 0) {sum + = fib [I % 3] ;}} printf ("% d \ n", FIB [(I-2) % 3]); printf ("% d", sum );}

The following are several solutions provided by question resolution.

Direct Solution

This method works the same way as my method.

# include <stdio.h>int main(void){int limit;int a, b, h;int sum;limit = 4000000;a = b = 1;sum = 0;while(b < limit) {if(b % 2 == 0) {sum += b;}h = a + b;a = b;b = h;}printf("%d", sum);}
Another method

After carefully observing the series, we can find that each of the three fibre nacci numbers has an even number. With this rule, conditional judgment can be omitted.

# include <stdio.h>int main(void){int limit;int a, b, c;int sum;limit = 4e6;a = 1;b = 1;c = 2;sum = 0;while(c < limit) {sum += c;a = b + c;b = a + c;c = a + b;}printf("%d", sum);}
Efficient Solution

If you take out the even number of items:
34,144 ......

They follow these rules:
E (n) = 4 * E (n-1) + E (n-2)

# Include <stdio. h> int main (void) {int limit; int A, B; int sum; Limit = 4e6; sum = 2; A = 2; B = 8; while (B <limit) {sum + = B; B = 4 * B + A; A = (B-a)/4; // if no third variable is used, the variable is moved back.} printf ("% d", sum );}

To prove that the above relation is true, you only need to prove:
F (n) = 4 * F (n-3) + f (n-6)

The verification process is as follows:
F (n) = f (n-1) + f (n-2)
= F (n-2) + f (n-3) + f (n-2) = 2 F (n-2) + f (n-3)
= 2 (f (n-3) + f (n-4) + f (n-3) = 3 F (n-3) + 2 F (n-4)
= 3 F (n-3) + f (n-4) + f (N-5) + f (n-6)
= 4 f (n-3) + f (n-6)

Final answer

4613732

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.