Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
37
Task/Exponentiation-order/Swift/exponentiation-order.swift
Normal file
37
Task/Exponentiation-order/Swift/exponentiation-order.swift
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
precedencegroup ExponentiationPrecedence {
|
||||
associativity: left
|
||||
higherThan: MultiplicationPrecedence
|
||||
}
|
||||
|
||||
infix operator ** : ExponentiationPrecedence
|
||||
|
||||
@inlinable
|
||||
public func ** <T: BinaryInteger>(lhs: T, rhs: T) -> T {
|
||||
guard lhs != 0 else {
|
||||
return 1
|
||||
}
|
||||
|
||||
var x = lhs
|
||||
var n = rhs
|
||||
var y = T(1)
|
||||
|
||||
while n > 1 {
|
||||
switch n & 1 {
|
||||
case 0:
|
||||
n /= 2
|
||||
case 1:
|
||||
y *= x
|
||||
n = (n - 1) / 2
|
||||
case _:
|
||||
fatalError()
|
||||
}
|
||||
|
||||
x *= x
|
||||
}
|
||||
|
||||
return x * y
|
||||
}
|
||||
|
||||
print(5 ** 3 ** 2)
|
||||
print((5 ** 3) ** 2)
|
||||
print(5 ** (3 ** 2))
|
||||
Loading…
Add table
Add a link
Reference in a new issue