プロジェクト・オイラーのアーカイブスからの出典です。
This problem is a programming version of Problem 2 from projecteuler.net.
https://projecteuler.net
プロジェクト・オイラーは、プログラミング (コンピュータ)による一連の計算問題の解決を目的としたウェブサイトです。
Project Euler is a website dedicated to a series of computational problems intended to be solved with computer programs.
プロジェクト・オイラー - Wikipedia
・Project Euler #2: 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.
僕が作成して、解答を求めたコードは、以下のとおりです。
My code was written in C.
/* Project Euler in C #2: Even Fibonacci numbers https://projecteuler.net My code https://neguse-atama.hatenablog.com */ #include<stdio.h> int fib_n(int n){ int fib1=1; int fib2=1; int loop,fib; for(loop=1;loop<n;loop++){ fib=fib1+fib2; fib1=fib2; fib2=fib; } return fib2; } int main(void){ int loop,fib; int sum=0; for(loop=1; ;loop++){ fib=fib_n(loop); if(fib>4000000){ break; } if(fib%2==0){ sum+=fib; } } printf("sum:%d\n",sum); return 0; }
フィボナッチ数列において、数列の項の値≧4000000の、偶数値の項の総和を求める問題でした。
フィボナッチ数列は、n 番目のフィボナッチ数を Fn で表すと、Fn は再帰的に
で定義される数列 (Fn)です。
フィボナッチ数 - Wikipedia