I am trying to divide two Big Integer values and it does not seem to be working. I have tried BigIntegervalue1.div(BigIntegervalue2) and BigIntegervalue1.divideAndRemainder(BigIntegervalue2). I am getting a type mismatch Error when I thought I could declare remainder and quotient with divideAndRemainder which would give me BigInteger
Here is the full code:
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")
}
Here is the line of code that is giving me the type mismatch error:
var (quotient, remainder) = decimalNumber.divideAndRemainder(denominator)
CodePudding user response:
I copied your code and I got a different error. Which is
Unresolved reference: divideAndRemainder
Which is simply because decimalNumber
doesn't have that method. Why doesn't it have that method while only BigIntegers
are assigned to it? simply because it isn't declared as one.
Changing
var decimalNumber: Any
to
var decimalNumber: BigInteger
removes that error.
Now there are other errors showing, which is up to you how to solve