euler project

Computer 2008/08/05 16:25

최근에 재밌는 곳을 찾아서 적어 본다. 이름하여 project euler... http://projecteuler.net/ 200 가지 이상의 문제들이 있다. 문제를 풀어 답을 맞추게 되면 타인의 답안을 열람할 수 있다. 물론 사용자 등록과 로그인이 이루어져야 한다. 문제들이 너무 산수다... 시간이 정말 남을때만 하나씩 해야 겠다.

동일한 문제를 다양한 언어로 풀이된 것을 보고 있으면 견문이 넓어지는 기분이다. 물론 답을 맞추었을 경우의 이야기이다. 아래는 일번 문제와 다른 사람들의 몇가지 solution 을 적어본다.

함수형 언어를 공부해야 겠다는 다짐을 다시 하게된다...


문제


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.


solution


php

$x = 1000;
echo 1.5*(int)(($x-1)/3)*(int)(($x+2)/3) + 2.5*(int)(($x-1)/5)*(int)(($x+4)/5) - 7.5*(int)(($x-1)/15)*(int)(($x+14)/15);

haskell

sum [n | n <- [1..1000-1], n `mod` 5 == 0 || n `mod` 3 == 0]

erlang

lists:sum([X || X <- lists:seq(1,999), X rem 3 =:= 0 orelse X rem 5 =:= 0]).

python

sum([x for x in range(1,999) if x % 3 == 0 or x % 5 == 0])

assembly

; for each integer from 1 to 1000
mov ecx, 3
 
mov esi, 3
mov edi, 5
 
xor ebx, ebx ; sum
 
_0: mov eax, ecx
xor edx, edx
div esi
test edx, edx
je _yes
 
mov eax, ecx
xor edx, edx
div edi
test edx, edx
jne _no
 
_yes: add ebx, ecx
 
_no: inc ecx

cmp ecx, 1000
jne _0


이올린에 북마크하기(0) 이올린에 추천하기(0)
Posted by likechad