Skip to content

Commit 0435b24

Browse files
author
Julian
authored
Merge pull request #448 from ThijsRay/euclidian_algorithm_in_piet
Euclidian Algorithm Piet Implementation
2 parents fb4f592 + 539cd58 commit 0435b24

8 files changed

+166
-0
lines changed

CONTRIBUTORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ This file lists everyone, who contributed to this repo and wanted to show up her
3737
- Ken Power
3838
- PaddyKe
3939
- nic-hartley
40+
- Thijs Raymakers
4041
- crafter312
4142
- Christopher Milan
4243
- Vexatos

book.json

+4
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@
167167
{
168168
"lang": "lolcode",
169169
"name": "LOLCODE"
170+
},
171+
{
172+
"lang": "piet",
173+
"name": "Piet"
170174
}
171175
]
172176
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
This file describes the path that the Piet program takes in a more human readable form.
2+
Two functions are implemented:
3+
- GCD with subtraction
4+
- GCD with the modulo operator
5+
6+
-------------------
7+
SUBTRACTION
8+
-------------------
9+
10+
Pseudo code:
11+
12+
function gcd(a, b)
13+
while a ≠ b
14+
if a > b
15+
a := a − b;
16+
else
17+
b := b − a;
18+
return a;
19+
20+
21+
Piet code:
22+
23+
COMMAND STATE OF STACK
24+
in(number) A // Take A as an input
25+
duplicate AA // Start to take the absolute value of A
26+
push 1 1AA
27+
duplicate 11AA
28+
subtract 0AA
29+
greater 0/1A // 1 if A > 0, 0 if A <= 0
30+
not 1/0A // 0 if A > 0, 1 if A <= 0
31+
push 1 1 1/0 A
32+
push 3 31 1/0 A
33+
subtract -2 1/0 A
34+
multiply -2/0 A
35+
push 1 1 -2/0 A
36+
add -1/1 A
37+
multiply A // A should now be an absolute value
38+
39+
in(number) BA // Take B as an input
40+
duplicate BBA // Start to take the absolute value of B
41+
push 1 1BBA
42+
duplicate 11BBA
43+
subtract 0BBA
44+
greater 0/1BA // 1 if B > 0, 0 if B <= 0
45+
not 1/0BA // 0 if B > 0, 1 if B <= 0
46+
push 1 1 1/0 BA
47+
push 3 31 1/0 BA
48+
subtract -2 1/0 BA
49+
multiply -2/0 BA
50+
push 1 1 -2/0 BA
51+
add -1/1 BA
52+
multiply BA // B should now be an absolute value
53+
54+
// Start of the main loop while a ≠ b
55+
duplicate BBA
56+
push 3 3BBA
57+
push 2 23BBA
58+
roll ABB
59+
duplicate AABB
60+
push 4 4AABB
61+
push 1 14AABB
62+
roll ABBA
63+
subtract 0/x BA
64+
not 1/0 BA // 1 if a = b and 0 if a ≠ b
65+
not 0/1 BA // 1 if a ≠ b and 0 if a = b
66+
pointer BA // If a ≠ b, the DP should change one clockwise, otherwise, go straight ahead.
67+
68+
// Go left if a ≠ b (DP changed one clockwise)
69+
duplicate BBA
70+
push 3 3BBA
71+
push 2 23BBA
72+
roll ABB
73+
duplicate AABB
74+
push 4 4AABB
75+
push 1 14AABB
76+
roll ABBA
77+
push 2 2ABBA
78+
push 1 12ABBA
79+
roll BABA
80+
greater 0/1 BA // A > B; 1 if true; 0 if false
81+
pointer BA // If A > B, DP goes one clockwise, otherwise, DP stays the same.
82+
83+
// If A > B (DP has changed 1 clockwise)
84+
duplicate BBA
85+
push 3 3BBA
86+
push 1 13BBA
87+
roll BAB
88+
subtract AB // A = A - B
89+
push 2 2AB
90+
push 1 12AB
91+
roll BA
92+
// Go back to start of loop
93+
94+
// If B > A (DP stayed the same)
95+
push 2 2BA
96+
push 1 12BA
97+
roll AB
98+
duplicate AAB
99+
push 3 3AAB
100+
push 1 13AAB
101+
roll ABA
102+
subtract BA // B = B - A
103+
// Go back to start of loop
104+
105+
// Go down if a = b (end of while loop)
106+
pop A
107+
out(number) - // Print out A when done.
108+
109+
---------------------------------------------------------------------------
110+
111+
-------------------
112+
MODULO
113+
-------------------
114+
115+
Pseudo code:
116+
117+
function gcd(a, b)
118+
while b ≠ 0
119+
t := b;
120+
b := a mod b;
121+
a := t;
122+
return a;
123+
124+
Piet code:
125+
126+
COMMAND STATE OF STACK
127+
in(number) A
128+
in(number) BA
129+
130+
// Start of loop
131+
duplicate BBA
132+
not 0/1 BA
133+
not 1/0 BA
134+
pointer BA
135+
136+
// Go down if b ≠ 0
137+
duplicate TBA
138+
push 3 3TBA
139+
push 1 13TBA
140+
roll BAT
141+
mod BA // b = a mod b; a = t
142+
// Go back to the start of the loop
143+
144+
// Go right if b = 0
145+
pop A
146+
out(number) - // Print out A when done.
Loading
Loading
Loading
Loading

contents/euclidean_algorithm/euclidean_algorithm.md

+15
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
6363
[import:25-40, lang="LOLCODE"](code/lolcode/euclid.lol)
6464
{% sample lang="bash" %}
6565
[import:24-38, lang="bash"](code/bash/euclid.bash)
66+
{% sample lang="piet" %}
67+
> ![](code/piet/subtract/euclidian_algorithm_subtract_large.png) ![](code/piet/subtract/euclidian_algorithm_subtract.png)
6668
{% endmethod %}
6769

6870
Here, we simply line the two numbers up every step and subtract the lower value from the higher one every timestep. Once the two values are equal, we call that value the greatest common divisor. A graph of `a` and `b` as they change every step would look something like this:
@@ -132,6 +134,8 @@ Modern implementations, though, often use the modulus operator (%) like so
132134
[import:9-23, lang="LOLCODE"](code/lolcode/euclid.lol)
133135
{% sample lang="bash" %}
134136
[import:10-22, lang="bash"](code/bash/euclid.bash)
137+
{% sample lang="piet" %}
138+
> ![](code/piet/mod/euclidian_algorithm_mod_large.png) ![](code/piet/mod/euclidian_algorithm_mod.png)
135139
{% endmethod %}
136140

137141
Here, we set `b` to be the remainder of `a%b` and `a` to be whatever `b` was last timestep. Because of how the modulus operator works, this will provide the same information as the subtraction-based implementation, but when we show `a` and `b` as they change with time, we can see that it might take many fewer steps:
@@ -217,6 +221,17 @@ and modulo method:
217221
[import, lang="LOLCODE"](code/lolcode/euclid.lol)
218222
{% sample lang="bash" %}
219223
[import, lang="bash"](code/bash/euclid.bash)
224+
{% sample lang="piet" %}
225+
A text version of the program is provided for both versions.
226+
#### Subtraction
227+
> ![](code/piet/subtract/euclidian_algorithm_subtract_large.png) ![](code/piet/subtract/euclidian_algorithm_subtract.png)
228+
229+
[import:23-107](code/piet/euclidian_algorithm.piet)
230+
231+
#### Modulo
232+
> ![](code/piet/mod/euclidian_algorithm_mod_large.png) ![](code/piet/mod/euclidian_algorithm_mod.png)
233+
234+
[import:126-146](code/piet/euclidian_algorithm.piet)
220235
{% endmethod %}
221236

222237
<script>

0 commit comments

Comments
 (0)