This repository has been archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.py
336 lines (259 loc) · 7.96 KB
/
sample.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# -*- coding: utf-8 -*-
from __future__ import print_function
import src as ntlib
print()
# Create an instacne of Polynomial.
print('Create Polynomial.')
poly_0 = ntlib.Polynomial(('b', (2, -1), (0, 1)), ('c', (7, 1), (1, -1)))
poly_1 = ntlib.Polynomial(('a', (1, 3), (3, 4), (2, 2), (34, (1+3j))))
poly_2 = ntlib.Polynomial(('a', (1, 0), (4, -4), (2, 3), (0, 1)))
poly_3 = poly_1 / poly_2
print(poly_0)
print(poly_1[::])
print(poly_3)
print()
# Create an instacne of Congruence.
print('Create Congruence.')
_con = ntlib.Congruence(('z', (2, 1), (0, -46)), mod=105)
_ret = _con.solution
print('The solution of %s is\n' % str(_con))
print('\t', _ret)
print()
# Create an instacne of Quadratic.
print('Create Quadratic.')
_qua = ntlib.Quadratic(8068, vars=('p', 'q'))
_rst = _qua.solution
print('The solution of %s is\n' % str(_qua))
print('\t', _rst)
print()
# Create an instacne of Index.
print('Create Index.')
index = ntlib.Index(41)
print(index)
print()
rst = index(4, 8)
print('4 * 8 ≡ %d' % rst)
print()
# Create an instacne of Legendre.
print('Create Legendre.')
l1 = ntlib.Legendre(3, 17)
l2 = ntlib.Legendre('3|17')
l3 = ntlib.Legendre(l1)
print(l1, l1.eval())
print(l2, l2.simplify())
print(l3, l3.reciprocate())
print()
# Create an instacne of Jacobi.
print('Create Jacobi.')
j1 = ntlib.Jacobi(47, 359)
j2 = ntlib.Jacobi('47|359')
j3 = ntlib.Jacobi(l1)
print(j1, j1.eval())
print(j2, j2.simplify())
print(j3, j3.reciprocate())
print()
# Create an instacne of Fraction.
print('Create Fraction.')
print('7700/2145 = ', end=' ')
rst_ = ntlib.Fraction('7699/2145')
dst_ = ntlib.Fraction(1, 2145)
print(rst_ + dst_)
print()
# Print all primes numbers less than 97.
print('Call primelist.')
print(ntlib.primelist(97, -1))
print()
# Check if 13 and 20 are divisible.
print('Call isdivisible.')
if ntlib.isdivisible(13, 20):
print('The result is 13|20.')
else:
print('The result is 13∤20.')
print()
# Check if 197 is prime.
print('Call isprime.')
if ntlib.isprime(197):
print('197 is a prime number.')
else:
print('197 is a composit number.')
print()
# Print the GCD of 10 and 24.
print('Call gcd.')
print('(10,24) = %d' % ntlib.gcd(10, 24))
print()
# Check if 157 and 673 are coprime numbers.
print('Call coprime.')
if ntlib.coprime(157, 673):
print('157 and 673 are coprime.')
else:
print('157 and 673 are not coprime.')
print()
# Print the Bézout Equation with parameters 179 and 367.
print('Call bezout.')
print('%d*179 + %d*367 = (179,367)' % ntlib.bezout(179, 367))
print()
# Print the factorisation of -234.
print('Call factor.')
print('-100 =', end=' ')
ctr = 0
for prime in ntlib.factor(-100):
if ctr > 0:
print('*', end=' ')
print(prime, end=' ')
ctr += 1
print()
print()
# Print the solutions for N|a^2-b^2 while N∤a+b and N∤a-b.
print('Call decomposit.')
print('The solutions for N|a^2-b^2 while N∤a+b and N∤a-b\
is\n\ta = %d\n\tb = %d' % ntlib.decomposit(100))
print()
# Return the special solutions for indefinite binary equation, 7*x + 24*y = -3.
print('Call binary.')
print('The general solutions for \'7*x + 24*y = -3\' is (t∈Z)\
\n\tx = %d + 24*t\n\ty = %d - 7*t' % ntlib.binary(7, 24, -3))
print()
# Print the result of 235^12235 (mod 9).
print('Call modulo.')
print('235 ^ 12235 ≡ %d (mod 9)\n' % ntlib.modulo(235, 12235, 9))
print()
# Return the Euclidean division result of
# (20140515x^20140515 + 201495x^201405 + 2014x^2014
# + 8x^8 + x^6 + 4x^3 + x + 1) / (x^7 - 1).
print('Call polydiv.')
dvdExp = [1, 3, 2, 34]
dvdCoe = [-2, 4, 3, 1]
dvsExp = [7, 1]
dvsCoe = [1, -1]
(qttExp, qttCoe, rtoExp, rtoCoe) = ntlib.polydiv(
dvdExp, dvdCoe, dvsExp, dvsCoe)
print('\n\t')
for ptr in range(len(dvdExp)):
print('%dx^%d' % (dvdCoe[ptr], dvdExp[ptr]), end=' ')
if ptr < len(dvdExp) - 1:
print('+', end=' ')
print('=\n(', end=' ')
for ptr in range(len(dvsExp)):
print('%dx^%d' % (dvsCoe[ptr], dvsExp[ptr]), end=' ')
if ptr < len(dvsExp) - 1:
print('+', end=' ')
print(') * (', end=' ')
for ptr in range(len(qttExp)):
print('%dx^%d' % (qttCoe[ptr], qttExp[ptr]), end=' ')
if ptr < len(qttExp) - 1:
print('+', end=' ')
print(') +', end=' ')
for ptr in range(len(rtoExp)):
print('%dx^%d' % (rtoCoe[ptr], rtoExp[ptr]), end=' ')
if ptr < len(rtoExp) - 1:
print('+', end=' ')
print()
print()
# Return the coefficients and exponents of quotient and remainder
# after congruence simplification of
# 3x^14 + 4x^13 + 2x^11 + x^9 + x^6 + x^3 + 12x^2 + x ≡ 0 (mod 5).
print('Call simplify.')
cgcExp = [14, 13, 11, 9, 6, 3, 2, 1]
cgcCoe = [3, 4, 2, 1, 1, 1, 12, 1]
modulo = 5
(rtoExp, rtoCoe) = ntlib.simplify(cgcExp, cgcCoe, modulo)
print('The original polynomial congruence is\n\t', end=' ')
for ptr in range(len(cgcExp)):
print('%dx^%d' % (cgcCoe[ptr], cgcExp[ptr]), end=' ')
if ptr < len(cgcExp) - 1:
print('+', end=' ')
print('≡ 0 (mod %d)' % modulo)
print()
print('The simplified polynomial congruence is\n\t', end=' ')
for ptr in range(len(rtoExp)):
print('%dx^%d' % (rtoCoe[ptr], rtoExp[ptr]), end=' ')
if ptr < len(rtoExp) - 1:
print('+', end=' ')
print('≡ 0 (mod %d)' % modulo)
print()
# Return the solutions of a naïve congruence set.
print('Call crt')
remainder = ntlib.crt((3, [1, -1]), (5, [1, -1]), (7, [2, -2]))
print('x ≡ ±1 (mod 3)')
print('x ≡ ±1 (mod 5)')
print('x ≡ ±2 (mod 7)')
print('The solutions of the above equation set is\n\tx ≡', end=' ')
for rst in remainder:
print(rst, end=' ')
print('(mod 105)')
print()
# Return the solutions of congruence equation x^2 ≡ 46 (mod 105).
print('Call congsolve')
cgcExp = [2, 0]
cgcCoe = [1, -46]
modulo = 105
for ptr in range(len(cgcExp)):
print('%dx^%d' % (cgcCoe[ptr], cgcExp[ptr]), end=' ')
if ptr < len(cgcExp) - 1:
print('+', end=' ')
print('≡ 0 (mod %d)' % modulo)
remainder = ntlib.congsolve(cgcExp, cgcCoe, modulo)
print('The solution of the above polynomial congruence is\n\tx ≡', end=' ')
for rst in remainder:
print('%d' % rst, end=' ')
print('(mod %d)' % modulo)
print()
# Return the solutions of quadratic equation x^2 + y^2 = 2017.
print('Call quadratic.')
print('The solution of the equation x^2 + y^2 = 2017 is\
\n\tx=±%d, y=±%d' % ntlib.quadratic(2017))
print()
# Return the order of 2 for modulo 9.
print('Call ord.')
print('The order of 2 mod 9 is\n\tord_9(2) = %d' % ntlib.ord(9, 2))
print()
# Return Euler function φ(40)
print('Call euler.')
print('φ(40) = %d' % ntlib.euler(40))
print()
# Return the primitive residue class of integer 40.
print('Call prc')
print('The primitive residue class of 40 is')
for num in ntlib.prc(40):
print(num, end=' ')
print('.')
print()
# Return the primitive root(s) of modulo 7.
print('Call root.')
print('The primtive root(s) of modulo 7 is/are', end=' ')
for root in ntlib.root(7):
print(root, end=' ')
print('.')
print()
# Return the result of Legendre symbol for (3 | 17).
print('Call legendre.')
print('(3 | 17) = %d' % ntlib.legendre(3, 17))
print()
# Return the result of jocabi symbol for (47 | 359).
print('Call jacobi.')
print('(47 | 359) = %d' % ntlib.jacobi(47, 359))
print()
# Return if an integer is a Carmicheal number.
print('Call carmicheal.')
print('3499', end=' ')
if ntlib.carmicheal(3499):
print('is', end=' ')
else:
print('isn\'t', end=' ')
print('a Carmicheal number.')
print()
# Return a pseudo-prime number with certain paterns.
print('Call pseudo.')
print('A pseudo-prime number with Fermat test (t=100000) is %d.'
% ntlib.pseudo(byte=16, para=10000, mode='Fermat', flag=False))
print('An Euler pseudo-prime number with Solovay-Stassen test (t=100000) is %d.'
% ntlib.pseudo(byte=16, para=10000, mode='Euler'))
print('A strong pseudo-prime number with Miller-Rabin test (k=100000) is %d.'
% ntlib.pseudo(byte=16, para=10000, mode='Strong'))
print()
# Return the continued fraction of a number.
print('Call fraction.')
fct_ = __import__('fractions').Fraction(7700, 2145)
rst_ = ntlib.fraction(fct_)
print(fct_, '=', rst_)
print()