/
builtin_numbertheory.go
65 lines (63 loc) · 1.47 KB
/
builtin_numbertheory.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
package expreduce
import "math/big"
func GetNumberTheoryDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "PrimeQ",
legacyEvalFn: singleParamQEval(primeQ),
})
defs = append(defs, Definition{
Name: "GCD",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
zero := big.NewInt(0)
var ints [](*big.Int)
for i := 1; i < len(this.Parts); i++ {
asInt, isInt := this.Parts[i].(*Integer)
if !isInt {
return this
}
r := asInt.Val.Cmp(zero)
if r > 0 {
tmpI := big.NewInt(0)
tmpI.Set(asInt.Val)
ints = append(ints, tmpI)
}
if r < 0 {
tmpI := big.NewInt(0)
tmpI.Neg(asInt.Val)
ints = append(ints, tmpI)
}
}
if len(ints) == 0 {
return &Integer{zero}
}
gcd := ints[0]
for i := 1; i < len(ints); i++ {
gcd.GCD(nil, nil, gcd, ints[i])
}
return &Integer{gcd}
},
})
defs = append(defs, Definition{Name: "LCM"})
defs = append(defs, Definition{
Name: "Mod",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 3 {
return this
}
xi, xIsInt := this.Parts[1].(*Integer)
yi, yIsInt := this.Parts[2].(*Integer)
if !xIsInt || !yIsInt {
return this
}
if yi.Val.Cmp(big.NewInt(0)) == 0 {
return &Symbol{"System`Indeterminate"}
}
m := big.NewInt(0)
m.Mod(xi.Val, yi.Val)
return &Integer{m}
},
})
defs = append(defs, Definition{Name: "EvenQ"})
defs = append(defs, Definition{Name: "OddQ"})
return
}