Hackerrank – Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Create a recursive function with condition described – see implementation.
I created solution in:
All solutions are also available on my GitHub.
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import scala.io.Source object SuperDigit extends App { val line = Source.stdin.bufferedReader().readLine() val nk = line.split(" ") val n = nk(0) val k = nk(1).toInt println(superDigit(n, k)) def superDigit(num: String, k: Int): String = { if(num.length == 1) { return num } else { val sum: BigInt = num.foldLeft(BigInt(0))(_ + Character.digit(_, 10)) * k return superDigit(sum.toString, 1) } } } |