-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPicalculator.cpp
More file actions
52 lines (44 loc) · 1.45 KB
/
Picalculator.cpp
File metadata and controls
52 lines (44 loc) · 1.45 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
#include <iostream>
#include <iomanip>
#include <cmath>
typedef long double ld;
constexpr bool SHOW_ARCCOS_V2 = false;
using std::cout;
using std::endl;
using std::setbase;
using std::acos;
using std::pow;
int main(int argc, const char* argv[]) {
// change *digit* and *base* to see different results!
// digit: number of sig-fig of pi
// base: can be 8, 10 or 16
constexpr auto digit = 10;
constexpr auto base = 10;
constexpr auto power = 2 * (digit - 1);
constexpr ld m1 = 1;
constexpr ld m2 = pow(base, power);
size_t count = 0;
// velocity to left < 0
ld v1 = 0, v2 = -1;
ld temp_v1;
// m1 will hit m2 iff v1 > v2;
// m1 will hit the wall iff v1 < 0;
while (v1 > v2) {
if constexpr (SHOW_ARCCOS_V2) {
cout << acos(v2) << endl;
}
// m1 hits m2
count++;
// temp_v1 = ((m2 - m1) * v2 + 2 * m1 * v1) / (m1 + m2);
temp_v1 = ((m1 - m2) / (m1 + m2)) * v1 + (2 * m2 / (m1 + m2)) * v2;
// m1 goes right after she hits m2
if (temp_v1 >= 0) break;
// v2 = ((m1 - m2) * v1 + 2 * m2 * v2) / (m2 + m1);
v2 = ((m2 - m1) / (m1 + m2)) * v2 + (2 * m1 / (m1 + m2)) * v1;
// m1 hits the wall and changes the direction of velocity
v1 = -temp_v1;
count++;
}
cout << "The collision happens " << setbase(base) << count << " times" << endl;
return 0;
}