操作
Problem 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, \dots$$
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
偶数のフィボナッチ数¶
フィボナッチ数列の項は前の2つの項の和である. 最初の2項を 1, 2 とすれば, 最初の10項は以下の通りである.
$$1, 2, 3, 5, 8, 13, 21, 34, 55, 89, \dots$$
数列の項の値が400万以下のとき, 値が偶数の項の総和を求めよ.
#!r6rs
#!chezscheme
(import (chezscheme))
(define (fib-numbers num)
(let loop ([first 0] [second 1] [result '(1 0)])
(let ([next (+ first second)])
(if (< num next)
result
(loop second next (cons next result))))))
(define (even-fib-numbers num)
(filter even? (fib-numbers num)))
(define answer-2
(apply + (even-fib-numbers 4000000)))
(printf "2: ~D~%" answer-2)
Noppi が2023/12/28に更新 · 4件の履歴