# Moduli number system in Kotlin

## The challenge

A number system with moduli is deﬁned by a vector of k moduli, `[m1,m2, ···,mk]`.

The moduli must be `pairwise co-prime`, which means that, for any pair of moduli, the only common factor is `1`.

In such a system each number `n` is represented by a string `"-x1--x2-- ... --xk-"` of its residues, one for each modulus. The product `m1 * ... * mk` must be greater than the given number `n` which is to be converted in the moduli number system.

For example, if we use the system `[2, 3, 5]` the number `n = 11` is represented by `"-1--2--1-"`,
the number `n = 23` by `"-1--2--3-"`.

If we use the system `[8, 7, 5, 3]` the number `n = 187` becomes `"-3--5--2--1-"`.

You will be given a number `n (n >= 0)` and a system `S = [m1,m2, ···,mk]` and you will return a string `"-x1--x2-- ...--xk-"` representing the number `n` in the system `S`.

If the moduli are not `pairwise co-prime` or if the product `m1 * ... * mk` is not greater than `n`, return `"Not applicable"`.

#### Examples:

(you can add them in the “Sample tests”)

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```fromNb2Str(11, [2,3,5]) -> "-1--2--1-"
fromNb2Str(6, [2, 3, 4]) -> "Not applicable", since 2 and 4 are not coprime
fromNb2Str(7, [2, 3]) -> "Not applicable" since 2 * 3 < 7```Code language: JavaScript (javascript)```

## The solution in Kotlin

Option 1:

``````package solution
import java.math.BigInteger

object ModSystem {

fun fromNb2Str(n: Int, sys: IntArray): String {
if (sys.reduce { acc, i -> acc * i } <= n) return "Not applicable"
sys.forEachIndexed { index, a -> sys.drop(index + 1).forEach { b ->
if (a.toBigInteger().gcd(b.toBigInteger()) > BigInteger.ONE) return "Not applicable"
} }
return sys.joinToString("") { "-\${n % it}-" }
}
}
```Code language: Kotlin (kotlin)```

Option 2:

``````package solution

object ModSystem {

fun fromNb2Str(n: Int, sys: IntArray): String {
return when {
sys.reduce {acc, i -> acc * i} <= n -> "Not applicable"
sys.count { it % 2 == 0} > 1 -> "Not applicable"
else -> sys.map { n % it}.joinToString(
prefix = "-",
postfix = "-",
separator = "--"
)
}
}
}
```Code language: Kotlin (kotlin)```

Option 3:

``````package solution
import kotlin.collections.*

object ModSystem {

fun product(sys: IntArray) = sys.fold(1) { acc, e -> acc * e }

tailrec fun gcd(a: Int, b: Int): Int = if(a == 0) b else gcd(b%a, a)

fun pairWiseCoprime(sys: IntArray): Boolean{
val arr = sys.toCollection(ArrayList())
do{
for(el in arr) if(gcd(el, head) != 1) return false
} while ( arr.isNotEmpty() )
return true
}

fun fromNb2Str(n: Int, sys: IntArray): String {
if(!pairWiseCoprime(sys) || product(sys) <= n) return "Not applicable"
return sys.map { n % it }.joinToString(separator = "--", prefix = "-", postfix = "-")
}
}
```Code language: Kotlin (kotlin)```

## Test cases to validate our solution

``````package solution

import org.junit.Test
import kotlin.test.assertEquals

class  ModSystemTest {

private fun testing(n: Int, bases: IntArray, expect: String) {
val actual: String = ModSystem.fromNb2Str(n, bases)
assertEquals(expect, actual)
}
@Test
fun basicTests() {
testing(779, intArrayOf(8,7,5,3), "-3--2--4--2-")
testing(187, intArrayOf(8,7,5,3), "-3--5--2--1-")
testing(259, intArrayOf(8,7,5,3), "-3--0--4--1-")
testing(15, intArrayOf(8,6,5,3), "Not applicable")
testing(15, intArrayOf(3, 2), "Not applicable")

}
}
```Code language: Kotlin (kotlin)```
Tags:
Subscribe
Notify of