Karatsuba added, 2 bugs are fixed #328

Merged
zhelenskiy merged 15 commits from dev into dev 2021-05-14 09:12:26 +03:00
3 changed files with 10 additions and 1 deletions
Showing only changes of commit 8443fac4ae - Show all commits

View File

@ -259,6 +259,8 @@ public fun <T> Ring<T>.pow(base: T, exponent: ULong): T = when {
else -> powWithoutOptimization(base, exponent) else -> powWithoutOptimization(base, exponent)
} }
public fun <T> Ring<T>.pow(base: T, exponent: UInt): T = pow(base, exponent.toULong())
private fun <T> Ring<T>.powWithoutOptimization(base: T, exponent: ULong): T = when (exponent) { private fun <T> Ring<T>.powWithoutOptimization(base: T, exponent: ULong): T = when (exponent) {
0UL -> one 0UL -> one
1UL -> base 1UL -> base

View File

@ -99,6 +99,7 @@ public class BigInt internal constructor(
} }
public fun pow(other: ULong): BigInt = BigIntField { pow(this@BigInt, other) } public fun pow(other: ULong): BigInt = BigIntField { pow(this@BigInt, other) }
public fun pow(other: UInt): BigInt = BigIntField { pow(this@BigInt, other) }
public operator fun times(other: Int): BigInt = when { public operator fun times(other: Int): BigInt = when {
other > 0 -> this * kotlin.math.abs(other).toUInt() other > 0 -> this * kotlin.math.abs(other).toUInt()

View File

@ -26,12 +26,18 @@ internal class BigIntAlgebraTest {
fun testKBigIntegerRingPow() { fun testKBigIntegerRingPow() {
BigIntField { BigIntField {
for (num in -5..5) for (num in -5..5)
for (exponent in 0UL..10UL) for (exponent in 0U..10U) {
assertEquals(
num.toDouble().pow(exponent.toInt()).toLong().toBigInt(),
num.toBigInt().pow(exponent.toULong()),
"$num ^ $exponent"
)
assertEquals( assertEquals(
num.toDouble().pow(exponent.toInt()).toLong().toBigInt(), num.toDouble().pow(exponent.toInt()).toLong().toBigInt(),
num.toBigInt().pow(exponent), num.toBigInt().pow(exponent),
"$num ^ $exponent" "$num ^ $exponent"
) )
}
} }
} }