18 std::chrono::steady_clock::time_point start_time =
19 std::chrono::steady_clock::now();
22 std::cout <<
"Miller-Rabin sequentially without ThreadPool" << std::endl;
24 for (uint64_t n : nums) {
26 std::cout << n <<
" is prime" << std::endl;
28 std::cout << n <<
" is composite" << std::endl;
31 std::chrono::steady_clock::time_point end_time =
32 std::chrono::steady_clock::now();
34 std::cout <<
"Time elapsed: "
35 << std::chrono::duration_cast<std::chrono::milliseconds>(
36 end_time - start_time)
38 <<
" ms" << std::endl;
43 std::chrono::steady_clock::time_point start_time =
44 std::chrono::steady_clock::now();
49 std::vector<std::future<bool>> miller_results;
52 miller_results.emplace_back(pool->
enqueue(
53 [&
prim, n]() { return prim.miller_rabin_prime(n, 120000); }));
57 std::cout <<
"\nResults:\n";
58 std::cout <<
"Miller-Rabin with ThreadPool" << std::endl;
59 for (
size_t i = 0; i < miller_results.size(); i++) {
60 bool is_prime = miller_results[i].get();
61 std::cout << nums[i] <<
" is " << (is_prime ?
"prime" :
"composite")
66 std::chrono::steady_clock::time_point end_time =
67 std::chrono::steady_clock::now();
69 std::cout <<
"Time elapsed: "
70 << std::chrono::duration_cast<std::chrono::milliseconds>(
71 end_time - start_time)
73 <<
" ms" << std::endl;
78 std::chrono::steady_clock::time_point start_time =
79 std::chrono::steady_clock::now();
80 auto start = std::chrono::high_resolution_clock::now();
82 std::vector<std::future<bool>> miller_results;
99 std::cout <<
"\nResults:\n";
100 std::cout <<
"Miller-Rabin with ThreadPool using ThreadDispatch.dispatch()"
103 for (
size_t i = 0; i < miller_results.size(); i++) {
104 bool is_prime = miller_results[i].get();
105 std::cout << nums[i] <<
" is " << (is_prime ?
"prime" :
"composite")
110 std::chrono::steady_clock::time_point end_time =
111 std::chrono::steady_clock::now();
113 std::cout <<
"Time elapsed: "
114 << std::chrono::duration_cast<std::chrono::milliseconds>(
115 end_time - start_time)
117 <<
" ms" << std::endl;
121 std::cout <<
"BASIC NUMBER THEORY OPERATIONS\n" << std::endl;
125 std::cout <<
"<--------- IS IT PRIME? --------->\n";
130 std::cout << a <<
" is prime? : ";
131 is_it ? std::cout <<
"true\n" : std::cout <<
"false\n" << std::endl;
136 std::cout << b <<
" is prime? : ";
137 is_it2 ? std::cout <<
"true\n" : std::cout <<
"false\n" << std::endl;
140 std::cout <<
"<--------- MILLER-RABIN METHOD --------->\n";
156 std::cout <<
"<--------- CARMICHAEL NUMBERS --------->\n";
166 std::cout << cm_test <<
" is a carmichael number : ";
167 carm_num ? std::cout <<
"true\n" : std::cout <<
"false\n";
169 std::cout << cm_test1 <<
" is a carmichael number : ";
170 carm_num1 ? std::cout <<
"true\n" : std::cout <<
"false\n";
172 std::cout << cm_test2 <<
" is a carmichael number : ";
173 carm_num2 ? std::cout <<
"true\n" : std::cout <<
"false\n";
183 int pow_res = p.
mod_pow(pow_a, pow_b, pow_c);
184 printf(
"%d ^ %d %% %d = %d\n", pow_a, pow_b, pow_c, pow_res);
190 int pow_res_1 = p.
mod_pow(pow_d, pow_e, pow_f);
191 printf(
"%d ^ %d %% %d = %d\n", pow_d, pow_e, pow_f, pow_res_1);
197 int pow_res_2 = p.
mod_pow(pow_g, pow_h, pow_i);
198 printf(
"%d ^ %d %% %d = %d\n", pow_g, pow_h, pow_i, pow_res_2);
201 std::cout <<
"miller_rabin_prime: " << cmp <<
"\n";
212 printf(
"%d is prime\n", ss_0);
214 printf(
"%d is composite\n", ss_0);
219 std::vector<int64_t> nums = {
220 9223372036854775803, 9223372036854775807, 9223372036854775303,
221 4567890123456789LL, 5678901234567890LL, 6789012345678901LL,
222 7890123456789012LL, 8901234567890123LL, 9999999967LL,
223 12345678901234567LL, 987654321987654321LL, 2147483647LL,
224 9223372036854775783LL, 1311768467463790320LL, 7237005577332262210LL,
225 3037000499LL, 2305843009213693951LL, 2305843009213693967LL,
226 2305843009213693971LL, 2305843009213693973LL, 2305843009213693977LL,
227 2305843009213693989LL};
230 std::cout <<
"<--------- USING THREADPOOL --------->\n";
bool miller_rabin_prime(uint64_t n, uint64_t iters)
Modified primes algorithm.
bool carmichael_num(uint64_t n)
void miller_rabin(uint64_t iters, uint64_t min_val, uint64_t max_val)
Miller-Rabin driver, prints values that satisfy conditions.
uint64_t mod_pow(uint64_t a, uint64_t b, uint64_t m)
bool is_prime(uint64_t n)
Determine if an integer is prime.
bool solovoy_strassen(uint64_t p, uint64_t iters)
A class that provides a function to dispatch a function call to a thread pool and return a future obj...
auto enqueue(F &&f, Args &&...args) -> std::future< typename std::invoke_result< F, Args... >::type >
Enqueues a task to the thread pool.
void testing_miller(std::vector< int64_t > nums)
void testing_new_miller(std::vector< int64_t > nums)
void testing_miller_thread(std::vector< int64_t > nums)