Created
December 31, 2019 20:37
-
-
Save NickLarsenNZ/ffd3dc9e087a32a3083190d18f414bc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val numbers = listOf( | |
12, | |
7476589, | |
97462994, | |
37837, | |
38840184, | |
2848493, | |
1847479 | |
) | |
numbers.map { | |
val root = digitalRoot(it) | |
println("${root} is the digital root of ${it}") } | |
} | |
// keep summing the digits until there is only one digit left | |
fun digitalRoot(n: Int): Int = when(n) { | |
in 0..9 -> n | |
else -> digitalRoot(digits(n).reduce { acc, i -> acc + i }) | |
} | |
// helper function to get a list of digits | |
fun digits(n: Int): List<Int> { | |
return n | |
.toString() | |
.toList() | |
.map { it | |
.toString() | |
.toInt() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 is the digital root of 12 | |
1 is the digital root of 7476589 | |
5 is the digital root of 97462994 | |
1 is the digital root of 37837 | |
9 is the digital root of 38840184 | |
2 is the digital root of 2848493 | |
4 is the digital root of 1847479 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment