Skip to content

Commit 7d01395

Browse files
authored
Merge branch 'master' into master
2 parents fa0a670 + 9595329 commit 7d01395

File tree

8 files changed

+124
-0
lines changed

8 files changed

+124
-0
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Maxime Dherbécourt
99
Jess 3Jane
1010
Pen Pal
1111
Mukundan
12+
Chinmaya Mahesh

book.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@
8282
{
8383
"lang": "d",
8484
"name": "D"
85+
},
86+
{
87+
"lang": "go",
88+
"name": "Go"
8589
}
8690

8791
],
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/euclidean.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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="go"](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:
@@ -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="go"](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:
@@ -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="go"](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+
}

chapters/monte_carlo/monte_carlo.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ 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/rust/monte_carlo.d)
4850
{% endmethod %}
4951

5052
If it's in the circle, we increase an internal count by one, and in the end,
@@ -88,6 +90,9 @@ Feel free to submit your version via pull request, and thanks for reading!
8890
{%sample lang="rs" %}
8991
### Rust
9092
[import, lang:"rust"](code/rust/monte_carlo.rs)
93+
### D
94+
{%sample lang="d" %}
95+
[import, lang:"d"](code/d/monte_carlo.d)
9196
{% endmethod %}
9297

9398

chapters/sorting_searching/bubble/bubble_sort.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
2626
[import:6-19, lang:"rust"](code/rust/bubble_sort.rs)
2727
{% sample lang="d" %}
2828
[import:3-18, lang:"d"](code/d/bubble_sort.d)
29+
{% sample lang="go" %}
30+
[import:7-21, lang:"go"](code/go/bubbleSort.go)
2931
{% endmethod %}
3032

3133
... And that's it for the simplest bubble sort method.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Submitted by Chinmaya Mahesh (chin123)
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func bubbleSort(array []int) {
8+
n := len(array)
9+
for i := 0; i < n-1; i++ {
10+
swapped := false
11+
for j := 0; j < n-i-1; j++ {
12+
if array[j] > array[j+1] {
13+
array[j], array[j+1] = array[j+1], array[j]
14+
swapped = true
15+
}
16+
}
17+
if !swapped {
18+
break
19+
}
20+
}
21+
}
22+
23+
func main() {
24+
array := [10]int{1, 45, 756, 4569, 56, 3, 8, 5, -10, -4}
25+
fmt.Println("Unsorted array:", array)
26+
27+
bubbleSort(array[:])
28+
29+
fmt.Println("Sorted array:", array)
30+
}

0 commit comments

Comments
 (0)