DEV Community

Discussion on: Challenge: Write your worst program

Collapse
 
helpermethod profile image
Oliver Weiler • Edited

Multiplication should be hard

EDIT Improved version

fun multiply(a: Int, b: Int): Int {
    tailrec fun multiplyRec(a: Int, b: Int, acc: Int = 0): Int =
        when (b) {
            0 -> acc
            else -> multiplyRec(a, b - 1, acc + a)
        }

    return multiplyRec(a, b)
}