Zhelenskiy
05/12/2021, 8:51 AMpublic fun <T> Ring<T>.pow(base: T, exponent: ULong): T = when {
this == zero && exponent > 0UL -> zero
this == one -> base
this == -one -> powWithoutOptimization(base, exponent % 2UL)
else -> powWithoutOptimization(base, exponent)
}
private fun <T> Ring<T>.powWithoutOptimization(base: T, exponent: ULong): T = when (exponent) {
0UL -> one
1UL -> base
else -> {
val pre = powWithoutOptimization(base, exponent shr 1).let { it * it }
if (exponent and 1UL == 0UL) pre else pre * base
}
}
I also invented function for the big integer to make it easier to access power:
public fun pow(other: ULong): BigInt = BigIntField { pow(this@BigInt, other) }
altavir
05/12/2021, 9:38 AMZhelenskiy
05/12/2021, 9:44 AMfun <T> Ring<T>.pow(base: T, exponent: UInt) = pow(base, exponent.toULong())
altavir
05/12/2021, 7:04 PMZhelenskiy
05/12/2021, 10:01 PMkotlin.Experimental
is not applicable to target 'top level' function' which pow
actually is.altavir
05/13/2021, 5:54 AMZhelenskiy
05/13/2021, 10:30 AMaltavir
05/13/2021, 10:42 AMZhelenskiy
05/13/2021, 10:43 AMaltavir
05/13/2021, 4:59 PMZhelenskiy
05/13/2021, 9:42 PMaltavir
05/14/2021, 6:13 AMZhelenskiy
05/14/2021, 7:01 AMField
can accept negative values. That was even in previous implementation. And there is a check that you still left in code. I haven't checked, but that is probably not compilable now.altavir
05/14/2021, 7:13 AMZhelenskiy
05/14/2021, 7:18 AMaltavir
05/14/2021, 7:19 AMdev
Zhelenskiy
05/14/2021, 7:20 AMaltavir
05/14/2021, 7:21 AMZhelenskiy
05/14/2021, 7:27 AMaltavir
05/14/2021, 7:28 AMZhelenskiy
05/14/2021, 7:29 AMaltavir
05/14/2021, 7:30 AMZhelenskiy
05/14/2021, 7:31 AMaltavir
05/14/2021, 7:33 AMZhelenskiy
05/14/2021, 7:35 AMaltavir
05/14/2021, 7:48 AMZhelenskiy
05/14/2021, 7:50 AM