Question: PYTHON ASSIGNMENT To make this project easier, you can utilize the Fraction module which will have a much higher resolution then using floating point types.

PYTHON ASSIGNMENT

To make this project easier, you can utilize the Fraction module which will have a much higher resolution then using floating point types. So, use the following import command:

from Fraction import *

Additionally, we will use the Decimal data type which has more precision than floats:

from decimal import *

Next, we will need some module-level variables for calculations:

Pi out to 50 digits:

pi50 = Decimal("3.14159265358979323846264338327950288419716939937510")

Number of iterations to perform:

iterations = 1000000

Create an iterator class to calculate the value of pi using the Leibniz series:

class LeibnizPiIterator:

Part II

Now you are going to create a generator that calculates pi using Nilakantha's Series.

def NilakanthaPiGenerator():

In the function, create the following variables:

  • fraction: set it to a new Fraction object with the numerator set to 3 and the denominator to 1. This represents the current value of pi after each iteration.
  • num: set it to 2. This represents the first factor in the denominator calculation at each iteration.
  • add_next: set it to True. This represents whether or not to add or subtract the next value to fraction.

Create an infinite loop. Inside the loop:

  • Calculate the next Fraction object to add/subtraction to fraction. Store it in a variable called operand.
  • Add or subtract operand to fraction based on the value of add_next
  • Flip the value of add_next
  • Add 2 to num
  • yield fraction.value
  • Next, test this generator in much the same way as the iterator created in the first section. You must test it at 100,000 and 10,000,000 iterations. The output should show how much closer to pi this series gets.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!