Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07bbf96

Browse files
committed
Add article about integer factorization
This time without overwriting all primality/montgomery stuff.
1 parent becfdf1 commit 07bbf96

File tree

8 files changed

+502
-3
lines changed

8 files changed

+502
-3
lines changed

img/pollard_rho.png

8.92 KB
Loading

src/algebra/factorization.md

Lines changed: 424 additions & 0 deletions
Large diffs are not rendered by default.

src/algebra/montgomery_multiplication.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<!--?title Montgomery Multiplication -->
22
# Montgomery Multiplication
33

4-
Many algorithms in number theory, like [prime testing](./algebra/primality_tests.html) or factorization, and in cryptography, like RSA, require lots of operations modulo a large number.
5-
4+
Many algorithms in number theory, like [prime testing](./algebra/primality_tests.html) or [integer factorization](./algebra/factorization.html), and in cryptography, like RSA, require lots of operations modulo a large number.
65
A multiplications like $x y \bmod{n}$ is quite slow to compute with the typical algorithms, since it requires a division to know how many times $n$ has to be subtracted from the product.
76
And division is a really expensive operation, especially with big numbers.
87

src/algebra/phi-function.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ int phi(int n) {
5757
}
5858
```
5959
60-
## Application in Euler's theorem
60+
## Application in Euler's theorem ## {#application}
6161
6262
The most famous and important property of Euler's totient function is expressed in **Euler's theorem**:
6363
$$a^{\phi(m)} \equiv 1 \pmod m$$ if $a$ and $m$ are relatively prime.

src/algebra/primality_tests.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ bool isPrime(int x) {
2626
2727
This is the simplest form of a prime check.
2828
You can optimize this function quite a bit, for instance by only checking all odd numbers in the loop, since the only even prime number is 2.
29+
Multiple such optimizations are described in the article about [integer factorization](./algebra/factorization.html).
2930
3031
## Fermat primality test
3132

src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and adding new articles to the collection.*
2121
- [Sieve of Eratosthenes](./algebra/sieve-of-eratosthenes.html)
2222
- [Sieve of Eratosthenes With Linear Time Complexity](./algebra/prime-sieve-linear.html)
2323
- [Primality tests](./algebra/primality_tests.html)
24+
- [Integer factorization](./algebra/factorization.html)
2425
- **Number-theoretic functions**
2526
- [Euler's totient function](./algebra/phi-function.html)
2627
- [Number of divisors / sum of divisors](./algebra/divisors.html)

test/test_factorization.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <vector>
2+
#include <cassert>
3+
#include <array>
4+
#include <map>
5+
using namespace std;
6+
7+
long long power(long long b, long long e, long long mod) {
8+
long long res = 1;
9+
while (e) {
10+
if (e & 1)
11+
res = res * b % mod;
12+
b = b * b % mod;
13+
e >>= 1;
14+
}
15+
return res;
16+
}
17+
18+
long long gcd(long long a, long long b) {
19+
if (a == 0)
20+
return b;
21+
return gcd(b % a, a);
22+
}
23+
24+
#include "../test/factorization_trial_division1.h"
25+
#include "../test/factorization_trial_division2.h"
26+
#include "../test/factorization_trial_division3.h"
27+
#include "../test/factorization_trial_division4.h"
28+
#include "../test/factorization_p_minus_1.h"
29+
#include "../test/pollard_rho.h"
30+
#include "../test/pollard_rho_brent.h"
31+
32+
int main() {
33+
map<long long, vector<long long>> test_data = {
34+
{132, {2, 2, 3, 11}},
35+
{123456789, {3, 3, 3607, 3803}},
36+
{4817191, {1303, 3697}}
37+
};
38+
primes = {2, 3, 5, 7, 1303, 3607};
39+
40+
for (auto n_expected : test_data) {
41+
auto n = n_expected.first;
42+
auto expected = n_expected.second;
43+
44+
assert(trial_division1(n) == expected);
45+
assert(trial_division2(n) == expected);
46+
assert(trial_division3(n) == expected);
47+
assert(trial_division4(n) == expected);
48+
49+
long long g = pollards_p_minus_1(n);
50+
assert(g > 1 && g < n && n % g == 0);
51+
52+
g = rho(n);
53+
assert(g > 1 && g < n && n % g == 0);
54+
g = brent(n);
55+
assert(g > 1 && g < n && n % g == 0);
56+
}
57+
}

test/test_pollard_rho.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <cassert>
2+
#include <cmath>
3+
using namespace std;
4+
5+
long long gcd(long long a, long long b) {
6+
if (a == 0)
7+
return b;
8+
return gcd(b % a, a);
9+
}
10+
11+
#include "pollard_rho.h"
12+
13+
int main() {
14+
long long n = 21471091LL * 38678789LL;
15+
long long g = rho(n);
16+
assert(g > 1 && g < n);
17+
}

0 commit comments

Comments
 (0)