# Lab3 - nextterm = num/den * (-1)**aterm acc = acc +nextterm...

This preview shows pages 1–3. Sign up to view the full content.

import math from math import * radius = r = 2 volumeSphere = 4/3 * pi * pow(radius, 3) print(volumeSphere) import math def archimedes(sides): innerangleB = 360.0/sides halfangleA = innerangleB/2 onehalfsideS = math.sin(math.radians (halfangleA)) sideS = onehalfsideS * 2 polygonCircumference = sides * sideS pi = polygonCircumference/2 return pi for sides in range(8, 100, 8): print(sides, archimedes(sides)) difference = pi - (archimedes(sides)) print ('difference', difference) def archimedes2 (sides, radius): innerangleB = 360.0/sides halfangleA = innerangleB/2 onehalfsideS = math.sin(math.radians (halfangleA)) * radius sideS = onehalfsideS * 2 polygonCircumference = sides * sideS pi = polygonCircumference/(2 * radius) return pi def calcOddSum (maxNum): acc = 0 for x in range (1, maxNum, 2): acc = acc + x print (acc) def powertower (a, k): acc = a for i in range ( 1, k+1): acc = a ** acc return acc

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
def leibniz (terms): acc = 0 num = 4 den = 1 for aterm in range (terms):
This is the end of the preview. Sign up to access the rest of the 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.

### Page1 / 3

Lab3 - nextterm = num/den * (-1)**aterm acc = acc +nextterm...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online