/**
 * Calculate an approximation of pi using the Nilakantha series.
 *
 * @param n Number of iterations.
 * @param precision Number of decimal places to round the approximation.
 * @return Approximation of pi rounded to the given precision.
 */
fun calculatePi(n: Int, precision: Int): Double {

    var pi = 3.0
    var sign = -1

    for (i in 2 until n * 2 step 2) {
        pi += sign * (4 / (i * (i + 1) * (i + 2)).toDouble())
        sign *= -1
    }

    return pi.round(precision)
}

fun Double.round(decimals: Int): Double = "%.${decimals}f".format(this).toDouble()
/**
 * Calculate an approximation of pi using the Nilakantha series.
 *
 * @param n Number of iterations.
 * @param precision Number of decimal places to round the approximation.
 * @return Approximation of pi rounded to the given precision.
 */
fun calculatePi(n: Int, precision: Int): Double {
/**
 * Calculate an approximation of pi using the Nilakantha series.
 *
 * @param n Number of iterations.
 * @param precision Number of decimal places to round the approximation.
 * @return Approximation of pi rounded to the given precision.
 */
fun calculatePi(n: Int, precision: Int): Double {

    var pi = 3.0
    var sign = -1

    for (i in 2 until n * 2 step 2) {
        pi += sign * (4 / (i * (i + 1) * (i + 2)).toDouble())
        sign *= -1
    }

    return pi.round(precision)
}

fun Double.round(decimals: Int): Double = "%.${decimals}f".format(this).toDouble()