From b7262611538d4003c39daffdb62d7d83383129c6 Mon Sep 17 00:00:00 2001 From: Ariana <1729ariana@gmail.com> Date: Mon, 1 Oct 2018 18:14:39 +0800 Subject: [PATCH 1/7] Did euclidean algorithm in brainfuck why am i doing this mdr --- .../code/brainfuck/euclidean_algorithm.bf | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 contents/euclidean_algorithm/code/brainfuck/euclidean_algorithm.bf diff --git a/contents/euclidean_algorithm/code/brainfuck/euclidean_algorithm.bf b/contents/euclidean_algorithm/code/brainfuck/euclidean_algorithm.bf new file mode 100644 index 000000000..2a4604fb9 --- /dev/null +++ b/contents/euclidean_algorithm/code/brainfuck/euclidean_algorithm.bf @@ -0,0 +1,23 @@ +>,>, scan +[ while b!=0 +0 a (b) 0 0 0 + +<[ + >->+<[>] + >[<+>-]< + <[<]>- +] +so basically this is the mod algo in brainfuck, it slowly shifts cell 2 to cell 3, while subtracting 1 from cell 1 +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 +0 (0) b_a%b a%b 0 0 + +>[-<+>] +0 b_a%b (0) a%b 0 0 + +>[-<+<+>>]< +0 b (a%b) 0 0 0 + +] +0 GCD(a b) 0 0 0 0 + +<. From 9e5e0095359fb1aaeebfe20ec85371e3e77ed092 Mon Sep 17 00:00:00 2001 From: Ariana <1729ariana@gmail.com> Date: Tue, 2 Oct 2018 12:56:59 +0800 Subject: [PATCH 2/7] Removed comments --- .../code/brainfuck/euclidean_algorithm.bf | 24 +------------------ 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/contents/euclidean_algorithm/code/brainfuck/euclidean_algorithm.bf b/contents/euclidean_algorithm/code/brainfuck/euclidean_algorithm.bf index 2a4604fb9..6d6395fb1 100644 --- a/contents/euclidean_algorithm/code/brainfuck/euclidean_algorithm.bf +++ b/contents/euclidean_algorithm/code/brainfuck/euclidean_algorithm.bf @@ -1,23 +1 @@ ->,>, scan -[ while b!=0 -0 a (b) 0 0 0 - -<[ - >->+<[>] - >[<+>-]< - <[<]>- -] -so basically this is the mod algo in brainfuck, it slowly shifts cell 2 to cell 3, while subtracting 1 from cell 1 -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 -0 (0) b_a%b a%b 0 0 - ->[-<+>] -0 b_a%b (0) a%b 0 0 - ->[-<+<+>>]< -0 b (a%b) 0 0 0 - -] -0 GCD(a b) 0 0 0 0 - -<. +>,>,[<[>->+<[>]>[<+>-]<<[<]>-]>[-<+>]>[-<+<+>>]<]<. From c709f9196dc67d42a55af79e5a0d2ad833332658 Mon Sep 17 00:00:00 2001 From: Ariana <1729ariana@gmail.com> Date: Tue, 2 Oct 2018 13:12:48 +0800 Subject: [PATCH 3/7] Added explanation in a different file --- .../code/brainfuck/explanation.md | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 contents/euclidean_algorithm/code/brainfuck/explanation.md diff --git a/contents/euclidean_algorithm/code/brainfuck/explanation.md b/contents/euclidean_algorithm/code/brainfuck/explanation.md new file mode 100644 index 000000000..537c3c35b --- /dev/null +++ b/contents/euclidean_algorithm/code/brainfuck/explanation.md @@ -0,0 +1,51 @@ +Pseudocode: +``` +word a,b; +scan a,b; +while(b!=0){ + a,b=b,a%b; +} +return a; +``` + +`scan a,b`: `>,>,` + +State: `0 a >b 0 0 0` + +`while(b!=0)`: `[` + +`a,b,0=0,b-a%b,a%b`: +``` +<[ + >->+<[>] + >[<+>-]< + <[<]>- +] +``` + +so basically this is the modulo algorithm in brainfuck, it slowly shifts cell 2 to cell 3, while subtracting 1 from cell 1 +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 + +State: `0 >0 b-a%b a%b 0 0` + +shifting: `>[-<+>]` + +State: `0 b-a%b >0 a%b 0 0` + +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 + +adding thing: `>[-<+<+>>]<` + +State: `0 b >(a%b) 0 0 0` + +So now we have a,b=b,a%b, we continue the loop + +`]` + +After the second cell is 0, the loop terminates and we obtain the GCD + +State: `0 >GCD(a b) 0 0 0 0` + +Now we print the GCD + +print: `<.` From 3bb938a3a2025d5359146120b8c07c2567c61223 Mon Sep 17 00:00:00 2001 From: Ariana <1729ariana@gmail.com> Date: Wed, 3 Oct 2018 00:54:22 +0800 Subject: [PATCH 4/7] Added brainfuck to book.json --- book.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/book.json b/book.json index 94676c45b..6c73bd216 100644 --- a/book.json +++ b/book.json @@ -139,6 +139,10 @@ { "lang": "f90", "name": "Fortran90" + }, + { + "lang": "bf", + "name": "Brainfuck" } ] } From 398c0f2120911c894a6dada959b6a3b785dbd3eb Mon Sep 17 00:00:00 2001 From: Ariana <1729ariana@gmail.com> Date: Wed, 3 Oct 2018 00:57:31 +0800 Subject: [PATCH 5/7] Rename euclidean_algorithm.bf to euclidean_mod.bf --- .../code/brainfuck/{euclidean_algorithm.bf => euclidean_mod.bf} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename contents/euclidean_algorithm/code/brainfuck/{euclidean_algorithm.bf => euclidean_mod.bf} (100%) diff --git a/contents/euclidean_algorithm/code/brainfuck/euclidean_algorithm.bf b/contents/euclidean_algorithm/code/brainfuck/euclidean_mod.bf similarity index 100% rename from contents/euclidean_algorithm/code/brainfuck/euclidean_algorithm.bf rename to contents/euclidean_algorithm/code/brainfuck/euclidean_mod.bf From b19217a3633a046e156cf5f97449c5afeec82867 Mon Sep 17 00:00:00 2001 From: Ariana <1729ariana@gmail.com> Date: Sat, 6 Oct 2018 16:12:29 +0800 Subject: [PATCH 6/7] Added subtraction algorithm and explanation --- .gitignore | 1 + .../code/brainfuck/euclidean_sub.bf | 2 + .../code/brainfuck/explanation.md | 51 -------- .../euclidean_algorithm.md | 114 ++++++++++++++++++ 4 files changed, 117 insertions(+), 51 deletions(-) create mode 100644 contents/euclidean_algorithm/code/brainfuck/euclidean_sub.bf delete mode 100644 contents/euclidean_algorithm/code/brainfuck/explanation.md diff --git a/.gitignore b/.gitignore index ba5a4a49c..a5fb5df38 100644 --- a/.gitignore +++ b/.gitignore @@ -498,3 +498,4 @@ __pycache__/ -# Other -*.xcf +listproj.py diff --git a/contents/euclidean_algorithm/code/brainfuck/euclidean_sub.bf b/contents/euclidean_algorithm/code/brainfuck/euclidean_sub.bf new file mode 100644 index 000000000..1bdf12888 --- /dev/null +++ b/contents/euclidean_algorithm/code/brainfuck/euclidean_sub.bf @@ -0,0 +1,2 @@ +,>,>>>+[[-]<<<<[->>>+<<<]>[->>>+<<<]>>[-<+<<+>>>]>[-<+<<+>>>]<<[->->[-]+<[>-]>[->>]<<<]>[>]<[<<[-]>>[-<<+>>>+<]]<[<<[-]>>[-<<+>>>>+<<]]>>>[-]+>[-]<<[>-]>[<<<<.>>>>->>]<<] + diff --git a/contents/euclidean_algorithm/code/brainfuck/explanation.md b/contents/euclidean_algorithm/code/brainfuck/explanation.md deleted file mode 100644 index 537c3c35b..000000000 --- a/contents/euclidean_algorithm/code/brainfuck/explanation.md +++ /dev/null @@ -1,51 +0,0 @@ -Pseudocode: -``` -word a,b; -scan a,b; -while(b!=0){ - a,b=b,a%b; -} -return a; -``` - -`scan a,b`: `>,>,` - -State: `0 a >b 0 0 0` - -`while(b!=0)`: `[` - -`a,b,0=0,b-a%b,a%b`: -``` -<[ - >->+<[>] - >[<+>-]< - <[<]>- -] -``` - -so basically this is the modulo algorithm in brainfuck, it slowly shifts cell 2 to cell 3, while subtracting 1 from cell 1 -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 - -State: `0 >0 b-a%b a%b 0 0` - -shifting: `>[-<+>]` - -State: `0 b-a%b >0 a%b 0 0` - -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 - -adding thing: `>[-<+<+>>]<` - -State: `0 b >(a%b) 0 0 0` - -So now we have a,b=b,a%b, we continue the loop - -`]` - -After the second cell is 0, the loop terminates and we obtain the GCD - -State: `0 >GCD(a b) 0 0 0 0` - -Now we print the GCD - -print: `<.` diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 81860ae5f..4680b112a 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -41,6 +41,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two [import:13-24, lang="nim"](code/nim/euclid_algorithm.nim) {% sample lang="f90" %} [import:1-19, lang="Fortran"](code/fortran/euclidean.f90) +{% sample lang="bf" %} +[import:1-19, lang="Brainfuck"](code/brainfuck/euclidean_sub.bf) {% endmethod %} 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 [import:1-11, lang="nim"](code/nim/euclid_algorithm.nim) {% sample lang="f90" %} [import:21-34, lang="Fortran"](code/fortran/euclidean.f90) +{% sample lang="bf" %} +[import:1-19, lang="Brainfuck"](code/brainfuck/euclidean_mod.bf) {% endmethod %} 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 [import, lang="nim" %](code/nim/euclid_algorithm.nim) {% sample lang="f90" %} [import, lang="Fortran"](code/fortran/euclidean.f90) +{% sample lang="bf" %} +#### Subtraction varient +##### Code +[import, lang="Brainfuck"](code/brainfuck/euclidean_sub.bf) +##### Explanation +Basic plan: get |a-b|, check if 0 + +So the program does something like +``` +a (b) 0 0 1 0 0 +a b a b (0) 0 0 +if(a>b) a b a-b 0 (a-b) 0 0 +else a b 0 a-b (a-b) 0 0 +if(a-b==0)print and break +else +if(a>b) a-b b 0 0 (a-b) 0 0 +else a a-b 0 0 (a-b) 0 0 +``` + +More detail: + +`scan a,b`: `>,>,` +State: `a (b) 0 0 0 0 0` +``` +>>>+ +[ +[-]<<< +<[->>>+<<<]>[->>>+<<<]>> +``` +State: `0 0 0 (a) b 0 0` +``` +[-<+<<+>>>] +>[-<+<<+>>>] +``` +State: `a b a b (0) 0 0` +``` +<< +[->- subtracts a from b, assuming a>b +>[-]+ +<[ +>-]> +[->>]<<< if a is 0, stop +] +``` +So basically the state will either be +a_b (0) 0 0 +or +(0) a_b 0 0 +but it's hard to do when states may be different, so `>[>]` moves the pointer to cell 4 +``` +<[<<[-]>>[-<<+>>>+<]] +<[<<[-]>>[-<<+>>>>+<<]] +``` +basically cell 4 will contain the difference +``` +>>>[-]+ +>[-]<< +[>-]> +[<<<<.>>> testing if difference is 0, if so return +>->>]<< +] +``` + +#### Modulo varient +##### Code +[import, lang="Brainfuck"](code/brainfuck/euclidean_mod.bf) +##### Explanation +`scan a,b`: `>,>,` + +State: `0 a >b 0 0 0` + +`while(b!=0)`: `[` + +`a,b,0=0,b-a%b,a%b`: +``` +<[ + >->+<[>] + >[<+>-]< + <[<]>- +] +``` + +so basically this is the modulo algorithm in brainfuck, it slowly shifts cell 2 to cell 3, while subtracting 1 from cell 1 +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 + +State: `0 >0 b-a%b a%b 0 0` + +shifting: `>[-<+>]` + +State: `0 b-a%b >0 a%b 0 0` + +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 + +adding thing: `>[-<+<+>>]<` + +State: `0 b >(a%b) 0 0 0` + +So now we have a,b=b,a%b, we continue the loop + +`]` + +After the second cell is 0, the loop terminates and we obtain the GCD + +State: `0 >GCD(a b) 0 0 0 0` + +Now we print the GCD + +print: `<.` + + {% endmethod %} From 069f87119802cf19cf577f4a63c9304faf4e96e7 Mon Sep 17 00:00:00 2001 From: Ariana <1729ariana@gmail.com> Date: Sat, 6 Oct 2018 18:37:24 +0800 Subject: [PATCH 7/7] Gaussian elimination translated from C to JS --- .../code/javascript/gaussian_elimination.js | 112 ++++++++++++++++++ .../gaussian_elimination.md | 8 ++ 2 files changed, 120 insertions(+) create mode 100644 contents/gaussian_elimination/code/javascript/gaussian_elimination.js diff --git a/contents/gaussian_elimination/code/javascript/gaussian_elimination.js b/contents/gaussian_elimination/code/javascript/gaussian_elimination.js new file mode 100644 index 000000000..0b40dd18f --- /dev/null +++ b/contents/gaussian_elimination/code/javascript/gaussian_elimination.js @@ -0,0 +1,112 @@ +function gaussian_elimination(a){ + var rows = a.length + var cols = a[0].length + var row = 0; + + for (let col = 0; col < cols - 1; ++col) { + + let pivot = row; + for (let i = row + 1; i < rows; ++i) { + if (Math.abs(a[i][col]) > Math.abs(a[pivot][col])) { + pivot = i; + } + } + + if (a[pivot][col] == 0) { + console.log("The matrix is singular.\n"); + continue; + } + + if (col != pivot) { + let t=a[col]; + a[col]=a[pivot]; + a[pivot]=t; + } + + for (let i = row + 1; i < rows; ++i) { + let scale = a[i][col] / a[row][col]; + + for (let j = col + 1; j < cols; ++j) { + a[i][j] -= a[row][j] * scale; + } + + a[i][col] = 0; + } + + ++row; + } + return a; +} + +function back_substitution(a){ + var rows = a.length; + var cols = a[0].length; + var sol=new Array(rows); + + for (let i = rows - 1; i >= 0; --i) { + + let sum = 0; + for (let j = cols - 2; j > i; --j) { + sum += sol[j] * a[i][j]; + } + + sol[i] = (a[i][cols - 1] - sum) / a[i][i]; + } + return sol; +} + +function gauss_jordan(a) { + var rows = a.length; + var cols = a[0].length; + var row = 0; + + for (let col = 0; col < cols - 1; ++col) { + if (a[row][col] != 0) { + for (let i = cols - 1; i > col - 1; --i) { + a[row][i] /= a[row][col]; + } + + for (let i = 0; i < row; ++i) { + for (let j = cols - 1; j > col - 1; --j) { + a[i][j] -= a[i][col] * a[row][j]; + } + } + + ++row; + } + } +} + +var a = [[3, 2 , -4, 3 ], + [ 2, 3 , 3 , 15], + [ 5, -3, 1 , 14]]; + +gaussian_elimination(a); +console.log("Gaussian elimination:\n"); +for(let i=0;i