Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <random>
- #include <cstdint>
- #include <cmath>
- /*****************************************************************************************/
- typedef std::uint_fast32_t random_num_type;
- /*****************************************************************************************/
- // Wrapper for random number generator (RNG)
- /*****************************************************************************************/
- std::random_device seed_generator;
- std::mersenne_twister_engine <random_num_type,
- 32, 624, 397, 31,
- 0x9908b0df, 11,
- 0xffffffff, 7,
- 0x9d2c5680, 15,
- 0xefc60000, 18, 1812433253> gen(seed_generator());
- random_num_type generate_number()
- {
- return gen();
- }
- /*****************************************************************************************/
- // GCD Calculator (Euclid's Devision Lemma/Algorithm)
- /*****************************************************************************************/
- random_num_type gcd(random_num_type a, random_num_type b)
- {
- return b == 0 ? a : gcd(b, a % b);
- }
- /*****************************************************************************************/
- // GCD Calculator (Euclid's Devision Lemma/Algorithm)
- /*****************************************************************************************/
- int main()
- {
- std::cout << std::setprecision(8);
- int count_coprime = 0, trials;
- std::cout << "Enter the number of number pairs/number of trials to use: ";
- std::cin >> trials;
- int trials_per_percent = trials / 100;
- for (int i = trials; i > 0; i--)
- {
- random_num_type a = generate_number();
- random_num_type b = generate_number();
- if (gcd(a, b) == 1) count_coprime++;
- if (i%trials_per_percent == 0) std::cout << ".";
- }
- double pi = std::sqrt(static_cast<double>(6*trials)/count_coprime);
- std::cout << std::endl << "Out of " << trials << " pairs, " <<count_coprime<< "were co-prime pairs." << std::endl;
- std::cout << "Approximate value of pi is: " << pi;
- std::cin >> trials;
- return 0;
- }
Add Comment
Please, Sign In to add comment