integ <- function(f, a, b, n, u, v) {
  h <- (b - a) / n
  s <- 0
  for (i in seq(0, n - 1)) {
    s <- s + sum(v * f(a + i * h + u * h))
  }
  s * h
}

test <- function(f, a, b, n) {
  c(rect.left = integ(f, a, b, n, 0, 1),
    rect.right = integ(f, a, b, n, 1, 1),
    rect.mid = integ(f, a, b, n, 0.5, 1),
    trapezoidal = integ(f, a, b, n, c(0, 1), c(0.5, 0.5)),
    simpson = integ(f, a, b, n, c(0, 0.5, 1), c(1, 4, 1) / 6))
}

test(\(x) x^3, 0, 1, 100)



test(\(x) 1 / x, 1, 100, 1000)



test(\(x) x, 0, 5000, 5e6)



test(\(x) x, 0, 6000, 6e6)