Created
October 20, 2012 23:59
-
-
Save rclabs/3925294 to your computer and use it in GitHub Desktop.
forth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
." hello world" cr | |
: hello ." hello world" cr ; | |
: 3times hello hello hello ; | |
: ntimes ( n -- ) begin dup 0> while hello 1- repeat drop ; | |
: sum ( xi n -- sum ) 0 swap begin dup 0> while 1- -rot + swap repeat drop ; | |
: store-ar ( xi n ^ "name" -- ) create dup , begin dup 0> while 1- swap , repeat drop ; | |
: sum-ar ( name-of-array -- sum ) 0 swap dup @ begin dup 0> while 1- swap cell+ rot swap dup @ rot + -rot swap repeat 2drop ; | |
: get-ar-i ( index name-of-array -- i ) swap 1+ cells + @ ; | |
\ project euler problem1 sum of all number less than 1000 | |
\ that are factors of 3 and 5 | |
: problem1 ( -- sum-35 ) | |
0 | |
999 | |
begin | |
dup | |
0> | |
while | |
dup 3 | |
mod | |
0= | |
over 5 | |
mod | |
0= | |
or | |
if | |
2dup | |
+ | |
-rot | |
swap | |
drop | |
then | |
1- | |
repeat | |
drop | |
; | |
\ problem 2 | |
\ sum of even numbers of the fibinacci sequence | |
\ starting with 1 2 (3 5 ...) | |
: next-fib ( n1 n2 -- n2 n3) | |
swap over + ; | |
: problem2 ( -- sum ) | |
0 | |
1 | |
2 | |
begin | |
dup | |
4000000 | |
< | |
while | |
dup | |
2 | |
mod | |
0= | |
if | |
rot | |
over | |
+ | |
-rot | |
then | |
next-fib | |
repeat | |
2drop | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment