Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/Modular-inverse/Swift/modular-inverse.swift
Normal file
20
Task/Modular-inverse/Swift/modular-inverse.swift
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
extension BinaryInteger {
|
||||
@inlinable
|
||||
public func modInv(_ mod: Self) -> Self {
|
||||
var (m, n) = (mod, self)
|
||||
var (x, y) = (Self(0), Self(1))
|
||||
|
||||
while n != 0 {
|
||||
(x, y) = (y, x - (m / n) * y)
|
||||
(m, n) = (n, m % n)
|
||||
}
|
||||
|
||||
while x < 0 {
|
||||
x += mod
|
||||
}
|
||||
|
||||
return x
|
||||
}
|
||||
}
|
||||
|
||||
print(42.modInv(2017))
|
||||
Loading…
Add table
Add a link
Reference in a new issue