Skip to content

Commit 42ab99e

Browse files
committed
Merge branch 'master' of github.com:jiegillet/algorithm-archive
2 parents 0d922cb + 93e6702 commit 42ab99e

File tree

27 files changed

+576
-181
lines changed

27 files changed

+576
-181
lines changed

CONTRIBUTORS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ Hitesh C
88
Maxime Dherbécourt
99
Jess 3Jane
1010
Pen Pal
11+
Chinmaya Mahesh
12+
Unlambder
13+
Kjetil Johannessen
14+
CDsigma

book.json

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,8 @@
3636
"name": "C"
3737
},
3838
{
39-
"lang": "py2",
40-
"name": "Python 2"
41-
},
42-
{
43-
"lang": "py3",
44-
"name": "Python 3"
39+
"lang": "py",
40+
"name": "Python"
4541
},
4642
{
4743
"lang": "js",
@@ -82,6 +78,22 @@
8278
{
8379
"lang": "d",
8480
"name": "D"
81+
},
82+
{
83+
"lang": "go",
84+
"name": "Go"
85+
},
86+
{
87+
"lang": "swift",
88+
"name": "Swift"
89+
},
90+
{
91+
"lang": "racket",
92+
"name": "Racket"
93+
},
94+
{
95+
"lang": "m",
96+
"name": "Matlab"
8597
}
8698

8799
],
File renamed without changes.

chapters/FFT/cooley_tukey.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ For some reason, though, putting code to this transformation really helped me fi
7777
[import:2-11, lang:"julia"](code/julia/fft.jl)
7878
{% sample lang="hs" %}
7979
[import:2-11, lang:"julia"](code/julia/fft.jl)
80-
{% sample lang="py2" %}
80+
{% sample lang="py" %}
8181
[import:2-11, lang:"julia"](code/julia/fft.jl)
8282
{% sample lang="scratch" %}
8383
[import:2-11, lang:"julia"](code/julia/fft.jl)
@@ -122,8 +122,8 @@ In the end, the code looks like:
122122
[import:27-57, lang:"c_cpp"](code/c++/fft.cpp)
123123
{% sample lang="hs" %}
124124
[import:6-19, lang:"haskell"](code/hs/fft.hs)
125-
{% sample lang="py2" %}
126-
[import:5-16, lang:"python"](code/python2/fft.py)
125+
{% sample lang="py" %}
126+
[import:5-16, lang:"python"](code/python/fft.py)
127127
{% sample lang="scratch" %}
128128
[import:14-31, lang:"julia"](code/julia/fft.jl)
129129
{% endmethod %}
@@ -230,9 +230,9 @@ Note: I implemented this in Julia because the code seems more straightforward in
230230
{% sample lang="hs" %}
231231
### Haskell
232232
[import, lang:"haskell"](code/hs/fft.hs)
233-
{% sample lang="py2" %}
233+
{% sample lang="py" %}
234234
### Python
235-
[import, lang:"python"](code/python2/fft.py)
235+
[import, lang:"python"](code/python/fft.py)
236236
{% sample lang="scratch" %}
237237
### Scratch
238238
Some rather impressive scratch code was submitted by Jie and can be found here: https://scratch.mit.edu/projects/37759604/#editor
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Submitted by Chinmaya Mahesh (chin123)
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func abs(a int) int {
8+
if a < 0 {
9+
a = -a
10+
}
11+
return a
12+
}
13+
14+
func euclidMod(a, b int) int {
15+
a = abs(a)
16+
b = abs(b)
17+
18+
for b != 0 {
19+
a, b = b, a%b
20+
}
21+
22+
return a
23+
}
24+
25+
func euclidSub(a, b int) int {
26+
a = abs(a)
27+
b = abs(b)
28+
29+
for a != b {
30+
if a > b {
31+
a -= b
32+
} else {
33+
b -= a
34+
}
35+
}
36+
37+
return a
38+
}
39+
40+
func main() {
41+
check1 := euclidMod(64*67, 64*81)
42+
check2 := euclidSub(128*12, 128*77)
43+
44+
fmt.Println(check1)
45+
fmt.Println(check2)
46+
}

chapters/euclidean_algorithm/code/python2/euclidean_example.py renamed to chapters/euclidean_algorithm/code/python/euclidean_example.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ def euclid_mod(a, b):
22

33
a = abs(a)
44
b = abs(b)
5-
temp = 0
65

76
while b > 0:
8-
temp = b
9-
b = a % b
10-
a = temp
7+
a, b = b, a % b
118

129
return a
1310

@@ -24,6 +21,8 @@ def euclid_sub(a, b):
2421

2522
return a
2623

27-
print euclid_mod(64 * 67, 64 * 81)
28-
print euclid_sub(128 * 12, 128 * 77)
24+
def main():
25+
print(euclid_mod(64 * 67, 64 * 81))
26+
print(euclid_sub(128 * 12, 128 * 77))
2927

28+
main()

chapters/euclidean_algorithm/euclidean.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
1515
[import:20-33, lang="c_cpp"](code/c++/euclidean.cpp)
1616
{% sample lang="js" %}
1717
[import:15-29, lang="javascript"](code/javascript/euclidean_example.js)
18-
{% sample lang="py2" %}
19-
[import:14-25, lang="python"](code/python2/euclidean_example.py)
18+
{% sample lang="py" %}
19+
[import:11-22, lang="python"](code/python/euclidean_example.py)
2020
{% sample lang="haskell" %}
2121
[import:3-11, lang="haskell"](code/haskell/euclidean_example.hs)
2222
{% sample lang="rs" %}
@@ -25,6 +25,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
2525
[import:9-17, lang="ocaml"](code/ocaml/euclidean_example.ml)
2626
{% sample lang="java" %}
2727
[import:9-22, lang="java"](code/java/euclidean_example.java)
28+
{% sample lang="go" %}
29+
[import:25-38, lang="golang"](code/go/euclidean.go)
2830
{% endmethod %}
2931

3032
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:
@@ -46,8 +48,8 @@ Modern implementations, though, often use the modulus operator (%) like so
4648
[import:7-17, lang="c_cpp"](code/c++/euclidean.cpp)
4749
{% sample lang="js" %}
4850
[import:1-13, lang="javascript"](code/javascript/euclidean_example.js)
49-
{% sample lang="py2" %}
50-
[import:1-12, lang="python"](code/python2/euclidean_example.py)
51+
{% sample lang="py" %}
52+
[import:1-9, lang="python"](code/python/euclidean_example.py)
5153
{% sample lang="haskell" %}
5254
[import:13-24, lang="haskell"](code/haskell/euclidean_example.hs)
5355
{% sample lang="rs" %}
@@ -56,6 +58,8 @@ Modern implementations, though, often use the modulus operator (%) like so
5658
[import:3-7, lang="ocaml"](code/ocaml/euclidean_example.ml)
5759
{% sample lang="java" %}
5860
[import:24-35, lang="java"](code/java/euclidean_example.java)
61+
{% sample lang="go" %}
62+
[import:14-23, lang="golang"](code/go/euclidean.go)
5963
{% endmethod %}
6064

6165
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:
@@ -87,9 +91,9 @@ Program.cs
8791
{% sample lang="js" %}
8892
### JavaScript
8993
[import, lang="javascript"](code/javascript/euclidean_example.js)
90-
{% sample lang="py2" %}
94+
{% sample lang="py" %}
9195
### Python
92-
[import, lang="python"](code/python2/euclidean_example.py)
96+
[import, lang="python"](code/python/euclidean_example.py)
9397
{% sample lang="haskell" %}
9498
### Haskell
9599
[import, lang="haskell"](code/haskell/euclidean_example.hs)
@@ -102,6 +106,9 @@ Program.cs
102106
{% sample lang="java" %}
103107
### Java
104108
[import, lang="java"](code/java/euclidean_example.java)
109+
{% sample lang="go" %}
110+
### Go
111+
[import, lang="golang"](code/go/euclidean.go)
105112
{% endmethod %}
106113

107114

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
///Returns true if a point (x, y) is in the circle with radius r
2+
bool inCircle(real x, real y)
3+
{
4+
return x ^^ 2 + y ^^ 2 < 1.0;
5+
}
6+
7+
///Calculate pi using monte carlo
8+
real monteCarloPI(ulong n)
9+
{
10+
import std.algorithm : count;
11+
import std.random : uniform01;
12+
import std.range : generate, take;
13+
import std.typecons : tuple;
14+
15+
auto piCount = generate(() => tuple!("x", "y")(uniform01, uniform01))
16+
.take(n)
17+
.count!(a => inCircle(a.x, a.y));
18+
return piCount * 4.0 / n;
19+
}
20+
21+
void main()
22+
{
23+
import std.math : abs, PI;
24+
import std.stdio : writeln;
25+
26+
auto p = monteCarloPI(100_000);
27+
writeln("Estimated pi: ", p);
28+
writeln("Percent error: ", abs(p - PI) * 100 / PI);
29+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Submitted by Chinmaya Mahesh (chin123)
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"math"
8+
"math/rand"
9+
"time"
10+
)
11+
12+
func inCircle(x, y float64) bool {
13+
return x*x+y*y < 1.0 // the radius of an unit circle is 1.0
14+
}
15+
16+
func monteCarlo(samples int) {
17+
count := 0
18+
s := rand.NewSource(time.Now().UnixNano())
19+
r := rand.New(s)
20+
21+
for i := 0; i < samples; i++ {
22+
x, y := r.Float64(), r.Float64()
23+
24+
if inCircle(x, y) {
25+
count += 1
26+
}
27+
}
28+
29+
estimate := 4.0 * float64(count) / float64(samples)
30+
31+
fmt.Println("The estimate of pi is", estimate)
32+
fmt.Printf("Which has an error of %f%%\n", 100*math.Abs(math.Pi-estimate)/math.Pi)
33+
}
34+
35+
func main() {
36+
monteCarlo(10000000)
37+
}

chapters/monte_carlo/monte_carlo.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ each point is tested to see whether it's in the circle or not:
4545
[import:7-7, lang:"haskell"](code/haskell/monteCarlo.hs)
4646
{% sample lang="rs" %}
4747
[import:7-9, lang:"rust"](code/rust/monte_carlo.rs)
48+
{% sample lang="d" %}
49+
[import:2-5, lang:"d"](code/d/monte_carlo.d)
50+
{% sample lang="go" %}
51+
[import:12-14, lang:"golang"](code/go/monteCarlo.go)
4852
{% endmethod %}
4953

5054
If it's in the circle, we increase an internal count by one, and in the end,
@@ -88,6 +92,12 @@ Feel free to submit your version via pull request, and thanks for reading!
8892
{%sample lang="rs" %}
8993
### Rust
9094
[import, lang:"rust"](code/rust/monte_carlo.rs)
95+
{%sample lang="d" %}
96+
### D
97+
[import, lang:"d"](code/d/monte_carlo.d)
98+
{%sample lang="go" %}
99+
### Go
100+
[import, lang:"golang"](code/go/monteCarlo.go)
91101
{% endmethod %}
92102

93103

chapters/physics_solvers/verlet/code/python2/verlet.py renamed to chapters/physics_solvers/verlet/code/python/verlet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ def main():
5252
sim = Verlet(Ball(pos = 5.0, acc = -10))
5353
sim.run()
5454

55-
print "Verlet:", sim.time
55+
print("Verlet:", sim.time)
5656

5757
sim = Stormer_Verlet(Ball(pos = 5.0, acc = -10))
5858
sim.run()
5959

60-
print "Stormer Verlet:", sim.time
60+
print("Stormer Verlet:", sim.time)
6161

6262
sim = Velocity_Verlet(Ball(pos = 5.0, acc = -10))
6363
sim.run()
6464

65-
print "Velocity Verlet:", sim.time
65+
print("Velocity Verlet:", sim.time)
6666

6767

6868
if __name__ == "__main__":

chapters/physics_solvers/verlet/verlet.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ Here is what it looks like in code:
3838
[import:3-16, lang:"c_cpp"](code/c/verlet.c)
3939
{% sample lang="java" %}
4040
[import:2-18, lang:"java"](code/java/verlet.java)
41-
{% sample lang="py2" %}
42-
[import:28-33, lang:"python"](code/python2/verlet.py)
41+
{% sample lang="py" %}
42+
[import:28-33, lang:"python"](code/python/verlet.py)
4343
{% sample lang="hs" %}
4444
Unfortunately, this has not yet been implemented in haskell, so here's Julia code:
4545
[import:1-13, lang:"julia"](code/julia/verlet.jl)
@@ -84,8 +84,8 @@ Here's what it looks like in code:
8484
[import:18-33, lang:"c_cpp"](code/c/verlet.c)
8585
{% sample lang="java" %}
8686
[import:21-40, lang:"java"](code/java/verlet.java)
87-
{% sample lang="py2" %}
88-
[import:35-42, lang:"python"](code/python2/verlet.py)
87+
{% sample lang="py" %}
88+
[import:35-42, lang:"python"](code/python/verlet.py)
8989
{% sample lang="hs" %}
9090
Unfortunately, this has not yet been implemented in scratch, so here's Julia code:
9191
[import:15-31, lang:"julia"](code/julia/verlet.jl)
@@ -141,8 +141,8 @@ Here is the velocity Verlet method in code:
141141
[import:35-46, lang:"c_cpp"](code/c/verlet.c)
142142
{% sample lang="java" %}
143143
[import:43-57, lang:"java"](code/java/verlet.java)
144-
{% sample lang="py2" %}
145-
[import:44-48, lang:"python"](code/python2/verlet.py)
144+
{% sample lang="py" %}
145+
[import:44-48, lang:"python"](code/python/verlet.py)
146146
{% sample lang="hs" %}
147147
Unfortunately, this has not yet been implemented in haskell, so here's Julia code:
148148
[import:33-45, lang:"julia"](code/julia/verlet.jl)
@@ -181,9 +181,9 @@ Both of these methods work simply by iterating timestep-by-timestep and can be w
181181
{% sample lang="java" %}
182182
### Java
183183
[import, lang:"java"](code/java/verlet.java)
184-
{% sample lang="py2" %}
184+
{% sample lang="py" %}
185185
### Python
186-
[import, lang:"python"](code/python2/verlet.py)
186+
[import, lang:"python"](code/python/verlet.py)
187187
{% sample lang="hs" %}
188188
### Haskell
189189
[import, lang:"haskell"](code/haskell/verlet.hs)

chapters/sorting_searching/bogo/bogo_sort.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ In code, it looks something like this:
2525
[import:2-17, lang:"java"](code/java/bogo.java)
2626
{% sample lang="js" %}
2727
[import:1-16, lang:"javascript"](code/js/bogo.js)
28+
{% sample lang="py" %}
29+
[import:4-12, lang:"python"](code/python/bogo.py)
2830
{% sample lang="hs" %}
2931
[import, lang:"haskell"](code/haskell/bogoSort.hs)
32+
{% sample lang="m" %}
33+
[import, lang:"matlab"](code/matlab/bogosort.m)
3034
{% sample lang="cpp" %}
3135
[import, lang:"c_cpp"](code/c++/bogosort.cpp)
3236
{% sample lang="rs" %}
3337
[import, lang:"rust"](code/rust/bogosort.rs)
38+
{% sample lang="swift" %}
39+
[import, lang:"swift"](code/swift/bogosort.swift)
3440
{% endmethod %}
3541

3642
That's it.

0 commit comments

Comments
 (0)