Lesson goal: Adding numbers to find Pi

Previous: Adding numbers like Carl Gauss did | Home | Next: Finding square roots

The famous number $\pi=3.1415...$ can be found by adding together certain patterns of numbers. There are many such patterns, and we'll use one of them here. When finding $\pi$ in this manner, the more numbers you add the better your result for $\pi$ will be. This is called the "infinite series" approach to finding $\pi$.

The infinite series we'll use for $\pi$ is this one $\frac{\pi^2}{6}=\frac{1}{1^2}+\frac{1}{2^2}+\frac{1}{3^2}+\frac{1}{4^2}+...$. As you can see, there is a definite pattern on the right side. Just count from $1$ to $2$ to $3$, etc., always putting the count number (squared) into the denominator of a fraction, with $1$ as the numerator. If you keep adding these fractions, you'll get $\pi^2/6$.

Now you try. Use a for loop and a separate variable called sum to add up ten terms of the $\pi$ sum shown above.

Type your code here:


See your results here:

The code will not run as is! Think a bit now. What should the variable sum be equal to in the sum= line as you start computing $\pi$? Next, in the body of the for-loop, what should you add to the variable sum with each count through the loop? (Hint: think of the $\pi$ pattern discussed above.) Lastly, in the pi=math.sqrt( ) line, what will you take the square root of to arrive at $\pi$ from the sum of your fractions? (Hint: from the formula above, it looks like $\pi=\sqrt{6\cdot sum}$.) Can you get this all fixed and find an approximation for $\pi$? Dismiss.
Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.