Skip to content

Commit 929b5b2

Browse files
Implement factorization (PollarsPho) (#2)
1 parent cabd2d3 commit 929b5b2

6 files changed

Lines changed: 177 additions & 0 deletions

File tree

BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module(
2+
name = "cpp-analytics-devops",
3+
version = "1.0.0",
4+
)
5+
6+
bazel_dep(name = "googletest", version = "1.17.0")

src/BUILD

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cc_library(
2+
name = "factorLib",
3+
srcs = [
4+
"factor.cpp"
5+
],
6+
hdrs = [
7+
"factor.h"
8+
],
9+
)
10+
11+
cc_binary(
12+
name = "mainProg",
13+
srcs = [
14+
"main.cpp",
15+
"main.h",
16+
],
17+
deps = [
18+
":factorLib",
19+
],
20+
)

src/factor.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include "factor.h"
2+
3+
using namespace std;
4+
5+
// === member functions for PollardsPho ===
6+
7+
/// @brief constructor
8+
/// @param n the value for fatorization
9+
PollardsPho::PollardsPho(int64_t n) : n(n)
10+
{
11+
srand(time(0));
12+
}
13+
14+
/// @brief the main function to do the calculation
15+
/// @return result
16+
int64_t PollardsPho::calculate()
17+
{
18+
int64_t d = 1;
19+
int64_t cnt = 0;
20+
int64_t x = selectX0();
21+
int64_t y = x;
22+
23+
while(d == 1 || d == n)
24+
{
25+
d = basicIteration(x, y);
26+
27+
cout << cnt << ":" << endl;
28+
cout << "x, y, d:" << x << "," << y << "," << d << endl;
29+
30+
if (d == n)
31+
{
32+
failure++;
33+
}
34+
cnt++;
35+
}
36+
37+
return d;
38+
}
39+
40+
/// @brief basic iteration of the algorithm
41+
/// @param x x
42+
/// @param y y
43+
/// @return d
44+
int64_t PollardsPho::basicIteration(int64_t &x, int64_t &y)
45+
{
46+
selectC(c);
47+
x = polyFuncMod(x, c);
48+
y = polyFuncMod(polyFuncMod(y, c), c);
49+
int64_t d = calculateD(x, y);
50+
51+
return d;
52+
}
53+
54+
int64_t PollardsPho::selectX0()
55+
{
56+
return (rand() % (n - 2)) + 2;
57+
}
58+
59+
/// @brief function to calculate f(x) = x^2 + c
60+
/// @param x x
61+
/// @param c c
62+
/// @return f(x)
63+
int64_t PollardsPho::polyFuncMod(int64_t x, int64_t c)
64+
{
65+
__int128_t output = (x * x + c) % n;
66+
return output;
67+
}
68+
69+
/// @brief funtion to calculate D = gcd(abs(x-y), n)
70+
int64_t PollardsPho::calculateD(int64_t x, int64_t y)
71+
{
72+
return gcd(abs(x-y), n);
73+
}
74+
75+
/// @brief function to select c
76+
/// @return c
77+
void PollardsPho::selectC(int64_t& input)
78+
{
79+
if (failure < 100)
80+
input = 1;
81+
else if (failure < 200)
82+
input = 2;
83+
else if (failure < 300)
84+
input = 3;
85+
// choose from [4, n)
86+
else
87+
input = (rand() % (n - 4)) + 4;
88+
}
89+
90+
91+
// === member functions for Factorization ===

src/factor.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef FACTOR_H
2+
#define FACTOR_H
3+
4+
#include <vector>
5+
#include <iostream>
6+
#include <cstdint>
7+
#include <ctime>
8+
#include <cmath>
9+
#include <cstdlib>
10+
#include <numeric>
11+
12+
class PollardsPho
13+
{
14+
public:
15+
PollardsPho(int64_t n);
16+
~PollardsPho() = default;
17+
int64_t calculate();
18+
19+
private:
20+
int64_t n;
21+
int64_t c;
22+
int64_t failure = 0;
23+
int64_t polyFuncMod(int64_t x, int64_t c);
24+
int64_t calculateD(int64_t x, int64_t y);
25+
void selectC(int64_t& input);
26+
int64_t selectX0();
27+
int64_t basicIteration(int64_t &x, int64_t &y);
28+
};
29+
30+
// class Factorization
31+
// {
32+
// public:
33+
// Factorization(int64_t n) : n(n)
34+
// {
35+
// }
36+
// ~Factorization() = default;
37+
// private:
38+
// int64_t n;
39+
// };
40+
41+
#endif

src/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "main.h"
2+
3+
using namespace std;
4+
5+
int main()
6+
{
7+
int64_t n = 10967535067;
8+
auto pollardsPho = PollardsPho(n);
9+
auto result = pollardsPho.calculate();
10+
11+
cout << "One factor of " << n << " is: " << result << endl;
12+
}

src/main.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef MAIN_H
2+
#define MAIN_H
3+
4+
#include <iostream>
5+
#include "factor.h"
6+
7+
#endif

0 commit comments

Comments
 (0)