This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: nextterm = num/den * (-1)**aterm acc = acc +nextterm den = den + 2 return acc def leibniz2 (terms): acc = 0 num = 4 den = 1 sign = 1 for aterm in range (terms): nextterm = num/den * sign acc = acc + nextterm den = den + 2 sign = sign * -1 return acc def leibniz12 (terms): for i in range (100000, 1000000+1, 100000): start = time.time() result1 = leibniz(terms) middle = time.time() result2 = leibniz2(terms) end = time.time() time1 = middle - start time2 = end - middle print (result1, result2, time1, time2) def wallis (pairs): acc = 1.0 num = 2.0 for i in range (pairs): leftterm = num/(num-1) rightterm = num/(num+1) acc = acc * leftterm * rightterm num = num + 2 pi = acc * 2 return pi def wallis2(pairs): acc = 2.0 num = 3.0 for i in range (pairs): leftterm = (num-1)/num rightterm = (num+1)/num acc = acc * leftterm * rightterm num = num + 2 pi = acc * 2 return pi def pi acc = 1 x = acc...
View Full Document
This note was uploaded on 04/26/2011 for the course CPS 324 taught by Professor Safa during the Spring '11 term at Dickinson.
- Spring '11