Created
August 8, 2020 02:01
-
-
Save kimji1/63c448815d10f8b07e341f54fa48369f to your computer and use it in GitHub Desktop.
codility
This file contains hidden or 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() { | |
| println(solution(28)) | |
| println(solution(734)) | |
| println(solution(1990)) | |
| println(solution(1000)) | |
| } | |
| fun solution(N: Int): Int { | |
| val target = sumOfDisassembledN(N) | |
| var n = N | |
| do { | |
| n += 1 | |
| val sum = sumOfDisassembledN(n) | |
| } while (sum != target) | |
| return n | |
| } | |
| fun sumOfDisassembledN(N: Int): Int { | |
| var sum = 0 | |
| var n = N | |
| while (n > 0) { | |
| sum += n % 10 | |
| n /= 10 | |
| } | |
| return sum | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment