24 lines
538 B
Go
24 lines
538 B
Go
package primes
|
|
|
|
// DecimalPeriod tries to find the decimal period (repetition) of the decimals of 1/n. Panics if n is not a prime number.
|
|
func DecimalPeriod(n int) int {
|
|
return DecimalPeriod64(int64(n))
|
|
}
|
|
|
|
// DecimalPeriod64 tries to find the decimal period (repetition) of the decimals of 1/n. Panics if n is not a prime number.
|
|
func DecimalPeriod64(n int64) int {
|
|
if !primes.IsPrime(n) {
|
|
panic("not a prime")
|
|
}
|
|
if n <= 5 {
|
|
return 1
|
|
}
|
|
length := 1
|
|
a := int64(1)
|
|
for a > 0 {
|
|
a = (10*a + 1) % n
|
|
length++
|
|
}
|
|
return length
|
|
}
|