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 ===
0 commit comments