Cálculo de pi usando la fórmula de Vieta
La fórmula de Vieta para el cálculo de pi es la siguiente \[ \pi = 2 \times \frac{2}{\sqrt{2}} \times \frac{2}{\sqrt{2 + \sqrt{2}}} \times \frac{2}{\sqrt{2 + \sqrt{2 + \sqrt{2}}}} \times \frac{2}{\sqrt{2 + \sqrt{2 + \sqrt{2 + \sqrt{2}}}}} \times \cdots \]
Definir las funciones
aproximacionPi :: Int -> Double errorPi :: Double -> Int
tales que
- (aproximacionPi n) es la aproximación de pi usando n factores de la fórmula de Vieta. Por ejemplo,
aproximacionPi 5 == 3.140331156954753 aproximacionPi 10 == 3.1415914215112 aproximacionPi 15 == 3.141592652386592 aproximacionPi 20 == 3.1415926535886207 aproximacionPi 25 == 3.141592653589795
- (errorPi x) es el menor número de factores de la fórmula de Vieta necesarios para obtener pi con un error menor que x. Por ejemplo,
errorPi 0.1 == 2 errorPi 0.01 == 4 errorPi 0.001 == 6 errorPi 0.0001 == 7 errorPi 1e-4 == 7 errorPi 1e-14 == 24 pi == 3.141592653589793 aproximacionPi 24 == 3.1415926535897913
Soluciones
aproximacionPi :: Int -> Double aproximacionPi n = product [2 / aux x | x <- [0..n]] where aux 0 = 1 aux 1 = sqrt 2 aux n = sqrt (2 + aux (n-1)) errorPi :: Double -> Int errorPi x = head [n | n <- [1..] , abs (pi - aproximacionPi n) < x]