forked from randall77/factorlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
151 lines (140 loc) · 3.36 KB
/
Copy pathmain.go
File metadata and controls
151 lines (140 loc) · 3.36 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"os"
"runtime/pprof"
"strconv"
"github.com/cfschilham/factorlib"
"github.com/cfschilham/factorlib/big"
)
var seed = flag.Int64("seed", 0, "seed for RNG")
var alg = flag.String("alg", "trial", "factoring algorithm to use")
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var logfile = flag.String("logfile", "", "logging file (default=stderr)")
func main() {
flag.Parse()
// Set up output streams and logger.
var logger *log.Logger
var fatal func(msg string, args ...interface{})
var output func(msg string, args ...interface{})
if *logfile == "" {
logger = log.New(os.Stderr, "", log.LstdFlags)
fatal = func(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
output = func(msg string, args ...interface{}) {
fmt.Fprintf(os.Stdout, msg+"\n", args...)
}
} else {
w, err := os.Create(*logfile)
if err != nil {
fmt.Fprintf(os.Stderr, "can't open log file %s\n", *logfile)
os.Exit(1)
}
defer w.Close()
logger = log.New(w, "", log.LstdFlags)
fatal = func(msg string, args ...interface{}) {
logger.Printf(msg, args...)
fmt.Fprintf(os.Stderr, msg+"\n", args...)
w.Close()
os.Exit(1)
}
output = func(msg string, args ...interface{}) {
logger.Printf(msg, args...)
fmt.Fprintf(os.Stdout, msg+"\n", args...)
}
}
// Set up profile output, if requested.
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
fatal("can't create profile file: %v", err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
logger.Printf("cpu profile output to %s", *cpuprofile)
}
// Initialize random seed.
rnd := rand.New(rand.NewSource(*seed))
logger.Printf("seed: %d", *seed)
// Figure out the number to factor
args := flag.Args()
logger.Printf("args: %s", args)
if len(args) == 0 {
fatal("no number to factor")
}
if len(args) > 1 {
fatal("can't factor multiple numbers")
}
var n big.Int
nstr := args[0]
switch nstr[0] {
case 'r':
// random d-digit number
d, err := strconv.Atoi(nstr[1:])
if err != nil {
fatal("%v", err)
}
k := big.Ten.Exp(int64(d) - 1)
n = k.Mul64(9).Rand(rnd).Add(k)
case 's':
// random d-digit semiprime
d, err := strconv.Atoi(nstr[1:])
if err != nil {
fatal("%v", err)
}
if d%2 != 0 {
fatal("semiprime %s must request an even number of digits", nstr)
}
min := big.Ten.Exp(int64(d) - 1)
max := min.Mul64(10)
for {
x := randomPrime(d/2, rnd)
y := randomPrime(d/2, rnd)
n = x.Mul(y)
if n.Cmp(min) >= 0 && n.Cmp(max) < 0 {
break
}
}
default:
var ok bool
n, ok = big.ParseInt(nstr)
if !ok {
fatal("parsing \"%s\": invalid number", nstr)
}
}
logger.Printf("factoring %d", n)
// Call into main library to do factoring
if *alg != "" {
logger.Printf("using algorithm %s", *alg)
}
factors, err := factorlib.Factor(n, *alg, rnd, logger)
// Print result
if err != nil {
output("factorization failed: %s", err)
return
}
s := fmt.Sprintf("%d = ", n)
for i, f := range factors {
if i > 0 {
s += "·"
}
s += fmt.Sprintf("%d", f)
}
output(s)
}
// make a random prime with the given number of digits
func randomPrime(digits int, rnd *rand.Rand) big.Int {
min := big.Ten.Exp(int64(digits - 1))
w := min.Mul64(9)
for {
n := min.Add(w.Rand(rnd))
if n.ProbablyPrime(1000) {
return n
}
}
}