fork download
  1. fun f(x: Double): Double {
  2. return x * x + 2 * x
  3. }
  4.  
  5. fun rectanglesMethod(a: Int, b: Int, n: Int): Double {
  6. val rectangleWidth = (b - a).toDouble() / n.toDouble()
  7. var area = 0.0
  8. var currentPoint = a.toDouble()
  9.  
  10. for (i in 1..n) {
  11. currentPoint += rectangleWidth
  12. val rectangleHeight = f(currentPoint)
  13. area += rectangleHeight * rectangleWidth
  14. }
  15.  
  16. return area
  17. }
  18.  
  19.  
  20. fun main() {
  21. val a = 0
  22. val b = 10
  23. val n = 100
  24.  
  25. val area = rectanglesMethod(a, b, n)
  26.  
  27. println(area)
  28. }
Success #stdin #stdout 0.08s 36552KB
stdin
Standard input is empty
stdout
439.34999999999894