Skip to content

Commit 7016bfc

Browse files
committed
Implemented a prime factorization function using Pollard Rho and a test program.
1 parent 36df926 commit 7016bfc

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ OPTION( BUILD_TESTS "Set to ON to build test programs" OFF )
2727
FIND_PACKAGE( GMP REQUIRED )
2828
INCLUDE_DIRECTORIES( ${GMP_INCLUDE_DIR} )
2929

30-
ADD_DEFINITIONS( "-Wall" )
30+
ADD_DEFINITIONS( "-Wall -Wno-unused-function" )
3131
ADD_SUBDIRECTORY( src )
3232

3333
# Build test programs.

src/PollardRho.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@
77
namespace Math {
88
namespace Fac {
99
namespace {
10+
void pollardRhoFactor0(mpz_t n, vector<mpz_t*> &facs) {
11+
if (isPropPrime(n)) {
12+
mpz_t *tmp = new mpz_t[1];
13+
mpz_init(*tmp);
14+
mpz_set(*tmp, n);
15+
facs.push_back(tmp);
16+
return;
17+
}
18+
19+
mpz_t *g = new mpz_t[1];
20+
mpz_init(*g);
21+
pollardRho(n, *g);
22+
facs.push_back(g);
23+
24+
mpz_divexact(n, n, *g);
25+
pollardRhoFactor0(n, facs);
26+
}
27+
1028
void func(mpz_t r, const mpz_t x, const mpz_t a, const mpz_t n) {
1129
mpz_mul(r, x, x);
1230
mpz_add(r, r, a);
@@ -18,6 +36,14 @@ namespace Math {
1836
}
1937
}
2038

39+
void pollardRhoFactor(const mpz_t n, vector<mpz_t*> &facs) {
40+
mpz_t tmp;
41+
mpz_init(tmp);
42+
mpz_set(tmp, n);
43+
pollardRhoFactor0(tmp, facs);
44+
mpz_clear(tmp);
45+
}
46+
2147
void pollardRho(const mpz_t n, mpz_t f) {
2248
// Return n as the factor if it's a probable-prime.
2349
if (isPropPrime(n)) {

src/PollardRho.h

+9
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
#define MATH_POLLARD_RHO_H
33

44
#include <gmp.h>
5+
#include <vector>
6+
using namespace std;
57

68
namespace Math {
79
namespace Fac {
810
namespace {
11+
void pollardRhoFactor0(mpz_t n, vector<mpz_t*> &facs);
912
void func(mpz_t r, const mpz_t x, const mpz_t a, const mpz_t n);
1013
int funcI(int x, int a, int n);
1114
}
1215

16+
/**
17+
* Finds the prime factorization of n and puts them in the vector
18+
* facs.
19+
*/
20+
void pollardRhoFactor(const mpz_t n, vector<mpz_t*> &facs);
21+
1322
/**
1423
* Finds a nontrivial factor of composite number n of arbitrary
1524
* size and writes the result to f.

test/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ TARGET_LINK_LIBRARIES( PollardRhoI ${CORELIBS} )
1212
ADD_EXECUTABLE( PollardRho PollardRho.cpp )
1313
TARGET_LINK_LIBRARIES( PollardRho ${CORELIBS} )
1414

15+
ADD_EXECUTABLE( PollardRhoFactor PollardRhoFactor.cpp )
16+
TARGET_LINK_LIBRARIES( PollardRhoFactor ${CORELIBS} )
17+

test/PollardRhoFactor.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <gmp.h>
2+
#include <vector>
3+
#include <sstream>
4+
#include <iostream>
5+
using namespace std;
6+
7+
#include "Common.h"
8+
#include "PollardRho.h"
9+
using namespace Math;
10+
using namespace Math::Fac;
11+
12+
#include "TestCommon.h"
13+
14+
void usage(char **argv) {
15+
cout << "Usage: " << argv[0] << " <num>" << endl
16+
<< "Find a nontrivial prime factor of <num>." << endl;
17+
}
18+
19+
int main(int argc, char **argv) {
20+
if (argc != 2) {
21+
usage(argv);
22+
return -1;
23+
}
24+
25+
mpz_t num;
26+
mpz_init(num);
27+
if (mpz_set_str(num, argv[1], 10) == -1) {
28+
mpz_clear(num);
29+
usage(argv);
30+
return -1;
31+
}
32+
33+
vector<mpz_t*> facs;
34+
pollardRhoFactor(num, facs);
35+
36+
dump(num, "Prime factorization of", false);
37+
cout << " is [";
38+
vector<mpz_t*>::iterator it;
39+
for (it = facs.begin(); it != facs.end(); it++) {
40+
dump(**it, "", false);
41+
42+
if (it + 1 != facs.end()) {
43+
cout << ", ";
44+
}
45+
46+
mpz_clear(**it);
47+
delete[] *it;
48+
}
49+
cout << "]" << endl;
50+
51+
mpz_clear(num);
52+
return 0;
53+
}

0 commit comments

Comments
 (0)