Debian Codegraph @

texmf-dist/tex/lualatex/luacas/test/rings/luacas-number.lua

raw
1 local a = Integer(5)
2 local b = Integer(3)
3 local c = Integer(-12)
4 local d = Integer("-54321")
5 local e = Integer("99989999999999999989999999999999999999999999999999999999989999999999999999999999999998999999999999999999999999989999999998")
6 local f = Integer("-1267650600228229401496703205376")
7 local g = Integer(16)
8 local h = Integer(8)
9 local x = Integer(8) / Integer(5)
10 local y = Integer(1) / Integer(12)
11 local z = Integer(-7) / Integer(10)
12
13
14 starttest("integer construction")
15 testeq(a, 5)
16 testeq(b, 3)
17 testeq(c, -12)
18 testeq(d, "-54321")
19 testeq(e, "99989999999999999989999999999999999999999999999999999999989999999999999999999999999998999999999999999999999999989999999998")
20 testeq(f, "-1267650600228229401496703205376")
21 endtest()
22
23 starttest("integer operations")
24 testeq(-c, 12)
25 testeq(a + b, 8)
26 testeq(b - c, 15)
27 testeq(d - d, 0)
28 testeq(e + f, "99989999999999999989999999999999999999999999999999999999989999999999999999999999999998999998732349399771770598493296794622")
29 testeq(a * c, -60)
30 testeq(f * f, "1606938044258990275541962092341162602522202993782792835301376")
31 testeq(e * f, "-126752383516820657842978847503263945985032967946239999999987323493997717705985032967944972349399771770598503296781947493995182404784576509143246593589248")
32 testeq(a // b, 1)
33 testeq(a % b, 2)
34 testeq(f // d, "23336289836862896513258283")
35 testeq(f % d, "-14533")
36 testeq(e // -f, "78878201913048970415230130190415677050906625793723347950240237316957169209243093407705758276")
37 testeq(e % -f, "1011644662020502370048160308222")
38 testeq(c ^ a, -248832)
39 testeq(d ^ a, "-472975648731213834575601")
40 testeq(a == b, false)
41 testeq(b < a, true)
42 testeq(a <= a, true)
43 testeq(f < d, true)
44 testeq(e <= f, false)
45 endtest()
46
47
48
49 starttest("integer conversions")
50 testeq(a / b, "5/3")
51 testeq(g / c, "-4/3")
52 testeq(c / b, -4)
53 endtest()
54
55
56 starttest("rational operations")
57 testeq(-x, "-8/5")
58 testeq(x + y, "101/60")
59 testeq(z - y, "-47/60")
60 testeq(x * z, "-28/25")
61 testeq(x / y, "96/5")
62 testeq(y<x, true)
63 testeq(z<z, false)
64 testeq(z<=z, true)
65 endtest()
66
67 starttest("combined integer/rational operations")
68 testeq(a + x, "33/5")
69 testeq(x + a, "33/5")
70 testeq(b - y, "35/12")
71 testeq(y - b, "-35/12")
72 testeq(c * y, -1)
73 testeq(y * c, -1)
74 testeq(a / x, "25/8")
75 testeq(x / a, "8/25")
76 testeq(a/h == x, false)
77 testeq(h/a == x, true)
78 testeq(y < b , true)
79 testeq(b < y, false)
80 endtest()
81
82 local f = Integer(3)
83 local g = Integer(216)
84 local h = Integer(945)
85 local i = Integer("7766999")
86 local j = Integer(4)
87 local k = Integer(8)
88 local m = Integer(16)
89 local n = Integer(100000000003)
90 local o = Integer(200250077)
91
92 starttest("Miller-Rabin Primes")
93 testeq(f:isprime(), true, f)
94 testeq(g:isprime(), false, g)
95 testeq(h:isprime(), false, h)
96 testeq(i:isprime(), false, i)
97 testeq(n:isprime(), true, n)
98 testeq(o:isprime(), false, o)
99 endtest()
100
101
102 starttest("Pollard Rho algorithm")
103 testeq(f:findafactor(), 3, f)
104 testeq(g:findafactor(), 2, g)
105 testeq(h:findafactor(), 3, h)
106 testeq(i:findafactor(), 41, i)
107 testeq(j:findafactor(), 2, j)
108 testeq(k:findafactor(), 2, k)
109 testeq(m:findafactor(), 2, m)
110 endtest()
111
112 starttest("prime factorization")
113 testeq(f:primefactorization(), "* (3 ^ 1)", f, true)
114 testeq(g:primefactorization(), "(2 ^ 3) * (3 ^ 3)", g, true)
115 testeq(h:primefactorization(), "(3 ^ 3) * (5 ^ 1) * (7 ^ 1)", h, true)
116 testeq(i:primefactorization(), "(41 ^ 1) * (189439 ^ 1)", i, true)
117 testeq(o:primefactorization(), "(10007 ^ 1) * (20011 ^ 1)", o, true)
118 endtest()