# Area Under Curves and Volume of Revolving a Curve – HackerRank Solution

In this post, we will solve Area Under Curves and Volume of Revolving a Curve HackerRank Solution. This problem (Area Under Curves and Volume of Revolving a Curve) is a part of HackerRank Functional Programming language.

Contents

Definite Integrals via Numerical Methods

This relates to definite integration via numerical methods.

Consider the algebraic expression given by:

(a1)xb1 + (a2)xb2 + (a3)xb3 + . . . . . + (an)xbn

For the purpose of numerical computation, the area under the curveÂ y = f(x)Â between the limitsÂ aÂ andÂ bÂ can be computed by theÂ Limit Definition of a Definite Integral.

Here is some background about areas and volume computation.

Using equal subintervals of lengthÂ = 0.001, you need to:

1. Evaluate the area bounded by a given polynomial function of the kind described above, between the given limits ofÂ LÂ andÂ R.
2. Evaluate the volume of the solid obtained by revolving this polynomial curve around theÂ x-axis.

A relative error margin ofÂ 0.01Â will be tolerated.

## Input Format

The first line containsÂ NÂ integers separated by spaces, which are the values ofÂ a1, a2, . . . , aN.
The second line containsÂ NÂ integers separated by spaces, which are the values ofÂ b1, b2, . . . , bN.
The third line contains two space separated integers,Â LÂ andÂ R, the lower and upper range limits in which the integration needs to be performed, respectively.

=== codingbroz.com_728x90 (#88864) ===
=== codingbroz.com_728x90 (#88864) ===

## Constraints

• -1000 <= a <= 1000
• -20 <= b <= 20
• 1 <= L <= R <= 20

## Output Format

The first line should contain the area between the curve and theÂ x-axis, bound between the specified limits.
The second line should contain the volume of the solid obtained by rotating the curve around theÂ x-axis, between the specified limits.

Sample Input

``````1 2 3 4 5
6 7 8 9 10
1 4  ``````

Explanation

The algebraic expression represented by:

(1)x6 + (2)x7 + (3)x8 + (4)x9 + (5)x10

We need to find the area of the curve enclosed under this curve, between the limitsÂ x = 1Â andÂ 4. We also need to find the volume of the solid formed by revolving this curve around theÂ x-axis between the limitsÂ x = 1Â andÂ 4.

Sample Output

``````2435300.3
26172951168940.8``````

Scoring

All test cases are weighted equally. You need to clear all the tests in a test case.

## Solution – Area Under Curves and Volume of Revolving a Curve – HackerRank Solution

Scala

```val step = 0.001

def area(coefficients: List[Int], powers: List[Int], x: Double): Double = {
val r = f(coefficients, powers, x)
r * r * math.Pi
}

// This function will be used while invoking "Summation" to compute
// The area under the curve.
def f(coefficients: List[Int], powers: List[Int], x: Double): Double = {
coefficients.zip(powers).map { case (c, p) => c * math.pow(x, p) }.sum
}

def summation(func: (List[Int], List[Int], Double) => Double, upperLimit: Int, lowerLimit: Int, coefficients: List[Int], powers: List[Int]): Double = {
(BigDecimal(lowerLimit) to upperLimit by step).map(x => func(coefficients, powers, x.toDouble)).sum * step
}

// readLine() is deprecated in Scala 13, but it is called by HackerRank's predefined code.
// So it is added to fix the issue.