diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2013-02-13 22:58:51 +0200 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2013-02-13 22:58:51 +0200 |
| commit | 77cdad53d8673c731718723250953f6c4ed5d504 (patch) | |
| tree | 9ab74f43039ef340aa72183cb9c6f18e102ae90d | |
| parent | 88ce15fb62cf615cdae0703c6361373644906f13 (diff) | |
| download | euler-c-77cdad53d8673c731718723250953f6c4ed5d504.tar.gz euler-c-77cdad53d8673c731718723250953f6c4ed5d504.zip | |
problem2
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | p2/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | p2/main.c | 51 |
3 files changed, 57 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 72c67ef..7e1b6b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,2 +1,3 @@ cmake_minimum_required(VERSION 2.8) add_subdirectory(p1) +add_subdirectory(p2) diff --git a/p2/CMakeLists.txt b/p2/CMakeLists.txt new file mode 100644 index 0000000..ddc870e --- /dev/null +++ b/p2/CMakeLists.txt @@ -0,0 +1,5 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +PROJECT(euler_problem2 C) +INCLUDE_DIRECTORIES(../common) +ADD_EXECUTABLE(p2 main.c ../common/utils.c) +TARGET_LINK_LIBRARIES(p2 m)
\ No newline at end of file diff --git a/p2/main.c b/p2/main.c new file mode 100644 index 0000000..b2d2a45 --- /dev/null +++ b/p2/main.c @@ -0,0 +1,51 @@ +/* + * 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. + */ + +#include <stdio.h> +#include <math.h> + + #include "utils.h" + +// /* http://www.evanmiller.org/mathematical-hacker.html */ +// long int fib(unsigned long int n) +// { +// return lround((pow(0.5 + 0.5 * sqrt(5.0), n) - +// pow(0.5 - 0.5 * sqrt(5.0), n)) / sqrt(5.0)); +// } + +// /* http://blog.noblemail.ca/2013/01/on-calculating-fibonacci-numbers-in-c.html */ +// long int fib2(unsigned long int n) +// { +// return lround((pow(0.5 + 0.5 * sqrt(5.0), n)) / sqrt(5.0)); +// } + +int main(int argc, char **argv) +{ + int i = 0; + int last = 0; + int sum = 0; + + last = fib(2); + + for (i = 3; last <= 4000000; i++) + { + /* even, if the lsb is 0 */ + if (!(last & 1)) + { + sum += last; + } + + last = fib(i); + } + + printf("result: %d\n", sum); + + return 0; +} |
