PI approximation
Description:
The aim of the kata is to try to show how difficult it can be to calculate decimals of an irrational number with a certain precision. We have chosen to get a few decimals of the number "pi" using the following infinite series (Leibniz 1646–1716):
PI / 4 = 1 - 1/3 + 1/5 - 1/7 + ... which gives an approximation of PI / 4.
http://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
To have a measure of the difficulty we will count how many iterations are needed to calculate PI with a given precision of epsilon
.
There are several ways to determine the precision of the calculus but to keep things easy we will calculate PI
within epsilon of your language Math::PI constant.
In other words, given as input a precision of epsilon
we will stop the iterative process when the absolute value of the difference between our calculation using the Leibniz series and the Math::PI constant of your language is less than epsilon
.
Your function returns an array or a string or a tuple depending on the language (See sample tests) with
- your number of iterations
- your approximation of PI with 10 decimals
Example :
Given epsilon = 0.001 your function gets an approximation of 3.140592653839794 for PI at the end of 1000 iterations : since you are within epsilon
of Math::PI
you return
iter_pi(0.001) --> [1000, 3.1405926538]
Notes :
Unfortunately, this series converges too slowly to be useful, as it takes over 300 terms to obtain a 2 decimal place precision. To obtain 100 decimal places of PI, it was calculated that one would need to use at least 10^50 terms of this expansion!
About PI : http://www.geom.uiuc.edu/~huberty/math5337/groupe/expresspi.html
Similar Kata:
Stats:
Created | Mar 15, 2015 |
Published | Mar 15, 2015 |
Warriors Trained | 26969 |
Total Skips | 9672 |
Total Code Submissions | 36792 |
Total Times Completed | 5719 |
Ruby Completions | 123 |
C# Completions | 324 |
Haskell Completions | 99 |
Java Completions | 633 |
Python Completions | 1527 |
JavaScript Completions | 824 |
CoffeeScript Completions | 16 |
Clojure Completions | 37 |
Elixir Completions | 102 |
TypeScript Completions | 133 |
C++ Completions | 545 |
PHP Completions | 166 |
Crystal Completions | 11 |
F# Completions | 46 |
C Completions | 293 |
OCaml Completions | 35 |
Rust Completions | 267 |
Swift Completions | 117 |
Go Completions | 290 |
R Completions | 70 |
Shell Completions | 28 |
Julia Completions | 33 |
Scala Completions | 64 |
PowerShell Completions | 33 |
Nim Completions | 18 |
Racket Completions | 17 |
Forth Completions | 12 |
Kotlin Completions | 102 |
Groovy Completions | 7 |
Fortran Completions | 21 |
Dart Completions | 85 |
Lua Completions | 37 |
Elm Completions | 6 |
D Completions | 5 |
Prolog Completions | 5 |
Total Stars | 297 |
% of votes with a positive feedback rating | 83% of 930 |
Total "Very Satisfied" Votes | 666 |
Total "Somewhat Satisfied" Votes | 206 |
Total "Not Satisfied" Votes | 58 |