Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C++ Program to Perform Baillie-PSW Primality Test
The Baillie-PSW Primality Test, this test named after Robert Baillie, Carl Pomerance, John Selfridge, and Samuel Wagstaff. It is a test which tests whether a number is a composite number or possibly prime.
Algorithm
MillerTest()
Begin Declare a function MillerTest of Boolean type. Declare MT_dt and MT_num of integer datatype and pass as the parameter. Declare MT_a and MT_x of integer datatype. Initialize MT_a = 2 + rand( ) % (MT_num - 4). Initialize MT_x = pow(MT_a, MT_dt, MT_num). if (MT_x == 1 || MT_x == MT_num - 1) then return true. while (MT_dt != MT_num - 1) do MT_x = (MT_x * MT_x) % MT_num. MT_dt *= 2. if (MT_x == 1) then return false; if (MT_x == MT_num - 1) then return true. return false. End.
Example
#include#include using namespace std; int pow(int pow_a, unsigned int pow_b, int pow_c) { int result = 1; pow_a = pow_a % pow_c; while (pow_b > 0) { if (pow_b & 1) result = (result * pow_a) % pow_c; pow_b = pow_b >> 1; pow_a = (pow_a * pow_a) % pow_c; } return result; } bool MiillerTest(int MT_dt, int MT_num) { int MT_a = 2 + rand( ) % (MT_num - 4); int MT_x = pow(MT_a, MT_dt, MT_num); if (MT_x == 1 || MT_x == MT_num - 1) return true; while (MT_dt != MT_num - 1) { MT_x = (MT_x * MT_x) % MT_num; MT_dt *= 2; if (MT_x == 1) return false; if (MT_x == MT_num - 1) return true; } return false; } bool prime(int P_N, int P_K) { if (P_N >num1; cout>num2; cout Output
Enter the first number: 23 23 is a prime number Enter another number: 45 45 is a composite number
Advertisements
