-
Notifications
You must be signed in to change notification settings - Fork 8
/
gmpy2_mpz_misc.c
1792 lines (1500 loc) · 48.1 KB
/
gmpy2_mpz_misc.c
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* gmpy2_mpz_misc.c *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Python interface to the GMP or MPIR, MPFR, and MPC multiple precision *
* libraries. *
* *
* Copyright 2000 - 2009 Alex Martelli *
* *
* Copyright 2008 - 2022 Case Van Horsen *
* *
* This file is part of GMPY2. *
* *
* GMPY2 is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation, either version 3 of the License, or (at your *
* option) any later version. *
* *
* GMPY2 is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public *
* License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with GMPY2; if not, see <http://www.gnu.org/licenses/> *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* return number-of-digits for an mpz in requested base, default 10 */
PyDoc_STRVAR(GMPy_doc_mpz_method_num_digits,
"x.num_digits([base]) -> int\n\n"
"Return length of string representing the absolute value of x in\n"
"the given base. Values for base can range between 2 and 62. The\n"
"value returned may be 1 too large.");
PyDoc_STRVAR(GMPy_doc_mpz_function_num_digits,
"num_digits(x[, base]) -> int\n\n"
"Return length of string representing the absolute value of x in\n"
"the given base. Values for base can range between 2 and 62. The\n"
"value returned may be 1 too large.");
static PyObject *
GMPy_MPZ_Method_NumDigits(PyObject *self, PyObject *args)
{
long base = 10;
PyObject *result;
if (PyTuple_GET_SIZE(args) == 1) {
base = PyIntOrLong_AsLong(PyTuple_GET_ITEM(args, 0));
if (base == -1 && PyErr_Occurred()) {
return NULL;
}
}
if ((base < 2) || (base > 62)) {
VALUE_ERROR("base must be in the interval [2, 62]");
return NULL;
}
result = PyIntOrLong_FromSize_t(mpz_sizeinbase(MPZ(self), (int)base));
return result;
}
static PyObject *
GMPy_MPZ_Function_NumDigits(PyObject *self, PyObject *args)
{
long base = 10;
Py_ssize_t argc;
MPZ_Object *temp;
PyObject *result;
argc = PyTuple_GET_SIZE(args);
if (argc == 0 || argc > 2) {
TYPE_ERROR("num_digits() requires 'mpz',['int'] arguments");
return NULL;
}
if (argc == 2) {
base = PyIntOrLong_AsLong(PyTuple_GET_ITEM(args, 1));
if (base == -1 && PyErr_Occurred()) {
return NULL;
}
}
if ((base < 2) || (base > 62)) {
VALUE_ERROR("base must be in the interval [2, 62]");
return NULL;
}
if (!(temp = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 0), NULL))) {
return NULL;
}
result = PyIntOrLong_FromSize_t(mpz_sizeinbase(temp->z, (int)base));
Py_DECREF((PyObject*)temp);
return result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_iroot,
"iroot(x,n) -> (number, boolean)\n\n"
"Return the integer n-th root of x and boolean value that is True\n"
"iff the root is exact. x >= 0. n > 0.");
static PyObject *
GMPy_MPZ_Function_Iroot(PyObject *self, PyObject *args)
{
unsigned long n;
int exact;
MPZ_Object *root = NULL, *tempx = NULL;
PyObject *result = NULL;
if ((PyTuple_GET_SIZE(args) != 2) ||
((!IS_INTEGER(PyTuple_GET_ITEM(args, 0))) ||
(!IS_INTEGER(PyTuple_GET_ITEM(args, 1))))) {
TYPE_ERROR("iroot() requires 'int','int' arguments");
return NULL;
}
n = GMPy_Integer_AsUnsignedLong_v2(PyTuple_GET_ITEM(args, 1));
if ((n == 0) || ((n == (unsigned long)(-1)) && PyErr_Occurred())) {
VALUE_ERROR("n must be > 0");
return NULL;
}
if (!(tempx = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 0), NULL))) {
/* LCOV_EXCL_START */
return NULL;
/* LCOV_EXCL_STOP */
}
if (mpz_sgn(tempx->z) < 0) {
VALUE_ERROR("iroot() of negative number");
Py_DECREF((PyObject*)tempx);
return NULL;
}
if (!(result = PyTuple_New(2)) ||
!(root = GMPy_MPZ_New(NULL))) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)tempx);
Py_XDECREF((PyObject*)root);
Py_XDECREF(result);
return NULL;
/* LCOV_EXCL_STOP */
}
exact = mpz_root(root->z, tempx->z, n);
Py_DECREF((PyObject*)tempx);
PyTuple_SET_ITEM(result, 0, (PyObject*)root);
PyTuple_SET_ITEM(result, 1, (PyObject*)PyBool_FromLong(exact));
return result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_iroot_rem,
"iroot_rem(x,n) -> (number, number)\n\n"
"Return a 2-element tuple (y,r), such that y is the integer n-th\n"
"root of x and x=y**n + r. x >= 0. n > 0.");
static PyObject *
GMPy_MPZ_Function_IrootRem(PyObject *self, PyObject *args)
{
unsigned long n;
MPZ_Object *root = NULL, *rem = NULL, *tempx = NULL;
PyObject *result = NULL;
if ((PyTuple_GET_SIZE(args) != 2) ||
((!IS_INTEGER(PyTuple_GET_ITEM(args, 0))) ||
(!IS_INTEGER(PyTuple_GET_ITEM(args, 1))))) {
TYPE_ERROR("iroot_rem() requires 'int','int' arguments");
return NULL;
}
n = GMPy_Integer_AsUnsignedLong(PyTuple_GET_ITEM(args, 1));
if ((n == 0) || ((n == (unsigned long)(-1)) && PyErr_Occurred())) {
VALUE_ERROR("n must be > 0");
return NULL;
}
if (!(tempx = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 0), NULL))) {
/* LCOV_EXCL_START */
return NULL;
/* LCOV_EXCL_STOP */
}
if (mpz_sgn(tempx->z) < 0) {
VALUE_ERROR("iroot_rem() of negative number");
Py_DECREF((PyObject*)tempx);
return NULL;
}
if (!(result = PyTuple_New(2)) ||
!(root = GMPy_MPZ_New(NULL)) ||
!(rem = GMPy_MPZ_New(NULL))) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)tempx);
Py_XDECREF(result);
Py_XDECREF((PyObject*)root);
Py_XDECREF((PyObject*)rem);
return NULL;
/* LCOV_EXCL_STOP */
}
mpz_rootrem(root->z, rem->z, tempx->z, n);
Py_DECREF((PyObject*)tempx);
PyTuple_SET_ITEM(result, 0, (PyObject*)root);
PyTuple_SET_ITEM(result, 1, (PyObject*)rem);
return result;
}
PyDoc_STRVAR(GMPy_doc_mpz_method_ceil, "Ceiling of an mpz returns itself.");
static PyObject *
GMPy_MPZ_Method_Ceil(PyObject *self, PyObject *other)
{
Py_INCREF(self);
return self;
}
PyDoc_STRVAR(GMPy_doc_mpz_method_floor, "Floor of an mpz returns itself.");
static PyObject *
GMPy_MPZ_Method_Floor(PyObject *self, PyObject *other)
{
Py_INCREF(self);
return self;
}
PyDoc_STRVAR(GMPy_doc_mpz_method_trunc, "Truncating an mpz returns itself.");
static PyObject *
GMPy_MPZ_Method_Trunc(PyObject *self, PyObject *other)
{
Py_INCREF(self);
return self;
}
PyDoc_STRVAR(GMPy_doc_mpz_method_round, "Round an mpz to power of 10.");
static PyObject *
GMPy_MPZ_Method_Round(PyObject *self, PyObject *args)
{
Py_ssize_t round_digits;
MPZ_Object *result;
mpz_t temp, rem;
if (PyTuple_GET_SIZE(args) == 0) {
Py_INCREF(self);
return self;
}
round_digits = GMPy_Integer_AsSsize_t(PyTuple_GET_ITEM(args, 0));
if (round_digits == -1 && PyErr_Occurred()) {
TYPE_ERROR("__round__() requires 'int' argument");
return NULL;
}
if (round_digits >= 0) {
Py_INCREF(self);
return self;
}
/* We can now assume round_digits > 0. */
round_digits = -round_digits;
if ((result = GMPy_MPZ_New(NULL))) {
if ((unsigned)round_digits >= mpz_sizeinbase(MPZ(self), 10)) {
mpz_set_ui(result->z, 0);
}
else {
mpz_init(temp);
mpz_init(rem);
mpz_ui_pow_ui(temp, 10, round_digits);
mpz_fdiv_qr(result->z, rem, MPZ(self), temp);
mpz_mul_2exp(rem, rem, 1);
if (mpz_cmp(rem, temp) > 0) {
mpz_add_ui(result->z, result->z, 1);
}
else if (mpz_cmp(rem, temp) == 0) {
if (mpz_odd_p(result->z)) {
mpz_add_ui(result->z, result->z, 1);
}
}
mpz_mul(result->z, result->z, temp);
mpz_clear(rem);
mpz_clear(temp);
}
}
return (PyObject*)result;
}
static int
GMPy_MPZ_NonZero_Slot(MPZ_Object *self)
{
return mpz_sgn(self->z) != 0;
}
#if PY_MAJOR_VERSION < 3
/* hex/oct formatting (mpz-only) */
static PyObject *
GMPy_MPZ_Oct_Slot(MPZ_Object *self)
{
return GMPy_PyStr_From_MPZ(self, 8, 0, NULL);
}
static PyObject *
GMPy_MPZ_Hex_Slot(MPZ_Object *self)
{
return GMPy_PyStr_From_MPZ(self, 16, 0, NULL);
}
#endif
/* Miscellaneous gmpy functions */
PyDoc_STRVAR(GMPy_doc_mpz_function_gcd,
"gcd(*integers) -> mpz\n\n"
"Return the greatest common divisor of integers.");
static PyObject *
GMPy_MPZ_Function_GCD(PyObject *self, PyObject *args)
{
PyObject *arg;
MPZ_Object *result = NULL, *tempa = NULL;
CTXT_Object *context = NULL;
Py_ssize_t i, nargs;
CHECK_CONTEXT(context);
if (!(result = GMPy_MPZ_New(NULL))) {
/* LCOV_EXCL_START */
return NULL;
/* LCOV_EXCL_STOP */
}
nargs = PyTuple_Size(args);
for (i = 0; i < nargs; i++) {
arg = PyTuple_GET_ITEM(args, i);
if MPZ_Check(arg) {
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_gcd(result->z, MPZ(arg), result->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);\
} else {
if (!(tempa = GMPy_MPZ_From_Integer(arg, NULL))) {
TYPE_ERROR("gcd() requires 'mpz' arguments");
Py_XDECREF((PyObject*)tempa);
Py_DECREF((PyObject*)result);
return NULL;
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_gcd(result->z, tempa->z, result->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Py_DECREF((PyObject*)tempa);
}
}
return (PyObject*)result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_lcm,
"lcm(*integers) -> mpz\n\n"
"Return the lowest common multiple of integers.");
static PyObject *
GMPy_MPZ_Function_LCM(PyObject *self, PyObject *args)
{
PyObject *arg;
MPZ_Object *result = NULL, *tempa = NULL;
CTXT_Object *context = NULL;
Py_ssize_t i, nargs;
CHECK_CONTEXT(context);
if (!(result = GMPy_MPZ_New(NULL))) {
/* LCOV_EXCL_START */
return NULL;
/* LCOV_EXCL_STOP */
}
mpz_set_ui(result->z, 1);
nargs = PyTuple_Size(args);
for (i = 0; i < nargs; i++) {
arg = PyTuple_GET_ITEM(args, i);
if MPZ_Check(arg) {
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_lcm(result->z, MPZ(arg), result->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
} else {
if (!(tempa = GMPy_MPZ_From_Integer(arg, NULL))) {
TYPE_ERROR("lcm() requires 'mpz' arguments");
Py_XDECREF((PyObject*)tempa);
Py_DECREF((PyObject*)result);
return NULL;
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_lcm(result->z, tempa->z, result->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Py_DECREF((PyObject*)tempa);
}
}
return (PyObject*)result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_gcdext,
"gcdext(a, b) - > tuple\n\n"
"Return a 3-element tuple (g,s,t) such that\n"
" g == gcd(a,b) and g == a*s + b*t");
static PyObject *
GMPy_MPZ_Function_GCDext(PyObject *self, PyObject *args)
{
PyObject *arg0, *arg1, *result = NULL;
MPZ_Object *g = NULL, *s = NULL, *t = NULL, *tempa = NULL, *tempb = NULL;
CTXT_Object *context = NULL;
CHECK_CONTEXT(context);
if(PyTuple_GET_SIZE(args) != 2) {
TYPE_ERROR("gcdext() requires 'mpz','mpz' arguments");
return NULL;
}
if (!(result = PyTuple_New(3)) ||
!(g = GMPy_MPZ_New(NULL)) ||
!(s = GMPy_MPZ_New(NULL)) ||
!(t = GMPy_MPZ_New(NULL))) {
/* LCOV_EXCL_START */
Py_XDECREF((PyObject*)g);
Py_XDECREF((PyObject*)s);
Py_XDECREF((PyObject*)t);
Py_XDECREF(result);
return NULL;
/* LCOV_EXCL_STOP */
}
arg0 = PyTuple_GET_ITEM(args, 0);
arg1 = PyTuple_GET_ITEM(args, 1);
if (MPZ_Check(arg0) && MPZ_Check(arg1)) {
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_gcdext(g->z, s->z, t->z, MPZ(arg0), MPZ(arg1));
GMPY_MAYBE_END_ALLOW_THREADS(context);
}
else {
if(!(tempa = GMPy_MPZ_From_Integer(arg0, NULL)) ||
!(tempb = GMPy_MPZ_From_Integer(arg1, NULL))) {
TYPE_ERROR("gcdext() requires 'mpz','mpz' arguments");
Py_XDECREF((PyObject*)tempa);
Py_XDECREF((PyObject*)tempb);
Py_DECREF((PyObject*)g);
Py_DECREF((PyObject*)s);
Py_DECREF((PyObject*)t);
Py_DECREF(result);
return NULL;
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_gcdext(g->z, s->z, t->z, tempa->z, tempb->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Py_DECREF((PyObject*)tempa);
Py_DECREF((PyObject*)tempb);
}
PyTuple_SET_ITEM(result, 0, (PyObject*)g);
PyTuple_SET_ITEM(result, 1, (PyObject*)s);
PyTuple_SET_ITEM(result, 2, (PyObject*)t);
return result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_divm,
"divm(a, b, m) -> mpz\n\n"
"Return x such that b*x == a mod m. Raises a ZeroDivisionError\n"
"exception if no such value x exists.");
static PyObject *
GMPy_MPZ_Function_Divm(PyObject *self, PyObject *args)
{
MPZ_Object *result = NULL, *num = NULL, *den = NULL, *mod = NULL;
mpz_t numz, denz, modz, gcdz;
int ok = 0;
CTXT_Object *context = NULL;
CHECK_CONTEXT(context);
if (PyTuple_GET_SIZE(args) != 3) {
TYPE_ERROR("divm() requires 'mpz','mpz','mpz' arguments");
return NULL;
}
if (!(result = GMPy_MPZ_New(NULL))) {
/* LCOV_EXCL_START */
return NULL;
/* LCOV_EXCL_STOP */
}
if (!(num = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 0), NULL)) ||
!(den = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 1), NULL)) ||
!(mod = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 2), NULL))) {
TYPE_ERROR("divm() requires 'mpz','mpz','mpz' arguments");
Py_XDECREF((PyObject*)num);
Py_XDECREF((PyObject*)den);
Py_XDECREF((PyObject*)mod);
Py_DECREF((PyObject*)result);
return NULL;
}
/* Make copies so we don't destroy the input. */
mpz_init(numz);
mpz_init(denz);
mpz_init(modz);
mpz_set(numz, num->z);
mpz_set(denz, den->z);
mpz_set(modz, mod->z);
Py_DECREF((PyObject*)num);
Py_DECREF((PyObject*)den);
Py_DECREF((PyObject*)mod);
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
ok = mpz_invert(result->z, denz, modz);
GMPY_MAYBE_END_ALLOW_THREADS(context);
if (!ok) {
/* last-ditch attempt: do num, den AND mod have a gcd>1 ? */
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_init(gcdz);
mpz_gcd(gcdz, numz, denz);
mpz_gcd(gcdz, gcdz, modz);
mpz_divexact(numz, numz, gcdz);
mpz_divexact(denz, denz, gcdz);
mpz_divexact(modz, modz, gcdz);
mpz_clear(gcdz);
ok = mpz_invert(result->z, denz, modz);
GMPY_MAYBE_END_ALLOW_THREADS(context);
}
if (ok) {
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_mul(result->z, result->z, numz);
mpz_mod(result->z, result->z, modz);
mpz_clear(numz);
mpz_clear(denz);
mpz_clear(modz);
GMPY_MAYBE_END_ALLOW_THREADS(context);
return (PyObject*)result;
}
else {
ZERO_ERROR("not invertible");
mpz_clear(numz);
mpz_clear(denz);
mpz_clear(modz);
Py_DECREF((PyObject*)result);
return NULL;
}
}
PyDoc_STRVAR(GMPy_doc_mpz_function_fac,
"fac(n) -> mpz\n\n"
"Return the exact factorial of n.\n\n"
"See factorial(n) to get the floating-point approximation.");
static PyObject *
GMPy_MPZ_Function_Fac(PyObject *self, PyObject *other)
{
MPZ_Object *result = NULL;
unsigned long n;
n = GMPy_Integer_AsUnsignedLong(other);
if (n == (unsigned long)(-1) && PyErr_Occurred()) {
return NULL;
}
if ((result = GMPy_MPZ_New(NULL))) {
mpz_fac_ui(result->z, n);
}
return (PyObject*)result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_double_fac,
"double_fac(n) -> mpz\n\n"
"Return the exact double factorial (n!!) of n. The double\n"
"factorial is defined as n*(n-2)*(n-4)...");
static PyObject *
GMPy_MPZ_Function_DoubleFac(PyObject *self, PyObject *other)
{
MPZ_Object *result = NULL;
unsigned long n;
n = GMPy_Integer_AsUnsignedLong(other);
if (n == (unsigned long)(-1) && PyErr_Occurred()) {
return NULL;
}
if ((result = GMPy_MPZ_New(NULL))) {
mpz_2fac_ui(result->z, n);
}
return (PyObject*)result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_primorial,
"primorial(n) -> mpz\n\n"
"Return the product of all positive prime numbers less than or"
"equal to n.");
static PyObject *
GMPy_MPZ_Function_Primorial(PyObject *self, PyObject *other)
{
MPZ_Object *result = NULL;
unsigned long n;
n = GMPy_Integer_AsUnsignedLong(other);
if (n == (unsigned long)(-1) && PyErr_Occurred()) {
return NULL;
}
if ((result = GMPy_MPZ_New(NULL))) {
mpz_primorial_ui(result->z, n);
}
return (PyObject*)result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_multi_fac,
"multi_fac(n,m) -> mpz\n\n"
"Return the exact m-multi factorial of n. The m-multi"
"factorial is defined as n*(n-m)*(n-2m)...");
static PyObject *
GMPy_MPZ_Function_MultiFac(PyObject *self, PyObject *args)
{
MPZ_Object *result = NULL;
unsigned long n, m;
if (PyTuple_GET_SIZE(args) != 2) {
TYPE_ERROR("multi_fac() requires 2 integer arguments");
return NULL;
}
n = GMPy_Integer_AsUnsignedLong(PyTuple_GET_ITEM(args, 0));
if (n == (unsigned long)(-1) && PyErr_Occurred()) {
return NULL;
}
m = GMPy_Integer_AsUnsignedLong(PyTuple_GET_ITEM(args, 1));
if (m == (unsigned long)(-1) && PyErr_Occurred()) {
return NULL;
}
if ((result = GMPy_MPZ_New(NULL))) {
mpz_mfac_uiui(result->z, n, m);
}
return (PyObject*)result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_fib,
"fib(n) -> mpz\n\n"
"Return the n-th Fibonacci number.");
static PyObject *
GMPy_MPZ_Function_Fib(PyObject *self, PyObject *other)
{
MPZ_Object *result = NULL;
unsigned long n;
n = GMPy_Integer_AsUnsignedLong(other);
if (n == (unsigned long)(-1) && PyErr_Occurred()) {
return NULL;
}
if ((result = GMPy_MPZ_New(NULL))) {
mpz_fib_ui(result->z, n);
}
return (PyObject*)result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_fib2,
"fib2(n) -> tuple\n\n"
"Return a 2-tuple with the (n-1)-th and n-th Fibonacci numbers.");
static PyObject *
GMPy_MPZ_Function_Fib2(PyObject *self, PyObject *other)
{
PyObject *result = NULL;
MPZ_Object *fib1 = NULL, *fib2 = NULL;
unsigned long n;
n = GMPy_Integer_AsUnsignedLong(other);
if (n == (unsigned long)(-1) && PyErr_Occurred()) {
return NULL;
}
if (!(result = PyTuple_New(2)) ||
!(fib1 = GMPy_MPZ_New(NULL)) ||
!(fib2 = GMPy_MPZ_New(NULL))) {
/* LCOV_EXCL_START */
Py_XDECREF(result);
Py_XDECREF((PyObject*)fib1);
Py_XDECREF((PyObject*)fib2);
result = NULL;
/* LCOV_EXCL_STOP */
}
mpz_fib2_ui(fib1->z, fib2->z, n);
PyTuple_SET_ITEM(result, 0, (PyObject*)fib1);
PyTuple_SET_ITEM(result, 1, (PyObject*)fib2);
return (PyObject*)result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_lucas,
"lucas(n) -> mpz\n\n"
"Return the n-th Lucas number.");
static PyObject *
GMPy_MPZ_Function_Lucas(PyObject *self, PyObject *other)
{
MPZ_Object *result = NULL;
unsigned long n;
n = GMPy_Integer_AsUnsignedLong(other);
if (n == (unsigned long)(-1) && PyErr_Occurred()) {
return NULL;
}
if ((result = GMPy_MPZ_New(NULL))) {
mpz_lucnum_ui(result->z, n);
}
return (PyObject*)result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_lucas2,
"lucas2(n) -> tuple\n\n"
"Return a 2-tuple with the (n-1)-th and n-th Lucas numbers.");
static PyObject *
GMPy_MPZ_Function_Lucas2(PyObject *self, PyObject *other)
{
PyObject *result = NULL;
MPZ_Object *luc1 = NULL, *luc2 = NULL;
unsigned long n;
n = GMPy_Integer_AsUnsignedLong(other);
if (n == (unsigned long)(-1) && PyErr_Occurred()) {
return NULL;
}
if (!(result = PyTuple_New(2)) ||
!(luc1 = GMPy_MPZ_New(NULL)) ||
!(luc2 = GMPy_MPZ_New(NULL))) {
/* LCOV_EXCL_START */
Py_XDECREF(result);
Py_XDECREF((PyObject*)luc1);
Py_XDECREF((PyObject*)luc2);
result = NULL;
/* LCOV_EXCL_STOP */
}
mpz_lucnum2_ui(luc1->z, luc2->z, n);
PyTuple_SET_ITEM(result, 0, (PyObject*)luc1);
PyTuple_SET_ITEM(result, 1, (PyObject*)luc2);
return result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_bincoef,
"bincoef(n, k) -> mpz\n\n"
"Return the binomial coefficient ('n choose k'). k >= 0.");
PyDoc_STRVAR(GMPy_doc_mpz_function_comb,
"comb(n, k) -> mpz\n\n"
"Return the number of combinations of 'n things, taking k at a\n"
"time'. k >= 0. Same as bincoef(n, k)");
static PyObject *
GMPy_MPZ_Function_Bincoef(PyObject *self, PyObject *args)
{
MPZ_Object *result = NULL, *tempx;
unsigned long n, k;
if (PyTuple_GET_SIZE(args) != 2) {
TYPE_ERROR("bincoef() requires two integer arguments");
return NULL;
}
if (!(result = GMPy_MPZ_New(NULL))) {
/* LCOV_EXCL_START */
return NULL;
/* LCOV_EXCL_STOP */
}
k = GMPy_Integer_AsUnsignedLong(PyTuple_GET_ITEM(args, 1));
if (k == (unsigned long)(-1) && PyErr_Occurred()) {
Py_DECREF((PyObject*)result);
return NULL;
}
n = GMPy_Integer_AsUnsignedLong(PyTuple_GET_ITEM(args, 0));
if (n == (unsigned long)(-1) && PyErr_Occurred()) {
/* Since we plan to skip the else clause and continue,
* we need to clear the error since we aren't acting on it.
*/
PyErr_Clear();
}
else {
/* Use mpz_bin_uiui which should be faster. */
mpz_bin_uiui(result->z, n, k);
return (PyObject*)result;
}
if (!(tempx = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 0), NULL))) {
Py_DECREF((PyObject*)result);
return NULL;
}
mpz_bin_ui(result->z, tempx->z, k);
Py_DECREF((PyObject*)tempx);
return (PyObject*)result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_isqrt,
"isqrt(x) -> mpz\n\n"
"Return the integer square root of an integer x. x >= 0.");
static PyObject *
GMPy_MPZ_Function_Isqrt(PyObject *self, PyObject *other)
{
MPZ_Object *result;
if (CHECK_MPZANY(other)) {
if (mpz_sgn(MPZ(other)) < 0) {
VALUE_ERROR("isqrt() of negative number");
return NULL;
}
if ((result = GMPy_MPZ_New(NULL))) {
mpz_sqrt(result->z, MPZ(other));
}
}
else {
if (!(result = GMPy_MPZ_From_Integer(other, NULL))) {
TYPE_ERROR("isqrt() requires 'mpz' argument");
return NULL;
}
if (mpz_sgn(result->z) < 0) {
VALUE_ERROR("isqrt() of negative number");
Py_DECREF((PyObject*)result);
return NULL;
}
mpz_sqrt(result->z, result->z);
}
return (PyObject*)result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_isqrt_rem,
"isqrt_rem(x) -> tuple\n\n"
"Return a 2-element tuple (s,t) such that s=isqrt(x) and t=x-s*s.\n"
"x >=0.");
static PyObject *
GMPy_MPZ_Function_IsqrtRem(PyObject *self, PyObject *other)
{
MPZ_Object *root = NULL, *rem = NULL, *temp = NULL;
PyObject *result;
if (!(temp = GMPy_MPZ_From_Integer(other, NULL))) {
TYPE_ERROR("isqrt_rem() requires 'mpz' argument");
return NULL;
}
if (mpz_sgn(temp->z) < 0) {
VALUE_ERROR("isqrt_rem() of negative number");
Py_DECREF((PyObject*)temp);
return NULL;
}
if (!(result = PyTuple_New(2)) ||
!(root = GMPy_MPZ_New(NULL)) ||
!(rem = GMPy_MPZ_New(NULL))) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)temp);
Py_XDECREF(result);
Py_XDECREF((PyObject*)root);
Py_XDECREF((PyObject*)rem);
return NULL;
/* LCOV_EXCL_STOP */
}
mpz_sqrtrem(root->z, rem->z, temp->z);
Py_DECREF((PyObject*)temp);
PyTuple_SET_ITEM(result, 0, (PyObject*)root);
PyTuple_SET_ITEM(result, 1, (PyObject*)rem);
return result;
}
PyDoc_STRVAR(GMPy_doc_mpz_function_remove,
"remove(x, f) -> tuple\n\n"
"Return a 2-element tuple (y,m) such that x=y*(f**m) and f does\n"
"not divide y. Remove the factor f from x as many times as\n"
"possible. m is the multiplicity f in x. f > 1.");
static PyObject *
GMPy_MPZ_Function_Remove(PyObject *self, PyObject *args)
{
MPZ_Object *result = NULL, *tempx = NULL, *tempf = NULL;
PyObject *x, *f;
size_t multiplicity;
if (PyTuple_GET_SIZE(args) != 2) {
TYPE_ERROR("remove() requires 'mpz','mpz' arguments");
return NULL;
}
if (!(result = GMPy_MPZ_New(NULL))) {
/* LCOV_EXCL_START */
return NULL;
/* LCOV_EXCL_STOP */
}
x = PyTuple_GET_ITEM(args, 0);
f = PyTuple_GET_ITEM(args, 1);
if (MPZ_Check(x) && MPZ_Check(f)) {
if (mpz_cmp_si(MPZ(f), 2) < 0) {
VALUE_ERROR("factor must be > 1");
Py_DECREF((PyObject*)result);
return NULL;
}
multiplicity = mpz_remove(result->z, MPZ(x), MPZ(f));
return Py_BuildValue("(Nk)", result, multiplicity);
}
else {
if (!(tempx = GMPy_MPZ_From_Integer(x, NULL)) ||
!(tempf = GMPy_MPZ_From_Integer(f, NULL))) {
TYPE_ERROR("remove() requires 'mpz','mpz' arguments");
Py_XDECREF((PyObject*)tempx);
Py_XDECREF((PyObject*)tempf);
Py_DECREF((PyObject*)result);
return NULL;
}
if (mpz_cmp_si(MPZ(tempf), 2) < 0) {
VALUE_ERROR("factor must be > 1");
Py_DECREF((PyObject*)tempx);
Py_DECREF((PyObject*)tempf);
Py_DECREF((PyObject*)result);
return NULL;
}
multiplicity = mpz_remove(result->z, tempx->z, tempf->z);
Py_DECREF((PyObject*)tempx);
Py_DECREF((PyObject*)tempf);
return Py_BuildValue("(Nk)", result, multiplicity);
}
}
PyDoc_STRVAR(GMPy_doc_mpz_function_invert,
"invert(x, m) -> mpz\n\n"
"Return y such that x*y == 1 modulo m. Raises ZeroDivisionError if no\n"
"inverse exists.");
static PyObject *
GMPy_MPZ_Function_Invert(PyObject *self, PyObject *args)
{
PyObject *x, *y;
MPZ_Object *result = NULL, *tempx = NULL, *tempy = NULL;
int success;
if (PyTuple_GET_SIZE(args) != 2) {
TYPE_ERROR("invert() requires 'mpz','mpz' arguments");
return NULL;
}
if (!(result = GMPy_MPZ_New(NULL))) {
/* LCOV_EXCL_START */
return NULL;
/* LCOV_EXCL_STOP */
}
x = PyTuple_GET_ITEM(args, 0);
y = PyTuple_GET_ITEM(args, 1);
if (MPZ_Check(x) && MPZ_Check(y)) {
if (mpz_sgn(MPZ(y)) == 0) {
ZERO_ERROR("invert() division by 0");
Py_DECREF((PyObject*)result);
return NULL;
}
success = mpz_invert(result->z, MPZ(x), MPZ(y));
if (!success) {