Created
December 29, 2019 18:32
-
-
Save ronchaine/1a90b5ac6896b7732542f9102de08166 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
#include <future> | |
#include <chrono> | |
struct Moon | |
{ | |
int pos = 0; | |
int vel = 0; | |
}; | |
struct One_axis_system | |
{ | |
Moon moon1; | |
Moon moon2; | |
Moon moon3; | |
Moon moon4; | |
}; | |
inline void apply_gravity(Moon& a, Moon& b) | |
{ | |
if (a.pos < b.pos) | |
{ | |
a.vel++; | |
b.vel--; | |
} | |
else if (a.pos > b.pos) | |
{ | |
a.vel--; | |
b.vel++; | |
} | |
} | |
inline void apply_gravity(One_axis_system& system) | |
{ | |
apply_gravity(system.moon1, system.moon2); | |
apply_gravity(system.moon1, system.moon3); | |
apply_gravity(system.moon1, system.moon4); | |
apply_gravity(system.moon2, system.moon3); | |
apply_gravity(system.moon2, system.moon4); | |
apply_gravity(system.moon3, system.moon4); | |
} | |
inline void apply_velocity(Moon& moon) | |
{ | |
moon.pos += moon.vel; | |
} | |
inline void apply_velocity(One_axis_system& system) | |
{ | |
apply_velocity(system.moon1); | |
apply_velocity(system.moon2); | |
apply_velocity(system.moon3); | |
apply_velocity(system.moon4); | |
} | |
uint64_t find_period(One_axis_system& system) | |
{ | |
uint64_t rval = 0; | |
while(true) | |
{ | |
apply_gravity(system); | |
apply_velocity(system); | |
apply_gravity(system); | |
apply_velocity(system); | |
rval+=2; | |
if ((system.moon1.vel == 0) | |
&& (system.moon2.vel == 0) | |
&& (system.moon3.vel == 0) | |
&& (system.moon4.vel == 0)) | |
break; | |
} | |
return rval * 2; | |
}; | |
int main() | |
{ | |
One_axis_system x_axis; | |
One_axis_system y_axis; | |
One_axis_system z_axis; | |
x_axis.moon1.pos = 8; | |
y_axis.moon1.pos = 0; | |
z_axis.moon1.pos = 8; | |
x_axis.moon2.pos = 0; | |
y_axis.moon2.pos = -5; | |
z_axis.moon2.pos = -10; | |
x_axis.moon3.pos = 16; | |
y_axis.moon3.pos = 10; | |
z_axis.moon3.pos = -5; | |
x_axis.moon4.pos = 19; | |
y_axis.moon4.pos = -10; | |
z_axis.moon4.pos = -7; | |
auto tp1 = std::chrono::high_resolution_clock::now(); | |
std::future<uint64_t> x_period = std::async(std::launch::async, find_period, std::ref(x_axis)); | |
std::future<uint64_t> y_period = std::async(std::launch::async, find_period, std::ref(y_axis)); | |
std::future<uint64_t> z_period = std::async(std::launch::async, find_period, std::ref(z_axis)); | |
std::cout << "x period: " << x_period.get() << "\n"; | |
std::cout << "y period: " << y_period.get() << "\n"; | |
std::cout << "z period: " << z_period.get() << "\n"; | |
auto tp2 = std::chrono::high_resolution_clock::now(); | |
std::cout << "duration: " << std::chrono::duration_cast<std::chrono::microseconds>(tp2 - tp1).count() << "µs\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I couldn't be arsed to calculate the LCM in the .cpp file.