-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteger_function.js
1387 lines (1267 loc) · 37.6 KB
/
integer_function.js
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
/*
Some assorted integer functions.
Needs bignum.js to function properly.
*/
// Some cutoffs related to factorial computing
// YMMV, but that's also about the range where the largest
// multiplications reach Toom-Cook territory
var FACTORIAL_BORW_LOOP_CUTOFF = 500;
var FACTORIAL_BORW_PRIMORIAL_CUTOFF = 500;
var FACTORIAL_BORW_CUTOFF = 500;
// Euler numbers, cache related
var STATIC_EULER_ARRAY;
var STATIC_EULER_ARRAY_SIZE = 0;
var STATIC_EULER_ARRAY_PREFILL = 50;
// helper functions
// find x in p^x <= n!
function prime_divisors(n, p) {
var q, m;
q = n;
m = 0;
if (p > n) {
return 0;
}
if (p > Math.floor(n / 2)) {
return 1;
}
while (q >= p) {
q = Math.floor(q / p);
m += q;
}
return m;
}
// calculated 100000! in about 8 minutes on my 1GHz Duron
function factorial(n) {
// first fifty factorials
var small_factorials = [
[1],
[1],
[2],
[6],
[24],
[120],
[720],
[5040],
[40320],
[362880],
[3628800],
[39916800],
[9239552, 7],
[53005312, 92],
[3876864, 1299],
[58152960, 19485],
[58032128, 311773],
[47022080, 5300155],
[41091072, 28293938, 1],
[42532864, 713921, 27],
[45350912, 14278432, 540],
[12845056, 31411630, 11344],
[14155776, 19967224, 249578],
[57147392, 56592972, 5740300],
[29360128, 16054068, 3549492, 2],
[62914560, 65807390, 21628441, 51],
[25165824, 33270564, 25468579, 1334],
[8388608, 25890006, 16563006, 36028],
[33554432, 53831531, 61110994, 1008790],
[33554432, 17610541, 27388385, 29254936],
[0, 58554197, 16345189, 5232860, 13],
[0, 3240779, 36938838, 28000939, 405],
[0, 36596064, 41192129, 23614833, 12973],
[0, 66819424, 17162994, 41092005, 428120],
[0, 57267904, 46670917, 54950898, 14556100],
[0, 58219584, 22869388, 44233262, 39701480, 7],
[0, 15530240, 17991631, 48893572, 19967159, 273],
[0, 37747968, 61710579, 64231709, 587405, 10112],
[0, 25136640, 63300647, 24885872, 22321426, 384256],
[0, 40804864, 52806143, 31024948, 65229260, 14985996],
[0, 21581824, 31870960, 33038399, 59033586, 62568966, 8],
[0, 12439552, 31640957, 12397098, 4457942, 15190810, 366],
[0, 52699136, 53851785, 50916087, 53015843, 34034246, 15381],
[0, 51470336, 33925412, 41908127, 65088769, 54186467, 661404],
[0, 50102272, 16323153, 32018282, 45333575, 35394350, 29101811],
[0, 40009728, 63453278, 31536556, 26744976, 49241908, 34513102,
19
],
[0, 28508160, 33169663, 41395475, 22309365, 50535274, 44098853,
897
],
[0, 64815104, 15470308, 66539156, 41907223, 26347653, 59380206,
42189
],
[0, 24117248, 4377326, 39762891, 65389695, 56727821, 31677618,
2025114
],
[0, 40894464, 13162399, 2224606, 49978476, 28199852, 8699451,
32121745, 1
],
[0, 31457280, 54140204, 44121445, 15895833, 706493, 32319387,
62583384, 73
]
];
var ret;
// The actual binary splitting algorithm
// A bit more complicated by the lack of factors of two
var fbinsplit2b = function(n, m) {
var t1, t2, k;
if (m <= (n + 1)) {
return n.toBigint();
}
if (m == (n + 2)) {
return (n * m).toBigint();
}
k = Math.floor((n + m) / 2);
if ((k & 1) != 1) {
k--;
}
t1 = fbinsplit2b(n, k);
t2 = fbinsplit2b(k + 2, m);
return t1.mul(t2);
};
// binary splitting, sieve out factors of two and feed
// the actual splitting algorithm
var fbinsplit2a = function(n, p, r) {
if (n <= 2){
return;
}
fbinsplit2a(Math.floor(n / 2), p, r);
p[0] = p[0].mul(fbinsplit2b(Math.floor(n / 2) + 1 + (Math.floor(
n / 2) & 1), n - 1 + (n & 1)));
r[0] = r[0].mul(p[0]);
};
// binary splitting, base function
var bin_split = function(n) {
var p, r, shift;
p = [new Bigint(1)];
r = [new Bigint(1)];
fbinsplit2a(n, p, r);
shift = prime_divisors(n, 2);
r[0].lShiftInplace(shift);
return r[0];
};
// compute primorial with binary splitting
var primorial__lowlevel = function(array, array_pointer, n, result) {
var i, first_half, second_half;
var tmp;
if (n == 0) {
result[0] = new Bigint(1);
return MP_OKAY;
}
// Do the rest linearly. Faster for primorials at least, but YMMV
if (n <= 64) {
result[0] = array[array_pointer].toBigint();
for (i = 1; i < n; i++){
result[0] = result[0].mul(array[array_pointer + i].toBigint());
}
return MP_OKAY;
}
first_half = Math.floor(n / 2);
second_half = n - first_half;
primorial__lowlevel(array, array_pointer, second_half, result);
tmp = [new Bigint(1)];
primorial__lowlevel(array, array_pointer + second_half,
first_half, tmp);
result[0] = result[0].mul(tmp[0]);
return MP_OKAY;
};
// Borwein trick
var factorial_borwein = function(n) {
var p_list, arr;
var exp_list;
var p, i, j, cut;
var bit;
var shift, e;
var temp;
var r;
var buffer;
var result;
p_list = primesieve.primeRange(3, n);
r = p_list.length;
buffer = new ArrayBuffer(r * 4);
exp_list = new Uint32Array(buffer);
result = new Bigint(1);
shift = prime_divisors(n, 2);
cut = Math.floor(n / 2);
for (p = 0; p < r; p++) {
if (p_list[p] > cut) {
break;
}
exp_list[p] = prime_divisors(n, p_list[p]);
}
bit = exp_list[0].highBit();
if (n < FACTORIAL_BORW_LOOP_CUTOFF) {
for (; bit >= 0; bit--) {
result = result.sqr();
for (i = 0; i < p; i++) {
if ((exp_list[i] & (1 << bit)) != 0) {
result = result.mul(p_list[i].toBigint());
}
}
}
} else {
// memory is abundant today, isn't it?
buffer = new ArrayBuffer(r * 4);
arr = new Uint32Array(buffer);
for (; bit >= 0; bit--) {
result = result.sqr();
temp = [new Bigint(1)];
for (i = 0, j = 0; i < p; i++) {
if ((exp_list[i] & (1 << bit)) != 0) {
/*
result = result.mul(p_list[i].toBigint());
*/
arr[j++] = p_list[i];
}
}
primorial__lowlevel(arr, 0, j, temp);
result = result.mul(temp[0]);
}
}
if (n < FACTORIAL_BORW_PRIMORIAL_CUTOFF) {
for (; p < r; p++) {
result = result.mul(p_list[p].toBigint());
}
} else {
temp = [new Bigint(1)];
p_list = primesieve.primeRange(cut, n);
primorial__lowlevel(p_list, 0, p_list.length, temp);
result = result.mul(temp[0]);
}
result.lShiftInplace(shift);
return result;
};
if(!n.isInt()) {
// gamma function not yet implemented
return (new Bigint()).setNaN();
}
if(n > 1<<26) {
// Result would be larger than 1.497e49,6101,294
// but more seriously: must be smaller than the base to work
return (new Bigint()).setNaN();
}
if (n < 0) {
// singularities at the negative integers, all the way down.
return (new Bigint()).setInf();
}
ret = new Bigint();
if (n < small_factorials.length) {
ret.dp = small_factorials[n];
ret.used = ret.dp.length;
ret.clamp();
return ret;
}
if (n < FACTORIAL_BORW_CUTOFF) {
return bin_split(n);
}
return factorial_borwein(n);
};
/*
Compute Euler (secant, "zig") numbers
This algorithm computes the signed variant.
The (unsigned) zig-zag (up/down) Euler numbers
can be computed with
(-1)^k euler(2k) and a(2k-1) = (-1)^(k-1)2^(2k)(2^(2k)-1)bernoulli(2k)/(2k)
(From OEIS: A000111 by Ronaldo (aga_new_ac(AT)hotmail.com), Jan 17 2005)
The author of this program (Hey, that's me!) had no need for them but they
can be easily computed from the tangent numbers, too. Just drop a note if
you need it.
Algorithm is good but won't run sufficiently fast for
values above about n = 500.
Got E_1000 calulated in slightly over 2 minutes on
an old 1GHz AMD-Duron. Not bad for such an old destrier.
Brent, Richard P., and David Harvey. "Fast computation of Bernoulli, Tangent
and Secant numbers." Computational and Analytical Mathematics. Springer New
York, 2013. 127-142.
Preprint: http://arxiv.org/abs/1108.0286
*/
function euler(N) {
var n, e, k;
var bheuler = function(limit) {
var tmp;
var j, k, N;
var e;
N = limit + 1;
if (N < STATIC_EULER_ARRAY_SIZE) {
//console.log("Cache hit:" + N);
return MP_OKAY;
}
// calculate the first one hundred to fill the cache
// because Euler numbers almost always come in hordes.
if (STATIC_EULER_ARRAY_SIZE == 0 && N <
STATIC_EULER_ARRAY_PREFILL) {
N = STATIC_EULER_ARRAY_PREFILL;
}
/* For sake of simplicity */
//euler_array = malloc(sizeof(mp_int)*N+2);
STATIC_EULER_ARRAY = new Array(N + 2);
STATIC_EULER_ARRAY[0] = new Bigint(1);
for (k = 1; k <= N; k++) {
STATIC_EULER_ARRAY[k] = STATIC_EULER_ARRAY[k - 1].mulInt(k);
}
STATIC_EULER_ARRAY[k] = new Bigint(0);
for (k = 1; k < N; k++) {
for (j = k + 1; j < N; j++) {
/* euler_array[j] = (j-k)*euler_array[j-1] + (j-k+1)*euler_array[j] */
/* tmp = (j-k)*euler_array[j-1] */
tmp = STATIC_EULER_ARRAY[j - 1].mulInt(j - k);
/* euler_array[j] = (j-k+1)*euler_array[j] */
STATIC_EULER_ARRAY[j] = STATIC_EULER_ARRAY[j].mulInt(j -
k + 1);
/* euler_array[j] = euler_array[j] + tmp*/
STATIC_EULER_ARRAY[j] = STATIC_EULER_ARRAY[j].add(tmp);
}
}
for (k = 0; k < N; k++) {
/* Even Euler numbers (if indexed by even n) are negative */
if (k & 0x1) {
STATIC_EULER_ARRAY[k].sign = MP_NEG;
}
}
STATIC_EULER_ARRAY_SIZE = N;
return MP_OKAY;
};
n = N;
/* all odd Euler numbers are zero */
if ((n & 0x1) && n != 1) {
return new Bigint(0);
}
if ((e = bheuler(Math.floor(n / 2))) != MP_OKAY) {
return e;
}
k = Math.floor(n / 2);
return STATIC_EULER_ARRAY[k];
};
function comb(n, k) {
var d;
var q;
if (n < k) return 0;
if (n == k || k == 0) return 1;
if (k > n / 2) {
k = n - k;
}
d = n.gcd(k);
q = Math.floor(k / d);
return (Math.floor(comb(n - 1, k - 1) / q)) * Math.floor(n / d);
}
function bigcomb(n, k) {
var i, c;
var temp;
var e;
if (n < k) {
return new Bigint(0);
}
if (n == k) {
return new Bigint(1);
}
/* That's over-simplified, e.g. comb(99,6) = 1120529256 is still
smaller than 2^32.
An upper limit would be (the magic number is 1/sqrt(2*pi))
ulim = (0.398942280401430*pow(n,(n+.5 ))*pow((n-k),(k-n-.5 ))
/pow(p,(k + 0.5 )));
Stanica, Pantelimon. "Good lower and upper bounds on binomial
coefficients." Journal of Inequalities in Pure and Applied
Mathematics 2.3 (2001): 30.
*/
if (n < 35) {
temp = comb(n, k);
return temp.toBigint();
}
c = new Bigint(1);
if (k > Math.abs(n / 2)) {
k = n - k;
}
for (i = 1; i <= k; i++) {
e = n - k + i;
temp = e.toBigint();
c = c.mul(temp);
temp = i.toBigint();
c = c.div(temp);
}
return c;
}
function binomial(n, k) {
/* Idea shamelessly stolen from Calc.
Hey, wait, I wrote that calc-script myself!
Oh well, my age's starting to show, I'm afraid ;-)*/
var prime_list;
var pix = 0,
prime, K, diff;
var bst, c;
var e;
if (n < k) {
return new Bigint(0);
}
if (n == k || k == 0) {
return new Bigint(1);
}
if ((n - k) == 1 || k == 1) {
return n.toBigint();
}
/* The function comb(n,k) is faster if k<<n (and v.v.), the exact cut-off
has not been calculated yet but is quite large.*/
if (k < Math.floor(n / 900) || (n - k) < Math.floor(n / 900)) {
return bigcomb(n, k);
}
if (k > Math.floor(n / 2)) {
k = n - k;
}
primesieve.fill(n);
pix = primesieve.primePi(n);
prime_list = new Array(pix);
prime = 2;
K = 0;
do {
diff = prime_divisors(n, prime) -
(prime_divisors(n - k, prime) + prime_divisors(k, prime));
if (diff != 0) {
prime_list[K] = prime;
prime_list[K + 1] = diff;
K += 2;
}
prime = primesieve.nextPrime(prime + 1);
} while (prime > 0 && prime <= k);
do {
diff = prime_divisors(n, prime) - prime_divisors(n - k, prime);
if (diff != 0) {
prime_list[K] = prime;
prime_list[K + 1] = diff;
K += 2;
}
prime = primesieve.nextPrime(prime + 1);
} while (prime > 0 && prime <= n - k);
// we might be done now (for {k, n-k} = {2,3})
if(prime < 0){
return compute_factored_factorial(prime_list,K -1,0);
}
do {
prime_list[K] = prime;
prime_list[K + 1] = prime_divisors(n, prime);
prime = primesieve.nextPrime(prime + 1);
K += 2;
} while (prime > 0 && prime <= n);
c = compute_factored_factorial(prime_list, K - 1, 0)
return c;
}
// some more general functions to do arithmetic with factored integers
function factor_factorial(n, start) {
var pix, prime, K, test_before, prime_list;
var bst;
var prime_divisors = function(n, p) {
var q, m;
q = n;
m = 0;
if (p > n) {
return 0;
}
if (p > Math.floor(n / 2)) {
return 1;
}
while (q >= p) {
q = Math.floor(q / p);
m += q;
}
return m;
};
/* To be able to handle negative values in subtraction and to be sure that
n is positive*/
if (!n.isInt()) {
return MP_VAL;
}
primesieve.fill(n);
if (start <= 2) {
prime = 2;
} else {
if (!primesieve.isSmallPrime(start)) {
start = primesive.nextPrime(start);
//return MP_VAL;
}
prime = start;
}
if (prime > n) {
return MP_VAL;
}
K = 0;
pix = (primesieve.primePi(n) - primesieve.primePi(prime) + 1) * 2;
prime_list = new Array(pix);
do {
prime_list[K] = prime;
test_before = prime_divisors(n, prime);
if (!test_before.isInt()) {
return MP_VAL;
}
prime_list[K + 1] = test_before;
prime = primesieve.nextPrime(prime + 1);
K += 2;
} while (prime > 0 && prime <= n);
return prime_list;
}
var BN_MP_FACTORED_FACTORIAL_CUTOFF = 0x10000000;
/* Input is to be sorted as: prime,exponent,prime,exponent,... */
function compute_factored_factorial(f, f_length, stop) {
var length, start, i, c;
var shift = 0;
var bit, k = 0,
hb;
var temp;
var e;
if (stop == 0) {
length = f_length;
} else {
length = stop;
}
if (f[0] == 2 && f[1] > 0) {
shift = f[1];
if (length == 2) {
c = new Bigint(1);
c.lShiftInplace(f[1]);
return c;
}
}
start = 0;
if (shift) {
start += 2;
k = 2;
}
bit = 0;
for (; k < length; k += 2) {
hb = f[k + 1].highBit();
if (bit < hb) bit = hb;
}
/* just for safety reasons, y'know */
if (bit > MP_DIGIT_MAX) {
return MP_VAL;
}
c = new Bigint(1);
if (f_length > BN_MP_FACTORED_FACTORIAL_CUTOFF) {
for (; bit >= 0; bit--) {
c = c.sqr();
temp = new Bigint(1);
for (i = start; i < length; i += 2) {
if ((f[i + 1] & (1 << bit)) != 0) {
temp = temp.mulInt(f[i]);
}
}
c = c.mul(temp);
}
} else {
for (; bit >= 0; bit--) {
c = c.sqr();
for (i = start; i < length; i += 2) {
if ((f[i + 1] & (1 << bit)) != 0) {
c = c.mulInt(f[i]);
}
}
}
}
if (shift) {
if (shift <= MP_DIGIT_MAX) {
c.lShiftInplace(shift);
} else {
var multiplicator = 0;
/*
Dear future archeologist,
does it make sense in your time?
*/
while (shift >= MP_DIGIT_MAX) {
shift >>= 1;
multiplicator++;
}
c.lShiftInplace(shift);
c.lShiftInplace(1 << multiplicator);
}
}
return c;
}
function compute_signed_factored_factorial(f, f_length, do_division) {
var start = 0,
i, count = 0,
neg_count = 0,
k = 0,
c, r, last_div;
var shift = 0;
var bit = 0,
neg_bit = 0,
hb = 0,
neg_hb = 0,
p;
var temp;
var e;
if (arguments.length == 3) {
last_div = do_division;
} else {
last_div = false;
}
if (f[0] == 2 && f[1] > 0) {
shift = f[1];
if (f_length == 2) {
c = new Bigint(1);
c.lShiftInplace(f[1]);
return c;
}
}
if (shift) {
start += 2;
k = 2;
}
/*
One simple thing to handle negative exponents is to keep them in a second
array with absolute values of the exponents and divide at the end.
Another alternative might be to return both arrays and let the user decide.
No matter which way, we have to check and if there is at least one, divide.
This is expensive and should be done inline instead but for now...
*/
for (k = start; k < f_length; k += 2) {
if (f[k + 1] < 0) {
/* count how many to save some memory */
count++;
}
/* Only primes up to 2^DIGIT_BIT because of mp_mul_d() */
if (f[k] >= MP_DIGIT_MAX) {
return MP_VAL;
}
}
/* all are negative */
//if(count && count == f_length/2){
/* if the alternative with two outputs has been chosen just skip the
computation of the polynomial with all positive exponents and set
that return to 1 (one)
*/
/* goto: negative_exponents; */
/* The other alternative would compute 1/x that gives always 0 in integer
divisions if x>1. But it would need exactly one exponent with zero which
has been filtered out already. It is debatable now if that was a good
decision.*/
//return MP_OKAY;
//}
if (count) {
p = new Array((count * 2) + 1);
}
for (k = start; k < f_length; k += 2) {
/* won't branch if count == 0 */
if (f[k + 1] < 0) {
p[neg_count] = f[k];
p[neg_count + 1] = abs(f[k + 1]);
neg_count += 2;
/* And while we are already here ... */
neg_hb = Math.abs(f[k + 1]).highBit();
if (neg_bit < neg_hb) neg_bit = neg_hb;
/* This allows for some optimization, mainly w.r.t memory. Not done yet */
f[k] = 1;
/* Set to zero to let the main loop skip it */
f[k + 1] = 0;
} else {
hb = f[k + 1].highBit();
if (bit < hb) bit = hb;
}
}
/* DIGIT_BIT can be as small as 8 */
if (bit > MP_DIGIT_BIT || neg_bit > MP_DIGIT_BIT) {
return MP_VAL;
}
/* Anything times zero is zero, so set the result to one in advance */
c = new Bigint(1);
/* Everything is in place, so let's start by computing with the positive
exponents first */
/* The other function mp_compute_factored_factorials() has a branch
but the cases where it makes sense are quite rare. Feel free to add it
yourself */
for (; bit >= 0; bit--) {
c = c.sqr();
for (i = start; i < f_length; i += 2) {
if ((f[i + 1] & (1 << bit)) != 0) {
/* This is problematic if DIGIT_BIT is too small. checked above */
c = c.mulInt(f[i]);
}
}
}
/* Compute c * 2^n if n!=0 */
if (shift && shift > 0) {
if (shift < MP_DIGIT_MAX) {
c.lShiftInplace(shift);
} else {
return MP_VAL;
}
}
/* None are negative*/
if (count == 0) {
/* Clean up */
/* Nothing to clean up */
return c;
}
/* Calculate with the negative eponents */
temp = new Bigint(1);
for (; neg_bit >= 0; neg_bit--) {
temp = temp.sqr();
/* The exponents of 2 have been stripped already so we start at zero.
"count" counts only the occurences, the size itself is twice as large.
*/
for (i = 0; i < count * 2; i += 2) {
if ((p[i + 1] & (1 << neg_bit)) != 0) {
temp = temp.mulInt(p[i]);
}
}
}
/* Compute c * 2^n if n!=0 for negative n*/
if (shift && shift < 0) {
/* check for overflow */
if (shift <= -MP_DIGIT_MAX) {
return MP_VAL;
}
shift = -shift;
temp.lShiftInplace(shift);
}
if (last_div == true) {
return c.divrem(temp);
} else {
return [c, temp];
}
}
// reciprocal of a factorial
function negate_factored_factorials(input, l_input){
var k, counter=0;
var neg;
var output,l_output
output = new Array(l_input+1);
for(k=0;k<l_input;k+=2){
neg = input[k+1];
if(Math.abs(neg) >= MP_DIGIT_MAX){
return MP_VAL;
}
output[counter] = input[k];
output[counter+1] = -neg;
counter += 2;
}
l_output = counter;
return [output,l_output];
}
// multiply two factorials
function add_factored_factorials( summand_1,l_summand_1,
summand_2, l_summand_2){
var k, counter=0,sum, l_sum;
var max_length = Math.max(l_summand_1,l_summand_2);
var min_length = Math.min(l_summand_1,l_summand_2);
var s,p, s_1,s_2;
/* For a more detailed comments see subtract_factored_factorials() */
sum = new Array(max_length+1);
for(k=0;k<min_length;k+=2){
p = summand_1[k];
/*s = summand_1[k+1] + summand_2[k+1];*/
s_1 = summand_1[k+1];
s_2 = summand_2[k+1];
s = s_1 + s_2;
sum[counter] = p;
sum[counter+1] = s;
counter += 2;
}
if(l_summand_1 >= l_summand_2){
for(;k<max_length;k+=2){
p = summand_1[k];
s = summand_1[k+1];
sum[counter] = p;
sum[counter+1] = s;
counter += 2;
}
}
else{
for(;k<max_length;k+=2){
p = summand_2[k];
s = summand_2[k+1];
sum[counter] = p;
sum[counter+1] = s;
counter += 2;
}
}
l_sum = counter;
return [sum, l_sum];
}
// divide two factorials (binom. etc.)
function subtract_factored_factorials( subtrahend,
l_subtrahend,
minuend,
l_minuend
){
var k, counter=0,difference, l_difference;
var max_length = Math.max(l_subtrahend,l_minuend );
var min_length = Math.min(l_subtrahend,l_minuend );
var d,p,d_1,d_2;
/* TODO: check for sizes > 0 here */
/* Things work a bit different from ordinary arithmetic from now on */
difference = new Array(max_length+1);
/* Loop over smaller chunk first */
for(k=0;k<min_length;k+=2){
/* both have same length, we can take the value of the prime from any */
p = subtrahend[k];
/*d = subtrahend[k+1] - minuend[k+1];*/
d_1 = subtrahend[k+1];
d_2 = minuend[k+1];
d = d_1 - d_2;
difference[counter] = p;
difference[counter+1] = d;
counter += 2;
}
/* We need to find out which one is the smaller array and we have basically
two ways to approach the problem:
a) complicated and error-prone pointer juggling
b) just two loops
Here I have chosen b.
*/
/* If both arrays have the same length we can just stop here */
if(max_length == min_length){
/* We made nothing dirty, so there's nothing to clean up here, let's just
grab our stuff and run */
l_difference = counter;
return [difference, l_difference];
}
/* If the subtrahend is bigger we subtract zeros, so simply copy */
if(l_subtrahend >= l_minuend ){
for(k=min_length;k<max_length;k+=2){
p = subtrahend[k];
d = subtrahend[k+1];
difference[counter] = p;
difference[counter+1] = d;
counter += 2;
}
}
/* If the minuend is bigger we subtract from zeros, so negate before copy*/
else{
for(k=min_length;k<max_length;k+=2){
p = minuend[k];
d_1 = minuend[k+1];
d = -d_1;
difference[counter] = p;
difference[counter+1] = d;
counter += 2;
}
}
l_difference = counter;
return [difference, l_difference];
}
// exponentiate a factorial by an integer
function power_factored_factorials( input,
l_input,
multiplicator){
var k, counter=0;
var prod,p, p_1,p_2, product, l_product;
var temp;
product = new Array(l_input+1);
p_2 = multiplicator;
for(k=0;k<l_input;k+=2){
p = input[k];
/* Over/underflow possible! */
/*prod = input[k+1] * multiplicator;*/
p_1 = input[k+1];
temp = p * p_1;
prod = temp;
product[counter] = p;
product[counter+1] = prod;
counter += 2;
}
l_product = counter;
return [product,counter];
}
// computes catalan(10000) in about 2,5 seconds on my good ol' 1GHz Duron
// a 6015 decimal digits long number.
function catalan(n) {
var temp, c;
if (n == 0 || n == 1) {
return new Bigint(1);
}
/* C(2^31) is already a very large number
C(2^31) ~ 1.7593e1,292,913,972
The number of digits follow loosely the size of n here, e.g.
C(2^32) ~ 1.9303e2,585,827,958
C(2^64) ~ 2.5891e11,106,046,577,046,714,235
C(2^128) ~ 1.2728e204,870,398,877,478,727,500,024,218,500,206,465,342
*/
if (n >= MP_INT_MAX) {
return MP_VAL;
}
c = binomial(2 * n, n);
temp = (n + 1).toBigint();
c = c.div(temp);
return c;
}
// timing is about the same as for the factorial
function doublefactorial(n) {
var prime_list, c;
var pix = 0,
prime, K, diff;
var temp;
/* it is df(2n + 1) = (2*n)!/(n!*2^n) for odd n */
if (n >= MP_INT_MAX >>> 1) {
return MP_VAL;
}
// TODO: make a larger table out of it
switch (n) {
case -1:
case 0:
return new Bigint(1);
break;
case 2:
return new Bigint(2);
break;
case 3:
return new Bigint(3);
break;
case 4:
return new Bigint(8);
break;
case 5:
return new Bigint(15);
break;
case 6:
return new Bigint(48);
break;
/* smallest DIGIT_BIT is 8 */