Skip to content

Commit 7372cd9

Browse files
committed
Added euclid algo in bf
1 parent 95a2811 commit 7372cd9

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
>,>,[<[>->+<[>]>[<+>-]<<[<]>-]>[-<+>]>[-<+<+>>]<]<.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
,>,>>>+[[-]<<<<[->>>+<<<]>[->>>+<<<]>>[-<+<<+>>>]>[-<+<<+>>>]<<[->->+<[>-]>[->>]<<<]>[>]<[<<[-]>>[-<<+>>>+<]]<[<<[-]>>[-<<+>>>>+<<]]>>>+<[>-]>[<<<<.>>>>->>]<<]
2+

contents/euclidean_algorithm/euclidean_algorithm.md

+112
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="bf" %}
67+
[import, lang="brainfuck"](code/brainfuck/euclidean_sub.bf)
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="bf" %}
138+
[import, lang="brainfuck"](code/brainfuck/euclidean_mod.bf)
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:
@@ -209,6 +213,114 @@ and modulo method:
209213
[import, lang="LOLCODE"](code/lolcode/euclid.lol)
210214
{% sample lang="bash" %}
211215
[import, lang="bash"](code/bash/euclid.bash)
216+
{% sample lang="bf" %}
217+
#### Subtraction varient
218+
##### Code
219+
[import, lang="brainfuck"](code/brainfuck/euclidean_sub.bf)
220+
##### Explanation
221+
Basic plan: get |a-b|, check if 0
222+
223+
So the program does something like
224+
```
225+
a (b) 0 0 1 0 0
226+
a b a b (0) 0 0
227+
if(a>b) a b a-b 0 (a-b) 0 0
228+
else a b 0 a-b (a-b) 0 0
229+
if(a-b==0)print and break
230+
else
231+
if(a>b) a-b b 0 0 (a-b) 0 0
232+
else a a-b 0 0 (a-b) 0 0
233+
```
234+
235+
More detail:
236+
237+
`scan a,b`: `>,>,`
238+
State: `a (b) 0 0 0 0 0`
239+
```
240+
>>>+
241+
[
242+
[-]<<<
243+
<[->>>+<<<]>[->>>+<<<]>>
244+
```
245+
State: `0 0 0 (a) b 0 0`
246+
```
247+
[-<+<<+>>>]
248+
>[-<+<<+>>>]
249+
```
250+
State: `a b a b (0) 0 0`
251+
```
252+
<<
253+
[->- subtracts a from b, assuming a>b
254+
>[-]+
255+
<[
256+
>-]>
257+
[->>]<<< if a is 0, stop
258+
]
259+
```
260+
So basically the state will either be
261+
a_b (0) 0 0
262+
or
263+
(0) a_b 0 0
264+
but it's hard to do when states may be different, so `>[>]` moves the pointer to cell 4
265+
```
266+
<[<<[-]>>[-<<+>>>+<]]
267+
<[<<[-]>>[-<<+>>>>+<<]]
268+
```
269+
basically cell 4 will contain the difference
270+
```
271+
>>>[-]+
272+
>[-]<<
273+
[>-]>
274+
[<<<<.>>> testing if difference is 0, if so return
275+
>->>]<<
276+
]
277+
```
278+
279+
#### Modulo varient
280+
##### Code
281+
[import, lang="brainfuck"](code/brainfuck/euclidean_mod.bf)
282+
##### Explanation
283+
`scan a,b`: `>,>,`
284+
285+
State: `0 a >b 0 0 0`
286+
287+
`while(b!=0)`: `[`
288+
289+
`a,b,0=0,b-a%b,a%b`:
290+
```
291+
<[
292+
>->+<[>]
293+
>[<+>-]<
294+
<[<]>-
295+
]
296+
```
297+
298+
so basically this is the modulo algorithm in brainfuck, it slowly shifts cell 2 to cell 3, while subtracting 1 from cell 1
299+
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
300+
301+
State: `0 >0 b-a%b a%b 0 0`
302+
303+
shifting: `>[-<+>]`
304+
305+
State: `0 b-a%b >0 a%b 0 0`
306+
307+
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
308+
309+
adding thing: `>[-<+<+>>]<`
310+
311+
State: `0 b >(a%b) 0 0 0`
312+
313+
So now we have a,b=b,a%b, we continue the loop
314+
315+
`]`
316+
317+
After the second cell is 0, the loop terminates and we obtain the GCD
318+
319+
State: `0 >GCD(a b) 0 0 0 0`
320+
321+
Now we print the GCD
322+
323+
print: `<.`
212324
{% endmethod %}
213325

214326
<script>

0 commit comments

Comments
 (0)