Question

Write a program that approximates the value of pi. For a given n, the value of...

Write a program that approximates the value of pi. For a given n, the value of pi can be approximated by summing the terms in this series: 4 1 − 4 3 + 4 5 − 4 7 + 4 9 − 4 11 . . . .or by using the following formula:

p i = 4 ∑ k = 1 n ( − 1 ) k + 1 2 k + 1

Assume the value of n is provided by a user.

Homework Answers

Answer #1
# pi_value.py

# most accurate value is at value n = 1000 
n = int(input("Enter value of n : "))

sum = 0
odd = 1

for i in range(n):

    # as we need to perform alternate
    # subtractions and addition
    # we consider even index for addition
    # odd index for subtraction
    if i%2==0:
        sum += (4/odd)
    else:
        sum -= (4/odd)
    
    odd += 2

    
print("Value of  pi : ",sum)

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a python program to find the sum of the first n natural numbers, where the...
Write a python program to find the sum of the first n natural numbers, where the value of n is provided by the user. What is the sum of the first 200 numbers? Write a program that finds the average of a series of numbers entered by the user. As in the previous problem, the program will first ask the user how many numbers there are. Note: the average should always be a float. What is the average of 10.2,...
# Description: This program is a function which calculates pi #per Leibniz formula,based on the number...
# Description: This program is a function which calculates pi #per Leibniz formula,based on the number of values passed to it. # Programmer: xxxxx def calculate_pi(n):     total, sign = 0, 1     for i in range(n):         term = 1 / (2 * i + 1)         total += term * sign         sign *= -1     total *= 4     return total n = int(input("How many terms: ")) print(calculate_pi(n)) How many terms: 12 3.058402765927333 HW In Puthon Modify...
Write a matlab program that determine the value of pi using Monte Carlo technique, using a...
Write a matlab program that determine the value of pi using Monte Carlo technique, using a while loop for a fixed number of precision (i.e 3 significant figures, 4 significant figures). The level of precision should not reference the actual value of pi . (can use avg approximiaton or std deviation method).
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
Write a program which inputs an integer value, incr, from the user at the keyboard, and...
Write a program which inputs an integer value, incr, from the user at the keyboard, and then displays a graph of the sine of the degree values from 0 to 180, in increments of incr. You must use the sin function (in a similar manner to ceil and floor) to find the sine. Note that the sin function only operates on radians so the degree value must first be converted to radians. To do so, multiply the degree value by...
Write a C program that prompts the user to input as many unsigned(n) as the user...
Write a C program that prompts the user to input as many unsigned(n) as the user wants to type, terminated by non negative number You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a double You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a negative number. For each number the user types, output whether that number is prime or not. You won't need...
Part II - Pi Calculation implementation. If you watch the Discovery or Sci-Fi channel you will...
Part II - Pi Calculation implementation. If you watch the Discovery or Sci-Fi channel you will find dozens of alien conspiracy shows that reference Pi as something so advanced that it must be alien.  The number πis a mathematical constant, the ratio of a circle's circumference to its diameter, commonly approximated as 3.14159. So you could graph the value or calculate an approximate value using the series given below: Pi = 4 * (1/1 – 1/3 + 1/5 – 1/7 +...
write a java code. Write a program using loops to compute the sum of the 30...
write a java code. Write a program using loops to compute the sum of the 30 terms of the series below. 91/(3 + 2 + 2) + 92/(4 - 4 + 5) + 93/(5 + 6 - 8) + 94/(6 - 8 + 11) + 95/(7 + 10 + 14) + 96/(8 - 12 - 17) + . . . . Output: Term Ratio Sum 1 13 13 2 18.4 31.4 etc etc
how can I write pi as a maclaurin series using the maclaurin series of arctan(x) and...
how can I write pi as a maclaurin series using the maclaurin series of arctan(x) and letting x = 1 / sqrt(3)?
C program fractions.c that does the following: 1. The program starts by making the user enter...
C program fractions.c that does the following: 1. The program starts by making the user enter a non-negative integer number a, called the first numerator. If the user enters a negative number, the program repeats the entry. 2. The program then makes the user enter a positive integer number b, called the first denominator. If the user enters a non-positive number, the program repeats the entry. 3. The program then makes the user enter a non-negative integer number c, called...