-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
59 lines (43 loc) · 1.31 KB
/
Copy pathmain.cpp
File metadata and controls
59 lines (43 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: Andrea
*
* Created on 30 de junio de 2016, 20:23
*/
#include <cstdlib>
#include <math.h>
#include <iostream>
#include <iomanip>
using namespace std;
//Tiene un gran problema. No tiene la precisión necesaria, por lo que en adelante probaré con GMP.
int factorial(int n) {
if (n == 0) return 1;
if (n != 1)
return n * factorial(n - 1);
}
/**
* @brief Calcula una aproximación al número Pi
* @param k Por cada incremento de k se obtienen 14 decimales, empezando por 0.
*/
long double pi(int k) {
long double sum = 0;
long double termino = 0;
for (int i = 0; i < k + 1; i++) {
long double dividendo = pow(-1, i) * factorial(6.0 * i) * (13591409.0 + 545140134.0 * i);
long double divisor = factorial(3 * i) * pow(factorial(i), 3.0) * pow(640320.0, (3.0 * i) + 3.0 / 2);
long double total = (dividendo / divisor);
sum += total;
}
sum *= 12.0;
sum = 1 / sum;
return sum;
}
int main(int argc, char** argv) {
cout << setprecision(30) << pi(1) << endl;
return 0;
}