// Списки (стеки, очереди) I(1..n) и U(1..n) содержат результаты n измерений тока и напряжения на неизвестном
// сопротивлении R. Найти приближенное число R методом наименьших квадратов.

import kotlin.math.pow

// Create a class LeastSquaresR that takes two lists of measurements, currents, and voltages
class LeastSquaresR(private val currents: List<Double>, private val voltages: List<Double>) {

    // Check if the input lists have the same size
    init {
        require(currents.size == voltages.size) { "Currents and voltages lists must have the same size" }
    }

    // Implement a method that calculates the least squares approximation of R based on the input data
    fun calculateR(): Double {
        // Calculate the sum of the product of each current and voltage measurement
        val sumIU = currents.zip(voltages) { i, u -> i * u }.sum()

        // Calculate the sum of the square of each current measurement
        val sumISquared = currents.map { it.pow(2) }.sum()

        // Calculate the number of measurements
        val n = currents.size

        // Calculate the approximate value of the unknown resistance R using the least squares method
        val r = sumIU / sumISquared

        return r
    }
}

fun task2V1() {
    // Provide sample data: two lists of current and voltage measurements
    val currents = listOf(0.5, 1.0, 1.5, 2.0, 2.5)
    val voltages = listOf(1.0, 2.1, 3.1, 4.1, 5.0)

    // Create a LeastSquaresR object with the sample data
    val leastSquaresR = LeastSquaresR(currents, voltages)

    // Calculate the approximate value of the unknown resistance R
    val r = leastSquaresR.calculateR()

    // Print the result
    println("The approximate value of the unknown resistance R is: $r Ω")
}