-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.fth
140 lines (114 loc) · 2.53 KB
/
test.fth
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
( Nip: drop the item one back on the stack: swap drop )
( tuck: put a copy of the current item behind the last item dup rot rot)
( [a-b]*[a - 1] 11 6 swap dup rot - swap 1 - * )
: myNip ( n1 n2 -- n2 n1 )
swap drop ;
: myTuck ( n1 n2 -- n2 n1 n2 )
dup rot rot ;
: thirdThing ( n1 n2 -- [n1-n2]*[n1-1] )
swap dup rot - swap 1 - * ;
: myNegate ( n1 -- -n1 )
0 swap - ;
: myDivMod ( n1 n2 -- [n1/n2] [n1 mod n2] )
2dup mod rot rot / ;
: myDivModLocal { n1 n2 -- [n1/n2] [n1 mod n2] }
n1 n2 mod n1 n2 / ;
: myAbs ( n1 -- +n1 )
dup 0 < if
negate
endif ;
: myMin ( n1 n2 -- n )
2dup < if
drop
else
nip
endif ;
: myMin2 ( n1 n2 -- n )
2dup > if
swap
endif
drop ;
: theirMin ( n1 n2 -- n )
2dup 2dup < rot rot >= rot and rot rot and + ;
: theirMin2 ( n1 n2 -- n )
2dup 2dup > and rot rot >= rot and + ;
: theirMin3 ( n1 n2 -- n )
2dup tuck > and swap rot tuck >= and + ;
: theirMinLocal { n1 n2 -- n }
n1 n1 n2 < and n2 n1 n2 > and + ;
: myMax ( n1 n2 -- n )
2dup > if
drop
else
nip
endif ;
: 2mod/
dup 2/ swap 1 and ;
: log2 ( +n1 -- n2 )
\ logarithmus dualis of n1>0, rounded down to the next integer
assert( dup 0> )
2/ 0 begin
over 0> while
1+ swap 2/ swap
repeat
nip ;
: pow ( n1 n2 -- n1^n2 )
1 swap
begin
dup 0> while
2mod/ 0= if
rot dup * -rot
else
-rot over * swap dup * swap rot
endif
repeat
drop nip ;
: theirPow
\ n = the uth power of n1
1 swap 0 u+do
over *
loop
nip ;
\ 1 2 3 4 5 6
\ 1 1 3 5 8 13
: fib ( n -- n! )
assert( dup 0> )
1 1 rot 2 u+do
tuck +
loop
nip ;
: fib-rec-int
CR .s
dup 2 > if
1- -rot tuck + rot recurse
else
drop nip
endif ;
: fib-rec ( n -- n! )
assert( dup 0> )
1 1 rot fib-rec-int ;
: gcd ( n1 n2 -- n )
0 rot rot
begin
dup 1 and rot dup 1 and rot or 0= while
2/ rot 1+ rot 2/ rot
repeat
begin
2dup <> while
dup 1 and 0= if
2/
else
swap dup 1 and 0= if
2/
else
2dup < if
swap
endif
tuck - 2/
endif
endif
repeat
drop 2 rot pow * ;
: @R I ;
: quadratic ( a b c x -- [ax + b]x + c )
>R swap rot R@ * + R> * + ;