0

I am having an issue getting the python 2.5 shell to do what I need to do. I am trying to have the user input a value for "n" representing a number of times the loop will be repeated. In reality, I need to have the user input N that will correspond to the number of terms from the Gregory–Leibniz series and outputs the approximation of pi.

Gregory–Leibniz series pi=4*((1/1)-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)...)

So when n is 3,I need the loop calculates up to 1/5. Unfortunately, it is always giving me a value of 0 for the variable of total.

My code as of right now is wrong, and I know that. Just looking for some help. Code below:

def main(): n = int(raw_input("What value of N would you like to calculate?")) for i in range(1,n,7): total = (((1)/(i+i+1))-((1)/(i+i+2))+((1)/(i+i+4))) value = 4*(1-total) print(value) if __name__ == "__main__": main() 
6
  • 1
    That's not Fermat's Last Theorem. FLT is about adding numbers together raised to the same power. Not about pi. Commented Sep 24, 2014 at 21:24
  • Sorry about that. That is my mistake. For some reason I thought it was Fermat. Commented Sep 24, 2014 at 21:32
  • 1
    It could be that your problem is that integers use integer division, so 1/2==0. If you use 1.0 in place of 1 you will get float division instead. Commented Sep 24, 2014 at 21:33
  • Look at the print function. O/P is using Python 3.0. Don't need explicit floating point. Commented Sep 24, 2014 at 21:35
  • @GaryWalker "...an issue getting the python 2.5 shell to do what I need ..." Commented Sep 24, 2014 at 21:37

3 Answers 3

2

This uses integer division, so you will get zero:

total = (((1)/(i+i+1))-((1)/(i+i+2))+((1)/(i+i+4))) 

Instead, use floats to get float division.

total = ((1.0/(i+i+1))-(1.0/(i+i+2))+(1.0/(i+i+4))) 

In python 2, by default doing / on integers will give you an integer.

In python 3, this has been changed, and / always performed float division (// does integer division).

Sign up to request clarification or add additional context in comments.

1 Comment

You can also call from future import division (or something like that).
2

You need to accumulate terms. e.g.

total = 0.0 term = 1.0 for i in range (1,n+1): denom = 2*i-1 total += term/denom term = -term 

Of course, you can express this more tersely

It is also more natural perhaps to use this instead

total = 0.0 term = 1.0 for i in range (n): denom = 2*i+1 total += term/denom term = -term

As you use the most natural form of of n terms in a range this way. Note the difference in how denominator is calculated.

3 Comments

total += 1/denom will convert return int not float
O/P is using Python 3 -- look at the print statement
@GaryWalker You can use print(value) in 2.5, and the question explicitly says that he's using 2.5. Your answer will not help the OP because of this.
0

Q1) Go to https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 to find the Leibniz formula for π. Let S be the sequence of terms that is used to approximate π. As we can see, the first term in S is +1, the second term in S is -1/3 and the third term in S is +1/5 and so on. Find the smallest number of terms such that the difference between 4*S and π is less than 0.01. That is, abs(4*S – math.pi) <= 0.01.

2 Comments

Welcome to Stack Overflow! If you wrap your code in backticks `like this` your posts will be much more beautiful.
This doesn't answer the question. You're saying how to use the Leibniz formula to calculate π to some accuracy. The question is how to get a non-zero value of the formula when you already know you want n terms.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.