fun f(x: Double): Double {
return x * x + 2 * x
}
fun trapezesMethod(a: Int, b: Int, n: Int): Double {
val trapezeHeight = (b - a).toDouble() / n.toDouble()
var area = 0.0
var currentPoint = a.toDouble()
for (i in 1..n) {
val trapezeFirstSide = f(currentPoint)
currentPoint += trapezeHeight
val trapezeSecondSide = f(currentPoint)
area += ((trapezeFirstSide + trapezeSecondSide) * trapezeHeight) / 2
}
return area
}
fun main() {
val a = 0
val b = 10
val n = 100
val area = trapezesMethod(a, b, n)
println(area)
}
ZnVuIGYoeDogRG91YmxlKTogRG91YmxlIHsKICAgIHJldHVybiB4ICogeCArIDIgKiB4Cn0KCmZ1biB0cmFwZXplc01ldGhvZChhOiBJbnQsIGI6IEludCwgbjogSW50KTogRG91YmxlIHsKICAgIHZhbCB0cmFwZXplSGVpZ2h0ID0gKGIgLSBhKS50b0RvdWJsZSgpIC8gbi50b0RvdWJsZSgpCiAgICB2YXIgYXJlYSA9IDAuMAogICAgdmFyIGN1cnJlbnRQb2ludCA9IGEudG9Eb3VibGUoKQoKICAgIGZvciAoaSBpbiAxLi5uKSB7CiAgICAgICAgdmFsIHRyYXBlemVGaXJzdFNpZGUgPSBmKGN1cnJlbnRQb2ludCkKICAgICAgICBjdXJyZW50UG9pbnQgKz0gdHJhcGV6ZUhlaWdodAogICAgICAgIHZhbCB0cmFwZXplU2Vjb25kU2lkZSA9IGYoY3VycmVudFBvaW50KQogICAgICAgIGFyZWEgKz0gKCh0cmFwZXplRmlyc3RTaWRlICsgdHJhcGV6ZVNlY29uZFNpZGUpICogdHJhcGV6ZUhlaWdodCkgLyAyCiAgICB9CgogICAgcmV0dXJuIGFyZWEKfQoKZnVuIG1haW4oKSB7CiAgICB2YWwgYSA9IDAKICAgIHZhbCBiID0gMTAKICAgIHZhbCBuID0gMTAwCiAgICAKICAgIHZhbCBhcmVhID0gdHJhcGV6ZXNNZXRob2QoYSwgYiwgbikKICAgIAogICAgcHJpbnRsbihhcmVhKQp9