Last active
February 21, 2021 15:20
-
-
Save koher/05027266a8681aaa062d81f1447cd39d to your computer and use it in GitHub Desktop.
a^iの7進法での1の位の周期を調べる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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