-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathfactor
executable file
·60 lines (51 loc) · 990 Bytes
/
factor
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
#!/usr/bin/perl
#
# factor numbers using yafu
# -samyk
use strict;
die "usage: $0 [-v] <number>\n" unless @ARGV;
my @factors;
my $num = shift;
my $YAFU = "yafu";
#83473593554391843334619428139045661537976651941410655062632649869770538548577
my $verbose = 1;
factor($num);
print "factors: @factors\n";
sub factor
{
my $num = shift;
my ($factor1, $factor2, $ecm);
print "Running: ecm($num)\n";
open(F, "$YAFU 'ecm($num)'|") || die;
while (<F>)
{
print if ($verbose);
# we may need to sqrt() the number
if (/^ecm/)
{
$ecm++;
}
elsif (/^P\d\s*=\s*(\d+)$/)
{
$factor1 = $1;
push @factors, $factor1;
}
#elsif (/factors found/)
elsif (/^(\d+)$/)
{
$factor2 = $1;
}
}
print "$factor1 - $factor2 - $ecm\n";
# found it!
if ($factor2 == 1) { print "factored!\n\n" }
elsif ($factor2 == $num)
{
my $sqrt = sqrt($num);
push @factors, $sqrt, $sqrt;
}
else
{
factor($factor2);
}
}