ProjectEuler
登録してみた。
http://projecteuler.net/index.php
とりあえず2問だけ解いた。
使用言語はGauche。
Cとかで書けるようになりたい><
Problem 1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
(define (add-n-num n) (define (iter result count) (cond [(= n count) result] [(= 0 (* (modulo count 3) (modulo count 5))) (iter (+ result count) (+ count 1))] [else (iter result (+ count 1))])) (iter 0 0))
Problem 2
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, ... Find the sum of all the even-valued terms in the sequence which do not exceed four million.
;;フィボナッチ配列の定義 (define (fib n) (define (iter-fib a b count) (if (= count 0) b (iter-fib (+ a b) a (- count 1)))) (iter-fib 1 1 n)) ;;和を求める (define (even-valued-fib-sum max-value) (define (iter-sum sum-of-terms count) (let1 fibonacci (fib count) (cond [(> fibonacci max-value) sum-of-terms] [(even? (fib count)) (iter-sum (+ sum-of-terms fibonacci) (+ count 1))] [else (iter-sum sum-of-terms (+ count 1))]))) (iter-sum 0 1))
ちょっとずつ解いていこう。