mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
formatting source-code for a48d05fb62
This commit is contained in:
parent
a48d05fb62
commit
bbe1ff1286
@ -15,7 +15,8 @@
|
|||||||
* @param num integer number that we want to convert
|
* @param num integer number that we want to convert
|
||||||
* @return result vector of the number input in reverse binary
|
* @return result vector of the number input in reverse binary
|
||||||
*/
|
*/
|
||||||
template <typename T> std::vector<T> reverse_binary(T num) {
|
template <typename T>
|
||||||
|
std::vector<T> reverse_binary(T num) {
|
||||||
std::vector<T> result;
|
std::vector<T> result;
|
||||||
T temp = num;
|
T temp = num;
|
||||||
while (temp > 0) {
|
while (temp > 0) {
|
||||||
@ -35,8 +36,8 @@ template <typename T> std::vector<T> reverse_binary(T num) {
|
|||||||
* raised to
|
* raised to
|
||||||
* @param mod modulo
|
* @param mod modulo
|
||||||
* @return r the modular exponentiation of \f$a^{n} \equiv r \mod{m}\f$ where
|
* @return r the modular exponentiation of \f$a^{n} \equiv r \mod{m}\f$ where
|
||||||
* \f$n\f$ is the base 10 representation of rev_binary_exponent and \f$m = mod \f$
|
* \f$n\f$ is the base 10 representation of rev_binary_exponent and \f$m = mod
|
||||||
* parameter.
|
* \f$ parameter.
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T modular_exponentiation(T base, const std::vector<T> &rev_binary_exponent,
|
T modular_exponentiation(T base, const std::vector<T> &rev_binary_exponent,
|
||||||
@ -68,7 +69,8 @@ T modular_exponentiation(T base, const std::vector<T> &rev_binary_exponent,
|
|||||||
* @return 'false' if n is composite
|
* @return 'false' if n is composite
|
||||||
* @return 'true' if n is (probably) prime.
|
* @return 'true' if n is (probably) prime.
|
||||||
*/
|
*/
|
||||||
template <typename T> bool miller_test(T d, T num) {
|
template <typename T>
|
||||||
|
bool miller_test(T d, T num) {
|
||||||
// random number seed
|
// random number seed
|
||||||
std::random_device rd_seed;
|
std::random_device rd_seed;
|
||||||
// random number generator
|
// random number generator
|
||||||
@ -112,14 +114,15 @@ template <typename T> bool miller_test(T d, T num) {
|
|||||||
* First we check whether the num input is less than 4, if so we can determine
|
* First we check whether the num input is less than 4, if so we can determine
|
||||||
* whether this is a prime or composite by checking for 2 and 3.
|
* whether this is a prime or composite by checking for 2 and 3.
|
||||||
* Next we check whether this num is odd (as all primes greater than 2 are odd).
|
* Next we check whether this num is odd (as all primes greater than 2 are odd).
|
||||||
* Next we write our num in the following format \f$num = 2^r \cdot d + 1\f$. After
|
* Next we write our num in the following format \f$num = 2^r \cdot d + 1\f$.
|
||||||
* finding r and d for our input num, we use for loop repeat number of times
|
* After finding r and d for our input num, we use for loop repeat number of
|
||||||
* inside which we check the miller conditions using the function miller_test.
|
* times inside which we check the miller conditions using the function
|
||||||
* If miller_test returns false then the number is composite
|
* miller_test. If miller_test returns false then the number is composite After
|
||||||
* After the loop finishes completely without issuing a false return call,
|
* the loop finishes completely without issuing a false return call, we can
|
||||||
* we can conclude that this number is probably prime.
|
* conclude that this number is probably prime.
|
||||||
*/
|
*/
|
||||||
template <typename T> bool miller_rabin_primality_test(T num, T repeats) {
|
template <typename T>
|
||||||
|
bool miller_rabin_primality_test(T num, T repeats) {
|
||||||
if (num <= 4) {
|
if (num <= 4) {
|
||||||
// If num == 2 or num == 3 then prime
|
// If num == 2 or num == 3 then prime
|
||||||
if (num == 2 || num == 3) {
|
if (num == 2 || num == 3) {
|
||||||
|
Loading…
Reference in New Issue
Block a user