-
Notifications
You must be signed in to change notification settings - Fork 33
/
factorization_with_difference_of_prime_factors.pl
executable file
·69 lines (52 loc) · 1.36 KB
/
factorization_with_difference_of_prime_factors.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 09 August 2017
# https://github.com/trizen
# Theorem:
# If the absolute difference between the prime factors of a
# semiprime `n` is known, then `n` can be factored in polynomial time.
# For example:
# n = 97 * 43
# n = 4171
#
# d = 97 - 43
# d = 54
# Then the factors of `n` are:
# 43 = abs((-54 + sqrt(54^2 + 4*4171)) / 2)
# 97 = abs((-54 - sqrt(54^2 + 4*4171)) / 2)
# In general:
# n = p * q
# d = abs(p - q)
# From which `n` can be factored as:
# n = abs((-d + sqrt(d^2 + 4*n)) / 2) *
# abs((-d - sqrt(d^2 + 4*n)) / 2)
#
# Based on the following quadratic equation:
# x^2 + (a - b)*x - a*b = 0
#
# which has the solutions:
# x₁ = -a
# x₂ = +b
use 5.010;
use strict;
use warnings;
use ntheory qw(random_nbit_prime);
use Math::AnyNum qw(:overload isqrt);
my $p = Math::AnyNum->new(random_nbit_prime(100));
my $q = Math::AnyNum->new(random_nbit_prime(100));
my $d = abs($p - $q);
my $n = $p * $q;
say "n = $p * $q";
say "d = $d";
sub integer_quadratic_formula {
my ($x, $y, $z) = @_;
(
((-$y + isqrt($y**2 - 4 * $x * $z)) / (2 * $x)),
((-$y - isqrt($y**2 - 4 * $x * $z)) / (2 * $x)),
);
}
my ($x1, $x2) = integer_quadratic_formula(1, $d, -$n);
printf("n = %s * %s\n", abs($x1), abs($x2));
if (abs($x1) * abs($x2) != $n) {
die "error: $x1 * $x2 != $n\n";
}