Skip to content

Instantly share code, notes, and snippets.

@koher
Last active February 21, 2021 15:20
Show Gist options
  • Save koher/05027266a8681aaa062d81f1447cd39d to your computer and use it in GitHub Desktop.
Save koher/05027266a8681aaa062d81f1447cd39d to your computer and use it in GitHub Desktop.
a^iの7進法での1の位の周期を調べる
func pow<Integer>(_ a: Integer, _ b: Integer, modulus: Integer?) -> Integer where Integer: BinaryInteger {
var result: Integer = 1
var a = a
var b = b
if let modulus = modulus {
while true {
if b & 0x1 != 0 {
result = (result * a) % modulus
}
b >>= 1
guard b > 0 else { break }
a = (a * a) % modulus
}
} else {
while true {
if b & 0x1 != 0 {
result *= a
}
b >>= 1
guard b > 0 else { break }
a *= a
}
}
return result
}
var periods: Set<Int> = []
for a in 1 ... 10000 {
var nn: Set<Int> = []
for i in 1 ... 1000 {
nn.insert(pow(a, i, modulus: 7))
}
print(a, nn.count)
periods.insert(nn.count)
}
print(periods.sorted())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment