Exercise: Compute Pi

Test your programming skills by writing a program to calculate an approximate value of Pi.

We'll cover the following

Compute Pi

Write a program to compute the value of π \pi  based on the formula below:

    π=4×(113+1517+19111+113115+...)\pi = 4 \times ( 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \frac{1}{11} + \frac{1}{13} - \frac{1}{15} + ... )

Note: We cannot sum infinitely many terms. So, we'll compute an approximate value by limiting the number of terms.

Compare your result with Ruby's built-in constant Math::PI.

Get hands-on with 1200+ tech skills courses.