-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMillerRabin.h
46 lines (36 loc) · 1.86 KB
/
MillerRabin.h
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
/*
* MillerRabin.h
*
* Created on: Sep 22, 2018
* Author: fanchen
*/
#ifndef MILLER_RABIN_H
#define MILLER_RABIN_H
#include <iostream>
#include <vector>
class MillerRabin{
// Use the Miller-Rabin primality test to determine whether a given number is prime.
// Upper limit for determination is set to the size limit of unsigned long long.
// User must provide the number to be checked, and a list of test cases (available at https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test) based on how big the given number is.
public:
// construtor
// Precondition: a static C-style int array containing test cases and the number of test cases are passed to the function
// Postcondition: the values in the array added to the testCases vector.
// Description: initialize test cases in the class
MillerRabin(int* tests, int size);
// Precondition: three unsigned long long variables passed to the function.
// Postcondition: return the remainder after 'mod' divides 'base' raised to 'expo' power.
// Description: handy function to perform base^expo % mod
unsigned long long expoMod(unsigned long long base, int expo, unsigned long long mod);
// Precondition: one specific testCase, the number to be checked, and special number d and r (acquired from isPrime()) are passed
// Postcondition: return true if the testCase passes, otherwise false.
// Description: testCase passes means n MIGHT be prime, but fails means n DEFINITELY NOT prime
bool millerRabinTest(int testCase, int d, int r, unsigned long long n);
// Precondition: the number to be checked is passed. The testCases vector must be initialized (call addTestCases() to initialize testCases).
// Postcondition: return true if n is prime, false otherwise
// Description: the main function to determine primality of n.
bool isPrime(unsigned long long num);
private:
std::vector<int> testCases;
};
#endif