Mormegil avatar

EulerProject Mathematical and Programming Problems (Answer + Analysis) #2

kdcory

Published: 12 Jul 2017 › Updated: 12 Jul 2017EulerProject Mathematical and Programming Problems (Answer + Analysis) #2

EulerProject Mathematical and Programming Problems (Answer + Analysis) #2

Today, I bring to you a solution to the second EulerProject problem. Without further ado, let us examine my solution.

Leonhard_Euler.jpg

Let us first enjoy this marvellous painting of the man himself, Leonhard Euler


The Problem

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.


The Solution

#include 
 
#define MAX_FIB 4000000
 
int main(void) {
    int a = 1, b = 1, c = 1, sum = 0;
    while (c <= MAX_FIB) {
        if (c % 2 == 0)
            sum += c;
        c = a + b;
        a = b;
        b = c;
    }
    printf("\nSum: %d\n", sum);
    return 0;

}

You may have heard of the Fibonacci sequence and for good reason. Fibonacci was a very prominent mathematician whose theories have fuelled many modern mathematical theories. For example, the golden ratio: if you take two successive numbers from the Fibonacci sequence and look at the ratio between them, you will find that it is very close to the golden ratio.

But enough about math history, let's continue with the problem. What this code does is firstly define a constant (to improve readability) that is the number that should not be passed, as stated in the problem.

Secondly, it defines four variables, one for storing the total sum of the numbers, two variables for storing two fibonacci numbers and a final variable for storing the result of the two previous numbers - the newest Fibonacci number.

Thirdly, the program uses the modulus operator to check whether the newest Fibonacci number is even, as stated in the problem. It does this by returning the remainder of a division. Thus, if the remainder of the number divided by two is 0, this means the number divides perfectly by two which means that it is as even number.

Finally, it updates the variables to move up a step within the Fibonacci sequence and adds the current number to the sum variable. It then outputs the sum variable.

I ran this program and surprisingly, it worked first time and output 4613732. I then put this number into the EulerProject website and it showed this:

prob2ans.png

As you can see, the program output the correct answer


That is all for today, folks, I hope you have found this somewhat useful. Expect the next solution within the next few days; and until the, have a good one :)


Links
https://projecteuler.net/problem=2

Leave EulerProject Mathematical and Programming Problems (Answer + Analysis) #2 to:

Written by

Computer Science Student with the Dream of Living in a Forest One Day

Read more #programming posts


Best Posts From Mormegil

We have not curated any of kdcory's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Mormegil