Skip to content

Instantly share code, notes, and snippets.

@kkleidal
Created March 14, 2015 16:30
Show Gist options
  • Save kkleidal/d9a7d74a74a22d16012f to your computer and use it in GitHub Desktop.
Save kkleidal/d9a7d74a74a22d16012f to your computer and use it in GitHub Desktop.
pi
@everywhere const exact = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092
@everywhere function approx_pi(n::Integer)
current_term = 0.5
sum = current_term
for i=1:n
current_term *= (2 * i) * (2 * i - 1) / (16 * i * i) * (2 * (i - 1) + 1) / (2 * i + 1)
sum += current_term
end
return 6 * sum
end
function main()
ns = 0:29
function get_error(n::Integer)
pi_approx = approx_pi(n)
rel_error = abs((exact - pi_approx) / exact)
return (n, rel_error)
end
results = pmap(get_error, ns)
for details=results
@printf("%10li %+20.12e \n",
details[1], details[2])
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment