-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbigint.go
105 lines (99 loc) · 2 KB
/
bigint.go
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
package main
import (
"fmt"
"math/big"
"os"
"sort"
"strconv"
)
type Ints []*big.Int
func (v Ints) Len() int {
return len(v)
}
func (v Ints) Less(i, j int) bool {
return v[i].Cmp(v[j]) < 0
}
func (v Ints) Swap(i, j int) {
v[i], v[j] = v[j], v[i]
}
const maxWordNum = 74
type BigIntArray [maxWordNum]big.Word
func toA(x *big.Int) (r BigIntArray) {
for i, v := range x.Bits() {
r[i] = v
}
return
}
func main() {
mode := 0
if len(os.Args) >= 2 {
mode, _ = strconv.Atoi(os.Args[1])
}
const an = 500
const bn = 500
switch mode {
case 0:
fmt.Println("use map")
m := map[string]bool{}
x := big.NewInt(0)
for a := 2; a <= an; a++ {
for b := 2; b <= bn; b++ {
m[x.Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), nil).String()] = true
}
}
fmt.Println(len(m))
case 1:
fmt.Println("use sort")
v := Ints{}
for a := 2; a <= an; a++ {
for b := 2; b <= bn; b++ {
x := new(big.Int).Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), nil)
v = append(v, x)
}
}
sort.Sort(v)
n := 1
for i := 1; i < len(v); i++ {
if v[i-1].Cmp(v[i]) != 0 {
n++
}
}
fmt.Println(n)
case 2:
fmt.Println("use map with Bytes(), String()")
m := map[string]bool{}
x := big.NewInt(0)
for a := 2; a <= an; a++ {
for b := 2; b <= bn; b++ {
m[string(x.Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), nil).Bytes())] = true
}
}
fmt.Println(len(m))
case 3:
fmt.Println("use BigIntArray")
m := map[BigIntArray]bool{}
x := big.NewInt(0)
for a := 2; a <= an; a++ {
for b := 2; b <= bn; b++ {
m[toA(x.Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), nil))] = true
}
}
fmt.Println(len(m))
case 4:
fmt.Println("use Bits().String()")
m := map[string]bool{}
x := big.NewInt(0)
toS := func(x []big.Word) (s string) {
for _, v := range x {
s += fmt.Sprintf("%x", v)
}
return
}
for a := 2; a <= an; a++ {
for b := 2; b <= bn; b++ {
m[toS(x.Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), nil).Bits())] = true
}
}
fmt.Println(len(m))
}
}