我如何 divide Kotlin 中的 BigIntegers? (我试过 div() 和 divideAndRemainder())

How do i divide BigIntegers in kotlin? (I have tried div() and divideAndRemainder())

我正在尝试除以两个大整数值,但它似乎不起作用。我试过 BigIntegervalue1.div(BigIntegervalue2)BigIntegervalue1.divideAndRemainder(BigIntegervalue2)。当我认为我可以用 divideAndRemainder 声明余数和商时,我得到了一个 类型不匹配错误 ,这会给我 BigInteger

完整代码如下:

package converter
import kotlin.system.exitProcess
import java.math.BigInteger
import java.util.*
var alphabet = listOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z")

fun convertNumberToDecimal(numberToConvert: String, sourceBase: Int): String { // fix this formula!
  var finalValue = ""
  var elementHolder = (numberToConvert).toMutableList()
  var i = elementHolder.size - 1
  var j = 0
  var decimalNumber = 0.toString()
  while (i != -1) {
    if (elementHolder.isEmpty()) {
      break
    } else if (elementHolder[j].toString() in alphabet) {
      elementHolder[j] = (9 + alphabet.indexOf(elementHolder[j].toString())).toChar()
      decimalNumber += (elementHolder[j].toInt() * Math.pow(sourceBase.toDouble(), i.toDouble()).toInt()).toString()
      println(decimalNumber)
    } else {
      decimalNumber += (elementHolder[j].toInt() * Math.pow(sourceBase.toDouble(), i.toDouble()).toInt()).toString()
    }
    j++
    i--
  }
  return decimalNumber
}

fun convertDecimalToNewBase(numberToConvert: String, targetBase: Int): Any {
  var checker = 0
  var i = 0
  var power = targetBase
  var sumOfNumbers = 0
  var decimalNumber: Any
  if (numberToConvert[0] == '0') {
    decimalNumber = BigInteger(numberToConvert.drop(1))
  } else {
    decimalNumber = BigInteger(numberToConvert)
  }
  var listOfNumbers: MutableList<String> = mutableListOf()
  var finalValue = ""
  var denominator = BigInteger("0")
  
  while (checker < numberToConvert.toInt()) {
    checker = Math.pow(targetBase.toDouble(), i.toDouble()).toInt()
    i++
    if (checker > numberToConvert.toInt()) {
      i -= 2
      checker = Math.pow(targetBase.toDouble(), i.toDouble()).toInt()
      break
    }
  }
  
  while (i != -1) {
    denominator = (Math.pow(targetBase.toDouble(), i.toDouble())).toInt().toBigInteger()
    var (quotient, remainder) = decimalNumber.divideAndRemainder(denominator)
    if (quotient.toInt() >= 10 && quotient.toInt() <= 36 && targetBase > 10) {
      quotient = alphabet[(quotient.toInt() - 10)]
    }
    listOfNumbers.add(quotient)
    decimalNumber = remainder
    i--
  }
    
  for (each in listOfNumbers) {
    finalValue += each.toString()
  }
  for (each in alphabet) {
    if (each in finalValue) {
      return finalValue
    }
  }
  return finalValue.toBigInteger()
}

fun main() {
  var scanner = Scanner(System.`in`)
  println("Enter two numbers in format: {source base} {target base} (To quit type /exit) ")
  var choice = readLine()!!.toString()
  var splitter = listOf<String>()
  var sourceBase: Int
  var targetBase: Int
  var numberInSourceBase: Any
  var newDecimalNumber: Any
  if (choice == "/exit") {
    exitProcess(0)
  } else {
    splitter = choice.split(" ")
    sourceBase = splitter[0].toInt()
    targetBase = splitter[1].toInt()
  }
  do {
    println("Enter number in base " + sourceBase + " to convert to base " + targetBase + " (To go back type /back) ")
    numberInSourceBase = readLine()!!.toString()
    if (numberInSourceBase == "/back") {
      main()
    }
    if (sourceBase != 10) {
      newDecimalNumber = convertNumberToDecimal(numberInSourceBase.toString(), sourceBase)
      newDecimalNumber = (convertDecimalToNewBase(newDecimalNumber.toString(), targetBase)).toString()
    } else {
      newDecimalNumber = (convertDecimalToNewBase(numberInSourceBase.toString(), targetBase)).toString()
    }
    println("Conversion result: " + newDecimalNumber)
  } while (numberInSourceBase != "/back")
}

这是给我类型不匹配错误的代码行:

var (quotient, remainder) = decimalNumber.divideAndRemainder(denominator)

我复制了你的代码,但出现了不同的错误。这是

Unresolved reference: divideAndRemainder

这仅仅是因为decimalNumber没有那个方法。为什么只有 BigIntegers 分配给它时没有那个方法?仅仅因为它没有被声明为一个。

改变

var decimalNumber: Any

var decimalNumber: BigInteger

删除该错误。

现在还有其他错误提示,看你怎么解决了