Skip to content

Commit cc6331c

Browse files
committed
Added brainfuck code and explanation
1 parent 44181dc commit cc6331c

File tree

6 files changed

+146
-0
lines changed

6 files changed

+146
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,5 @@ __pycache__/
498498

499499
-# Other
500500
-*.xcf
501+
502+
listproj.py

book.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@
139139
{
140140
"lang": "f90",
141141
"name": "Fortran90"
142+
},
143+
{
144+
"lang": "bf",
145+
"name": "Brainfuck"
142146
}
143147
]
144148
}
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: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
4141
[import:13-24, lang="nim"](code/nim/euclid_algorithm.nim)
4242
{% sample lang="f90" %}
4343
[import:1-19, lang="Fortran"](code/fortran/euclidean.f90)
44+
{% sample lang="bf" %}
45+
[import:1-19, lang="Brainfuck"](code/brainfuck/euclidean_sub.bf)
4446
{% endmethod %}
4547

4648
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:
@@ -88,6 +90,8 @@ Modern implementations, though, often use the modulus operator (%) like so
8890
[import:1-11, lang="nim"](code/nim/euclid_algorithm.nim)
8991
{% sample lang="f90" %}
9092
[import:21-34, lang="Fortran"](code/fortran/euclidean.f90)
93+
{% sample lang="bf" %}
94+
[import:1-19, lang="Brainfuck"](code/brainfuck/euclidean_mod.bf)
9195
{% endmethod %}
9296

9397
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:
@@ -140,6 +144,116 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout
140144
[import, lang="nim" %](code/nim/euclid_algorithm.nim)
141145
{% sample lang="f90" %}
142146
[import, lang="Fortran"](code/fortran/euclidean.f90)
147+
{% sample lang="bf" %}
148+
#### Subtraction varient
149+
##### Code
150+
[import, lang="Brainfuck"](code/brainfuck/euclidean_sub.bf)
151+
##### Explanation
152+
Basic plan: get |a-b|, check if 0
153+
154+
So the program does something like
155+
```
156+
a (b) 0 0 1 0 0
157+
a b a b (0) 0 0
158+
if(a>b) a b a-b 0 (a-b) 0 0
159+
else a b 0 a-b (a-b) 0 0
160+
if(a-b==0)print and break
161+
else
162+
if(a>b) a-b b 0 0 (a-b) 0 0
163+
else a a-b 0 0 (a-b) 0 0
164+
```
165+
166+
More detail:
167+
168+
`scan a,b`: `>,>,`
169+
State: `a (b) 0 0 0 0 0`
170+
```
171+
>>>+
172+
[
173+
[-]<<<
174+
<[->>>+<<<]>[->>>+<<<]>>
175+
```
176+
State: `0 0 0 (a) b 0 0`
177+
```
178+
[-<+<<+>>>]
179+
>[-<+<<+>>>]
180+
```
181+
State: `a b a b (0) 0 0`
182+
```
183+
<<
184+
[->- subtracts a from b, assuming a>b
185+
>[-]+
186+
<[
187+
>-]>
188+
[->>]<<< if a is 0, stop
189+
]
190+
```
191+
So basically the state will either be
192+
a_b (0) 0 0
193+
or
194+
(0) a_b 0 0
195+
but it's hard to do when states may be different, so `>[>]` moves the pointer to cell 4
196+
```
197+
<[<<[-]>>[-<<+>>>+<]]
198+
<[<<[-]>>[-<<+>>>>+<<]]
199+
```
200+
basically cell 4 will contain the difference
201+
```
202+
>>>[-]+
203+
>[-]<<
204+
[>-]>
205+
[<<<<.>>> testing if difference is 0, if so return
206+
>->>]<<
207+
]
208+
```
209+
210+
#### Modulo varient
211+
##### Code
212+
[import, lang="Brainfuck"](code/brainfuck/euclidean_mod.bf)
213+
##### Explanation
214+
`scan a,b`: `>,>,`
215+
216+
State: `0 a >b 0 0 0`
217+
218+
`while(b!=0)`: `[`
219+
220+
`a,b,0=0,b-a%b,a%b`:
221+
```
222+
<[
223+
>->+<[>]
224+
>[<+>-]<
225+
<[<]>-
226+
]
227+
```
228+
229+
so basically this is the modulo algorithm in brainfuck, it slowly shifts cell 2 to cell 3, while subtracting 1 from cell 1
230+
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
231+
232+
State: `0 >0 b-a%b a%b 0 0`
233+
234+
shifting: `>[-<+>]`
235+
236+
State: `0 b-a%b >0 a%b 0 0`
237+
238+
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
239+
240+
adding thing: `>[-<+<+>>]<`
241+
242+
State: `0 b >(a%b) 0 0 0`
243+
244+
So now we have a,b=b,a%b, we continue the loop
245+
246+
`]`
247+
248+
After the second cell is 0, the loop terminates and we obtain the GCD
249+
250+
State: `0 >GCD(a b) 0 0 0 0`
251+
252+
Now we print the GCD
253+
254+
print: `<.`
255+
256+
143257
{% endmethod %}
144258

145259

listproj.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from subprocess import check_output
2+
files=list(filter(None,check_output("find contents/ -type d -wholename 'contents/*/code/*' | sed -e 's!contents/\(.*\)/(code/\(.*\)!\1 \2!'",shell=True).decode('utf-8').split('\n')))
3+
langs=[]
4+
projs=[]
5+
lproj=[]
6+
for i in files:
7+
t=i.split('/')
8+
if(t[-1] not in langs):
9+
langs.append(t[-1])
10+
projs.append([])
11+
if(t[-3] not in lproj):lproj.append(t[-3])
12+
projs[langs.index(t[-1])].append(t[-3])
13+
for i in range(len(langs)):
14+
print('Completed in '+langs[i]+':')
15+
for j in projs[i]:print(j)
16+
print
17+
print('-'*100)
18+
print
19+
for i in range(len(langs)):
20+
print('Not completed in '+langs[i]+':')
21+
for j in lproj:
22+
if(j not in projs[i]):print(j)
23+
print

0 commit comments

Comments
 (0)