1

Im trying to make a program where you give to BigIntegers, finds their power and returns the last digit. But im having troubles passing the bigInteger into the pow method.Is there any way ?

Note: I dont want to change the bigInteger to an int

code:

import java.math.BigInteger;


fun main(args: Array<String>) {
    println(lastDigit(BigInteger("1606938044258990275541962092341162602522202993782792835301376"),BigInteger("2037035976334486086268445688409378161051468393665936250636140449354381299763336706183397376")))
}

fun lastDigit(base: BigInteger, exponent: BigInteger): Int {
    
    var allDigits : String = base.pow(exponent).toString()
    
    return allDigits[allDigits.count()-1].toInt()
}

output:

Type mismatch: inferred type is BigInteger but Int was expected

1 Answers1

1

If the BigInteger exponent is small enough, you can convert it to Int first using BigInteger.intValue().

However, if you do need to use actual BigInteger-range exponents, you may have to think about what your computer can actually hold in memory. Please have a look at this existing question which I guess is a duplicate of yours (it's in Java but it's about the same API).

Joffrey
  • 32,348
  • 6
  • 68
  • 100