-
Notifications
You must be signed in to change notification settings - Fork 36
/
Polydivisible-number.kt
36 lines (29 loc) · 1.25 KB
/
Polydivisible-number.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// https://www.codingame.com/ide/puzzle/polydivisible-number
import java.math.BigInteger
import java.util.*
// Migrate BigInteger from java lib, because the IDE not supporting kotlin's BigInteger.
fun Int.toBigInteger(): BigInteger = BigInteger.valueOf(this.toLong())
fun String.toBigInteger(): java.math.BigInteger = java.math.BigInteger(this)
fun main(args: Array<String>) {
val s = Scanner(System.`in`).nextLine().split(" ").map { it.toBigInteger() }.reversed()
for (base in 2 until 36) {
var base10 = BigInteger.valueOf(0)!!
if (!s.all { it < base.toBigInteger() }) continue
for (k in 0 until s.size) {
var tempBase = 1.toBigInteger()
// Using Math.pow with converting do double losing precision, so repeat is the best solution.
repeat(k) { tempBase *= base.toBigInteger() }
val currNum = s[k].times(tempBase)
base10 += currNum
}
var flag = true
for (n in 0 until base10.toString().length) {
val current = base10.toString().slice(0..n).toBigInteger()
if (current.rem((n + 1).toBigInteger()) != 0.toBigInteger()) {
flag = false
break
}
}
if (flag) println(base)
}
}