FastPrepEstimate Pi with a Finite Series

Estimate Pi with a Finite Series

Squarepoint Capital logoSquarepoint Capital● EasyNEW GRADONSITE INTERVIEW

Updated

Problem statement

Write a function that estimates pi using a finite series.

For this exercise, assume the required estimate is 4 * sum((-1)^k / (2*k + 1)) for integer k from 0 through terms - 1. Return this finite sum, rather than the built-in value of pi.

Each additional term alternates sign. The result approaches pi as the number of terms grows.

Function

estimatePi(terms: int) → double

Examples

Example 1

terms = 1return = 4.0

The first term is 4 / 1 = 4.

Example 2

terms = 2return = 2.6666666666666665

The first two terms give 4 * (1 - 1/3) = 8/3.

Example 3

terms = 3return = 3.466666666666667

The next positive term gives 4 * (1 - 1/3 + 1/5) = 52/15.

Constraints

  • 1 <= terms <= 1000000.
  • Return a finite decimal value. The answer is accepted with an absolute-or-relative tolerance of 10^-9 against the stated finite sum.

More Squarepoint Capital interview questions

Similar questions at other companies

Interview reports that mention this question

Squarepoint Capital interview experiences

All Squarepoint Capital interview questionsSee Squarepoint Capital hiring insights
Saved
class Solution {
    public double estimatePi(int terms) {
        // write your code here
    }
}
Checking account…