mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 19:54:43 +01:00
33: digit cancelling fractions
This commit is contained in:
34
33_digit_cancelling_fractions/main.go
Normal file
34
33_digit_cancelling_fractions/main.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
solution()
|
||||||
|
}
|
||||||
|
|
||||||
|
func solution() {
|
||||||
|
dp := 1
|
||||||
|
np := 1
|
||||||
|
for c := 1; c <= 9; c++ {
|
||||||
|
for d := 1; d < c; d++ {
|
||||||
|
for n := 1; n < d; n++ {
|
||||||
|
if (n*10+c)*d == (c*10+d)*n {
|
||||||
|
fmt.Println("d:", d, "n:", n)
|
||||||
|
dp *= d
|
||||||
|
np *= n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("np:", np, "dp:", dp)
|
||||||
|
fmt.Println("num:", dp/findGCD(np, dp))
|
||||||
|
}
|
||||||
|
|
||||||
|
func findGCD(a, b int) int {
|
||||||
|
for a > 0 {
|
||||||
|
t := a
|
||||||
|
a = b % a
|
||||||
|
b = t
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
43
33_digit_cancelling_fractions/main_test.go
Normal file
43
33_digit_cancelling_fractions/main_test.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test_findGCD(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
a int
|
||||||
|
b int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"4, 8",
|
||||||
|
args{4, 8},
|
||||||
|
4,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"12, 48",
|
||||||
|
args{12, 48},
|
||||||
|
12,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"12, 48",
|
||||||
|
args{12, 48},
|
||||||
|
12,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"319, 377",
|
||||||
|
args{319, 377},
|
||||||
|
29,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := findGCD(tt.args.a, tt.args.b); got != tt.want {
|
||||||
|
t.Errorf("findGCD() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user