mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-30 15:08:47 +01:00
26: reciprocal cycles
This commit is contained in:
@@ -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]
|
||||
}
|
||||
Reference in New Issue
Block a user