Skip to content

Runpython-IntroProgramming/Calculate-Pi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Calculate π

The purpose of this challenge is to practice using list comprehensions with input and output.

Write and submit a Python program that computes an approximate value of π by calculating the following sum:

Pi Generator

This sum approaches the true value of π as n approaches ∞.

The big "E" thing in that equation means "add that thing to the right over and over again, but keep changing the value of k. The first value of k is zero, the last value is n and k goes up by one each time."

Your program must ask the user how many terms to use in the estimate of π, how many decimal places, then print the estimate using that many decimal places. Exactly like this:

I will estimate pi. How many terms should I use? 100
How many decimal places should I use in the result? 7
The approximate value of pi is 3.1315929

What the heck? How about an example to get me started?

While this task can be accomplished in only one line of Python code, you might use three or more to keep the program readable. Brevity is important, but not most important! Because this task is challenging, I have an example of how you might implement a similar problem: computing an estimate for Euler's number, e.

One (admittedly silly) way to estimate e is to compute the reciprocal of the following:

Reciprocal of e

The true value of e begins with these digits (the true value is irrational and cannot be written with a finite number of digits): 2.7182818284590452353602874...

A sample Python program for estimating the value of e might look like this:

import math
n = int(input("I will estimate e. How many terms should I use? "))
decimals = int(input("How many decimal places should I use in the result? "))
e = 1.0/sum([((-1.0)**k)/math.factorial(k) for k in range(0,n)])
print("The approximate value of e is {0}".format(round(e, decimals)))
print("(The true value of e is {0})".format(round(math.e, decimals)))

The line:

e = 1.0/sum([((-1.0)**k)/math.factorial(k) for k in range(0,n)])

does all the work. Let's break it down.

  1. ((-1.0)**k)/math.factorial(k) computes the value of each element (k) in the series.
  2. for k in range(0,n) generates n values of k.
  3. [((-1.0)**k)/math.factorial(k) for k in range(0,n)] is a list comprehension that generates a list of all n elements (numbers).
  4. sum([((-1.0)**k)/math.factorial(k) for k in range(0,n)]) takes the list of elements and computes the sum of them (this is the big "E" in the formula).
  5. 1.0/sum([((-1.0)**k)/math.factorial(k) for k in range(0,n)]) takes the reciprocal of that sum!

Decimal Places

Notice how the example code prints the results with a specific number of decimal places. Your code will need to do something similar. Remember how to do this!

Importing Libraries

One thing this example shows that you may still be unfamiliar with is import math. The Python import statement is used to load another Python program file at the start of your program. In this case, math is a standard Python library that contains a variety of common mathematical functions (we are using the factorial function in the example). Look at the code example and notice that factorial is prefaced with math.. Any time you load external libraries like this, you can refer to them using this style (there are other ways of doing this, but this is the simplest).

You can see all of the mathematical functions in math by typing

import math

in the Python console, then

dir(math)

to see all of the functions it contains.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published