-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApproximating the Pi.cpp
64 lines (49 loc) · 1.65 KB
/
Approximating the Pi.cpp
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
60
61
62
63
64
#include <vector>
#include <iostream>
// I saw the logic of this at The Coding Train. Just gave it a try and that's some nice shit.
const int* radius = new int(2);
const int* radiusPowered = new int(pow(*radius, 2));
const int* sampleCountEachGeneration = new int(10000);
int* inCircle = new int(0);
int* outCircle = new int(0);
int* step = new int(0);
long double* approximation = new long double();
inline long double GetRandomValue()
{
long double number = (long double)rand() / (long double)(RAND_MAX) * *radius;
if (((float)rand() / RAND_MAX) > 0.5) number *= -1;
return number;
}
struct Particle {
long double* x = new long double(GetRandomValue());
long double* y = new long double(GetRandomValue());
long double* distance = new long double(sqrt((pow(*x, 2) + pow(*y, 2))));
bool* isInside = new bool(*distance <= *radiusPowered);
void Update() {
*x = GetRandomValue();
*y = GetRandomValue();
*distance = pow(*x, 2) + pow(*y, 2);
*isInside = *distance <= *radiusPowered;
}
};
int main() {
std::cout.precision(15);
srand((unsigned) time(NULL));
std::vector<Particle*>* particles = new std::vector<Particle*>();
for (int i = 0; i < *sampleCountEachGeneration; ++i) {
Particle* p = new Particle;
particles->push_back(p);
}
while (true) {
*inCircle = 0;
*outCircle = 0;
for (Particle* p : *particles) {
if (*p->isInside) *inCircle += 1;
else *outCircle += 1;
p->Update();
}
*step += 1;
*approximation = ((*step-1) * *approximation + static_cast<long double>(4) * (long double)*inCircle / (long double)*sampleCountEachGeneration) / (long double)*step;
std::cout << "Approximate Pi: " << *approximation << std::endl;
}
}