티스토리 툴바

Programming Language

'Programming Language/Factor'에 해당되는 글 2건

  1. 2009/02/19 Project Euler 2번 문제 factor 코드로 풀기 (5)
  2. 2009/02/15 Project Euler 1번 문제 factor 코드로 풀기

우아하지는 않네요.ㅋㅋ
리팩토링 해봐야지.

문제

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.



Source

: sum-all-even-valued-of-fibonacci-until-4000000 ( x y z -- t ) [ dup swapd + dup 4000000 < [ dup even? [ dup roll + -rot t ] [ t ] if ] [ . . f ] if ] loop ;
:  project-euler2 ( x y z -- ) 0 0 1 sum-all-even-valued-of-fibonacci-until-4000000 ;
Posted by 창민짱 Trackback 0 Comment 5
문제

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.


처음으로 작성한 factor code 네요..
막 작성을 시작할때는 막막했는데 문제를 하나씩 해결해 나가다 보니 완성이 되었네요 ^^

이번일을 계기로 실패를 두려워 해서는 안되겠다는 생각이 듭니다.^^


Source

: sum-multipler ( x -- y ) dup 3 mod zero? [ + ] [ dup 5 mod zero? [ + ] [ . ] if ] if ; 
: project-euler1 ( -- ) 0 1000 >array [ sum-multipler ] each ; 
project-euler1
project-euler 사이트






Posted by 창민짱 Trackback 0 Comment 0