diff --git a/26_reciprocal_cycles/main.go b/26_reciprocal_cycles/main.go new file mode 100644 index 0000000..807a329 --- /dev/null +++ b/26_reciprocal_cycles/main.go @@ -0,0 +1,34 @@ +package main + +import "fmt" + +func main() { + max := 0 + d := 1 + for d < 1000 { + n := findCycleLengthOfOneNth(d) + if n > max { + max = d + } + d++ + } + fmt.Println("max:", max) +} + +func findCycleLengthOfOneNth(n int) int { + m := map[int]int{} + a := 1 + t := 0 + for { + if _, ok := m[a]; ok { + break + } + m[a] = t + a = a % n * 10 + if a == 0 { + return t + } + t++ + } + return t - m[a] +} diff --git a/26_reciprocal_cycles/main_test.go b/26_reciprocal_cycles/main_test.go new file mode 100644 index 0000000..62accae --- /dev/null +++ b/26_reciprocal_cycles/main_test.go @@ -0,0 +1,55 @@ +package main + +import ( + "reflect" + "testing" +) + +func Test_findDecimalOfOneNth(t *testing.T) { + type args struct { + n int + } + tt := []struct { + name string + args args + want int + }{ + { + "2", + args{2}, + 1, + }, + { + "4", + args{4}, + 2, + }, + { + "6", + args{6}, + 1, + }, + { + "7", + args{7}, + 6, + }, + { + "10", + args{10}, + 1, + }, + { + "100", + args{100}, + 2, + }, + } + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + if got := findCycleLengthOfOneNth(tc.args.n); !reflect.DeepEqual(got, tc.want) { + t.Errorf("findCycleLengthOfOneNth() = %v, want %v", got, tc.want) + } + }) + } +}