4
ctypedef long long int128_t "__int128_t"
5
ctypedef unsigned long long uint128_t "__uint128_t"
9
print(str(x).rstrip('L'))
12
def unsigned_conversion(x):
14
>>> bigint(unsigned_conversion(0))
16
>>> bigint(unsigned_conversion(2))
19
>>> unsigned_conversion(-2) # doctest: +ELLIPSIS
20
Traceback (most recent call last):
21
OverflowError: can't convert negative value to ...uint128_t
22
>>> unsigned_conversion(-2**120) # doctest: +ELLIPSIS
23
Traceback (most recent call last):
24
OverflowError: can't convert negative value to ...uint128_t
25
>>> unsigned_conversion(-2**127) # doctest: +ELLIPSIS
26
Traceback (most recent call last):
27
OverflowError: can't convert negative value to ...uint128_t
28
>>> unsigned_conversion(-2**128) # doctest: +ELLIPSIS
29
Traceback (most recent call last):
30
OverflowError: can't convert negative value to ...uint128_t
32
>>> bigint(unsigned_conversion(2**20))
34
>>> bigint(unsigned_conversion(2**30-1))
36
>>> bigint(unsigned_conversion(2**30))
38
>>> bigint(unsigned_conversion(2**30+1))
43
>>> bigint(unsigned_conversion(2**60-1))
45
>>> bigint(unsigned_conversion(2**60))
47
>>> bigint(unsigned_conversion(2**60+1))
51
>>> bigint(unsigned_conversion(2**64))
55
1329227995784915872903807060280344576
56
>>> bigint(unsigned_conversion(2**120))
57
1329227995784915872903807060280344576
59
340282366920938463463374607431768211455
60
>>> bigint(unsigned_conversion(2**128-1))
61
340282366920938463463374607431768211455
62
>>> bigint(unsigned_conversion(2**128)) # doctest: +ELLIPSIS
63
Traceback (most recent call last):
64
OverflowError: ... to convert...
65
>>> bigint(unsigned_conversion(2**128+1)) # doctest: +ELLIPSIS
66
Traceback (most recent call last):
67
OverflowError: ... to convert...
68
>>> bigint(unsigned_conversion(2**129-1)) # doctest: +ELLIPSIS
69
Traceback (most recent call last):
70
OverflowError: ... to convert...
71
>>> bigint(unsigned_conversion(2**129)) # doctest: +ELLIPSIS
72
Traceback (most recent call last):
73
OverflowError: ... to convert...
79
def signed_conversion(x):
81
>>> bigint(signed_conversion(0))
83
>>> bigint(signed_conversion(2))
85
>>> bigint(signed_conversion(-2))
88
>>> bigint(signed_conversion(2**20))
90
>>> bigint(signed_conversion(2**32))
94
>>> bigint(signed_conversion(2**64))
96
>>> bigint(signed_conversion(-2**64))
100
332306998946228968225951765070086144
101
>>> bigint(signed_conversion(2**118))
102
332306998946228968225951765070086144
103
>>> bigint(signed_conversion(-2**118))
104
-332306998946228968225951765070086144
107
1329227995784915872903807060280344576
108
>>> bigint(signed_conversion(2**120))
109
1329227995784915872903807060280344576
110
>>> bigint(signed_conversion(-2**120))
111
-1329227995784915872903807060280344576
114
170141183460469231731687303715884105727
115
>>> bigint(signed_conversion(2**127-2))
116
170141183460469231731687303715884105726
117
>>> bigint(signed_conversion(2**127-1))
118
170141183460469231731687303715884105727
119
>>> bigint(signed_conversion(2**127)) # doctest: +ELLIPSIS
120
Traceback (most recent call last):
121
OverflowError: ... to convert...
122
>>> bigint(signed_conversion(-2**127+1))
123
-170141183460469231731687303715884105727
124
>>> bigint(signed_conversion(-2**127))
125
-170141183460469231731687303715884105728
126
>>> bigint(signed_conversion(-2**127-1)) # doctest: +ELLIPSIS
127
Traceback (most recent call last):
128
OverflowError: ... to convert...
129
>>> bigint(signed_conversion(-2**127-2)) # doctest: +ELLIPSIS
130
Traceback (most recent call last):
131
OverflowError: ... to convert...
132
>>> bigint(signed_conversion(-2**128+1)) # doctest: +ELLIPSIS
133
Traceback (most recent call last):
134
OverflowError: ... to convert...
135
>>> bigint(signed_conversion(-2**128)) # doctest: +ELLIPSIS
136
Traceback (most recent call last):
137
OverflowError: ... to convert...
143
def get_int_distribution(shuffle=True):
145
>>> L = get_int_distribution()
148
>>> bigint(L[ len(L) // 2 ])
151
52818775009509558395695966805
155
# Large integers that cover 1-4 (30 bits) or 1-7 (15 bits) PyLong digits.
156
# Uses only integer calculations to avoid rounding issues.
157
pow2 = [2**exp for exp in range(98)]
160
for i in range(11, len(pow2) - 1)
161
# Take a low but growing number of integers from each power-of-2 range.
162
for n in range(pow2[i], pow2[i+1], pow2[i - 8] - 1)
164
return ints * 3 # longer list, but keeps median in the middle
169
>>> L = get_int_distribution()
170
>>> bigint(intsum(L))
171
61084913298497804284622382871263
173
61084913298497804284622382871263
175
>>> from random import shuffle
177
>>> bigint(intsum(L))
178
61084913298497804284622382871263
180
cdef uint128_t i, x = 0
188
>>> L = get_int_distribution()
189
>>> bigint(intxor(L))
190
31773794341658093722410838161
191
>>> bigint(intxor(L * 2))
194
>>> from functools import reduce
195
>>> bigint(reduce(operator.xor, L))
196
31773794341658093722410838161
197
>>> bigint(reduce(operator.xor, L * 2))
200
>>> from random import shuffle
202
>>> bigint(intxor(L))
203
31773794341658093722410838161
204
>>> bigint(intxor(L * 2))
207
cdef uint128_t i, x = 0