/**
 * This function calculates the interest rate required to reach a target amount given an initial principal.
 * The interest rate is calculated using binary search.
 *
 * @param principal The initial amount of money
 * @param targetAmount The desired amount of money
 * @return The interest rate required to reach the target amount
 * @throws IllegalArgumentException If the target amount is smaller than the principal
 */
fun findInterestRate(principal: Double, targetAmount: Double): Double {

    require(targetAmount >= principal) { "Target amount must be greater than the principal." }

    var lowerBound = 0.0
    var upperBound = 1.0

    while (true) {
        val guessRate = (lowerBound + upperBound) / 2
        val prediction = principal * Math.pow(1 + guessRate, 5.0) // compound interest for 5 years

        if (Math.abs(prediction - targetAmount) < 1e-6) {
            return guessRate
        }

        if (prediction < targetAmount) {
            lowerBound = guessRate
        } else {
            upperBound = guessRate
        }
    }
}
/**
 * This function calculates the interest rate required to reach a target amount given an initial principal.
 * The interest rate is calculated using binary search.
 *
 * @param principal The initial amount of money
 * @param targetAmount The desired amount of money
 * @return The interest rate required to reach the target amount
 * @throws IllegalArgumentException If the target amount is smaller than the principal
 */
fun findInterestRate(principal: Double, targetAmount: Double): Double {
/**
 * This function calculates the interest rate required to reach a target amount given an initial principal.
 * The interest rate is calculated using binary search.
 *
 * @param principal The initial amount of money
 * @param targetAmount The desired amount of money
 * @return The interest rate required to reach the target amount
 * @throws IllegalArgumentException If the target amount is smaller than the principal
 */
fun findInterestRate(principal: Double, targetAmount: Double): Double {

    require(targetAmount >= principal) { "Target amount must be greater than the principal." }

    var lowerBound = 0.0
    var upperBound = 1.0

    while (true) {
        val guessRate = (lowerBound + upperBound) / 2
        val prediction = principal * Math.pow(1 + guessRate, 5.0) // compound interest for 5 years

        if (Math.abs(prediction - targetAmount) < 1e-6) {
            return guessRate
        }

        if (prediction < targetAmount) {
            lowerBound = guessRate
        } else {
            upperBound = guessRate
        }
    }
}