Skip to content

Commit 92e5489

Browse files
committed
Added brainfuck code, hopefully it does die when merging
1 parent 839416e commit 92e5489

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

book.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@
163163
{
164164
"lang": "lolcode",
165165
"name": "LOLCODE"
166+
},
167+
{
168+
"lang": "bf",
169+
"name": "brainfuck"
166170
}
167171
]
168172
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
>,>,[<[>->+<[>]>[<+>-]<<[<]>-]>[-<+>]>[-<+<+>>]<]<.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
,>,>>>+[[-]<<<<[->>>+<<<]>[->>>+<<<]>>[-<+<<+>>>]>[-<+<<+>>>]<<[->->[-]+<[>-]>[->>]<<<]>[>]<[<<[-]>>[-<<+>>>+<]]<[<<[-]>>[-<<+>>>>+<<]]>>>[-]+>[-]<<[>-]>[<<<<.>>>>->>]<<]
2+

contents/euclidean_algorithm/euclidean_algorithm.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
5959
[import:2-17, lang:"emojicode"](code/emojicode/euclidean_algorithm.emojic)
6060
{% sample lang="lolcode" %}
6161
[import:25-40, lang="LOLCODE"](code/lolcode/euclid.lol)
62+
{% sample lang="bf" %}
63+
[import, lang="Brainfuck"](code/brainfuck/euclidean_sub.bf)
6264
{% endmethod %}
6365

6466
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:
@@ -124,6 +126,8 @@ Modern implementations, though, often use the modulus operator (%) like so
124126
[import:19-31, lang:"emojicode"](code/emojicode/euclidean_algorithm.emojic)
125127
{% sample lang="lolcode" %}
126128
[import:9-23, lang="LOLCODE"](code/lolcode/euclid.lol)
129+
{% sample lang="bf" %}
130+
[import, lang="Brainfuck"](code/brainfuck/euclidean_mod.bf)
127131
{% endmethod %}
128132

129133
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:
@@ -197,6 +201,114 @@ and modulo method:
197201
[import, lang:"emojicode"](code/emojicode/euclidean_algorithm.emojic)
198202
{% sample lang="lolcode" %}
199203
[import, lang="LOLCODE"](code/lolcode/euclid.lol)
204+
{% sample lang="bf" %}
205+
#### Subtraction varient
206+
##### Code
207+
[import, lang="Brainfuck"](code/brainfuck/euclidean_sub.bf)
208+
##### Explanation
209+
Basic plan: get |a-b|, check if 0
210+
211+
So the program does something like
212+
```
213+
a (b) 0 0 1 0 0
214+
a b a b (0) 0 0
215+
if(a>b) a b a-b 0 (a-b) 0 0
216+
else a b 0 a-b (a-b) 0 0
217+
if(a-b==0)print and break
218+
else
219+
if(a>b) a-b b 0 0 (a-b) 0 0
220+
else a a-b 0 0 (a-b) 0 0
221+
```
222+
223+
More detail:
224+
225+
`scan a,b`: `>,>,`
226+
State: `a (b) 0 0 0 0 0`
227+
```
228+
>>>+
229+
[
230+
[-]<<<
231+
<[->>>+<<<]>[->>>+<<<]>>
232+
```
233+
State: `0 0 0 (a) b 0 0`
234+
```
235+
[-<+<<+>>>]
236+
>[-<+<<+>>>]
237+
```
238+
State: `a b a b (0) 0 0`
239+
```
240+
<<
241+
[->- subtracts a from b, assuming a>b
242+
>[-]+
243+
<[
244+
>-]>
245+
[->>]<<< if a is 0, stop
246+
]
247+
```
248+
So basically the state will either be
249+
a_b (0) 0 0
250+
or
251+
(0) a_b 0 0
252+
but it's hard to do when states may be different, so `>[>]` moves the pointer to cell 4
253+
```
254+
<[<<[-]>>[-<<+>>>+<]]
255+
<[<<[-]>>[-<<+>>>>+<<]]
256+
```
257+
basically cell 4 will contain the difference
258+
```
259+
>>>[-]+
260+
>[-]<<
261+
[>-]>
262+
[<<<<.>>> testing if difference is 0, if so return
263+
>->>]<<
264+
]
265+
```
266+
267+
#### Modulo varient
268+
##### Code
269+
[import, lang="Brainfuck"](code/brainfuck/euclidean_mod.bf)
270+
##### Explanation
271+
`scan a,b`: `>,>,`
272+
273+
State: `0 a >b 0 0 0`
274+
275+
`while(b!=0)`: `[`
276+
277+
`a,b,0=0,b-a%b,a%b`:
278+
```
279+
<[
280+
>->+<[>]
281+
>[<+>-]<
282+
<[<]>-
283+
]
284+
```
285+
286+
so basically this is the modulo algorithm in brainfuck, it slowly shifts cell 2 to cell 3, while subtracting 1 from cell 1
287+
then when cell 2 goes to 0, it shifts cell 3 to 2 and continues, this is like just constantly subtracting cell 2 from cell 1, until you cant subtract anymore then return at cell 3
288+
289+
State: `0 >0 b-a%b a%b 0 0`
290+
291+
shifting: `>[-<+>]`
292+
293+
State: `0 b-a%b >0 a%b 0 0`
294+
295+
Currently we have a,b,0=b-a%b,0,a%b, and we need a,b,0=b,a%b,0, so we just add the third cell to the first and second cell
296+
297+
adding thing: `>[-<+<+>>]<`
298+
299+
State: `0 b >(a%b) 0 0 0`
300+
301+
So now we have a,b=b,a%b, we continue the loop
302+
303+
`]`
304+
305+
After the second cell is 0, the loop terminates and we obtain the GCD
306+
307+
State: `0 >GCD(a b) 0 0 0 0`
308+
309+
Now we print the GCD
310+
311+
print: `<.`
200312
{% endmethod %}
201313

202314
<script>

0 commit comments

Comments
 (0)