project euler

Project Eulerをvimscriptで解く。楽しい。

とりあえず1, 2問くらいは載せてみる。

" 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.

let i = 1
let sum = 0
while i < 1000
    if (i % 3) == 0 || (i % 5) == 0
        let sum += i
    endif 
    let i += 1
endwhile

echo "sum : " . sum
" 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.

function! Fibo(list,v1,v2)
    call add(a:list,a:list[a:v1]+a:list[a:v2])
    return a:list[-1]
endfunction

let list = [1, 2]
let v1 = 0
let v2 = 1
let sum = list[v1] + list[v2]

while 1
    let sum += Fibo(list,v1,v2)
    let v1 += 1
    let v2 += 1
    if list[v2] > 4000000
        break
    endif
endwhile

echo "sum : " . sum