Skip to content

Commit f722c7e

Browse files
committed
Codility lesson implementation.
1 parent c3652ef commit f722c7e

File tree

8 files changed

+168
-0
lines changed

8 files changed

+168
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Theory Implementation
2+
3+
Euclidean algorithm
4+
5+
It solves the problem of computing the greatest common divisor (gcd) of two positive
6+
integers
7+
8+
lcm(a, b) = a·b / gcd(a,b)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package solution
2+
3+
// Returns greatest common divisor of a and b
4+
func gcd(a, b, res int) int {
5+
6+
if a == b {
7+
return res * a
8+
9+
} else if (a%2 == 0) && (b%2 == 0) {
10+
return gcd(a/2, b/2, 2*res)
11+
12+
} else if a%2 == 0 {
13+
return gcd(a/2, b, res)
14+
15+
} else if b%2 == 0 {
16+
return gcd(a, b/2, res)
17+
18+
} else if a > b {
19+
return gcd(a-b, b, res)
20+
21+
} else {
22+
return gcd(a, b-a, res)
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestSeieve(t *testing.T) {
9+
for k, test := range gcdData[:] {
10+
fmt.Printf("k %v | test %v\n", k, test.description)
11+
12+
if got := gcd(test.a, test.b, 1); got != test.want {
13+
t.Fatalf("FAIL: %s \n n: %#v got %v : want %v\n",
14+
test.description, test.a, got, test.want)
15+
}
16+
17+
// t.Logf("SUCCESS: %s", test.description)
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package solution
2+
3+
var gcdData = []struct {
4+
description string
5+
a int
6+
b int
7+
want int
8+
err string
9+
}{
10+
{
11+
description: "GCD of 2 and 6",
12+
a: 2,
13+
b: 6,
14+
want: 2,
15+
err: "",
16+
},
17+
{
18+
description: "GCD of 2 and 4",
19+
a: 2,
20+
b: 4,
21+
want: 2,
22+
err: "",
23+
},
24+
25+
{
26+
description: "GCD of 1 and 5",
27+
a: 5,
28+
b: 1,
29+
want: 1,
30+
err: "",
31+
},
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Theory Implementation
2+
3+
Euclidean algorithm
4+
5+
It solves the problem of computing the greatest common divisor (gcd) of two positive
6+
integers
7+
8+
lcm(a, b) = a·b / gcd(a,b)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package solution
2+
3+
// Returns greatest common divisor of a and b
4+
func gcd(a, b, res int) int {
5+
6+
if a == b {
7+
return res * a
8+
9+
} else if (a%2 == 0) && (b%2 == 0) {
10+
return gcd(a/2, b/2, 2*res)
11+
12+
} else if a%2 == 0 {
13+
return gcd(a/2, b, res)
14+
15+
} else if b%2 == 0 {
16+
return gcd(a, b/2, res)
17+
18+
} else if a > b {
19+
return gcd(a-b, b, res)
20+
21+
} else {
22+
return gcd(a, b-a, res)
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestSeieve(t *testing.T) {
9+
for k, test := range gcdData[:] {
10+
fmt.Printf("k %v | test %v\n", k, test.description)
11+
12+
if got := gcd(test.a, test.b, 1); got != test.want {
13+
t.Fatalf("FAIL: %s \n n: %#v got %v : want %v\n",
14+
test.description, test.a, got, test.want)
15+
}
16+
17+
// t.Logf("SUCCESS: %s", test.description)
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package solution
2+
3+
var gcdData = []struct {
4+
description string
5+
a int
6+
b int
7+
want int
8+
err string
9+
}{
10+
{
11+
description: "GCD of 2 and 6",
12+
a: 2,
13+
b: 6,
14+
want: 2,
15+
err: "",
16+
},
17+
{
18+
description: "GCD of 2 and 4",
19+
a: 2,
20+
b: 4,
21+
want: 2,
22+
err: "",
23+
},
24+
25+
{
26+
description: "GCD of 1 and 5",
27+
a: 5,
28+
b: 1,
29+
want: 1,
30+
err: "",
31+
},
32+
}

0 commit comments

Comments
 (0)