# Experimenting with a sequence of complex numbers in Kotlin ## The challenge

Consider the sequence `S(n, z) = (1 - z)(z + z**2 + z**3 + ... + z**n)` where `z` is a complex number and `n` a positive integer (n > 0).

When `n` goes to infinity and `z` has a correct value (ie `z` is in its domain of convergence `D`), `S(n, z)` goes to a finite limit `lim` depending on `z`.

Experiment with `S(n, z)` to guess the domain of convergence `D`of `S` and `lim` value when `z` is in `D`.

Then determine the smallest integer `n` such that `abs(S(n, z) - lim) < eps` where `eps` is a given small real number and `abs(Z)` is the modulus or norm of the complex number Z.

Call `f` the function `f(z, eps)` which returns `n`. If `z` is such that `S(n, z)` has no finite limit (when `z` is outside of `D``f` will return -1.

#### Examples:

I is a complex number such as I * I = -1 (sometimes written `i` or `j`).

`f(0.3 + 0.5 * I, 1e-4) returns 17`

`f(30 + 5 * I, 1e-4) returns -1`

#### Remark:

For languages that don’t have complex numbers or “easy” complex numbers, a complex number `z` is represented by two real numbers `x` (real part) and `y` (imaginary part).

`f(0.3, 0.5, 1e-4) returns 17`

`f(30, 5, 1e-4) returns -1`

#### Note:

You pass the tests if `abs(actual - exoected) <= 1`

## The solution in Kotlin

Option 1:

```.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%}```package solv

private fun modul(x: Double, y: Double): Double {
if (x != 0.0 || y != 0.0)
return Math.sqrt(x * x + y * y)
else
return 0.0
}
fun f(x: Double, y: Double, eps: Double): Int {
if (modul(x, y) >= 1.0)
return -1
return (Math.log(eps) / Math.log(modul(x, y))).toInt()
}
```Code language: Kotlin (kotlin)```

Option 2:

``````package solv

import kotlin.math.*

fun f(x: Double, y: Double, eps: Double): Int {
val m = hypot(x, y)
return if (m < 1) log(eps, m).toInt() else -1
}
```Code language: Kotlin (kotlin)```

Option 3:

``````package solv

fun f(x: Double, y: Double, eps: Double): Int {
val res = Math.log(eps) / Math.log(Math.hypot(x, y))
return if (res < 0) -1 else res.toInt()
}
```Code language: Kotlin (kotlin)```

## Test cases to validate our solution

``````package solv

import org.junit.Assert.*
import org.junit.Test
import java.util.Random

class solvTest {
private fun dotest(x:Double, y: Double, eps: Double, expect: Int) {
val merr = 1.0
println("Testing " + x + " " + y + " " + eps)
val actual = f(x, y, eps)
println("Actual: " + actual)
println("Expect: " + expect)
val inrange = Math.abs(actual - expect) <= merr
if (inrange == false)
{
println("Expected must be near " + expect + ", got " + actual)
}
println("-")
assertEquals(true, inrange)
}
@Test
fun test1() {
dotest(0.64, 0.75, 1e-12, 1952)
dotest(0.3, 0.5, 1e-4, 17)
dotest(30.0, 50.0, 1e-4, -1)

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