modified: SpatialOmicsCoord.py
[GalaxyCodeBases.git] / c_cpp / etc / calc / zprime.c
blob7a83531cc72dfbf2c2ccbd88f7eb1fa8f6f87d56
1 /*
2 * zprime - rapid small prime routines
4 * Copyright (C) 1999-2007 Landon Curt Noll
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.2 $
21 * @(#) $Id: zprime.c,v 30.2 2013/08/11 08:41:38 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/zprime.c,v $
24 * Under source code control: 1994/05/29 04:34:36
25 * File existed as early as: 1994
27 * chongo <was here> /\oo/\ http://www.isthe.com/chongo/
28 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
32 #include "zmath.h"
33 #include "prime.h"
34 #include "jump.h"
35 #include "config.h"
36 #include "zrand.h"
37 #include "have_const.h"
41 * When performing a probabilistic primality test, check to see
42 * if the number has a factor <= PTEST_PRECHECK.
44 * XXX - what should this value be? Perhaps this should be a function
45 * of the size of the text value and the number of tests?
47 #define PTEST_PRECHECK ((FULL)101)
50 * product of primes that fit into a long
52 STATIC CONST FULL pfact_tbl[MAX_PFACT_VAL+1] = {
53 1, 1, 2, 6, 6, 30, 30, 210, 210, 210, 210, 2310, 2310, 30030, 30030,
54 30030, 30030, 510510, 510510, 9699690, 9699690, 9699690, 9699690,
55 223092870, 223092870, 223092870, 223092870, 223092870, 223092870
56 #if FULL_BITS == 64
57 , U(6469693230), U(6469693230), U(200560490130), U(200560490130),
58 U(200560490130), U(200560490130), U(200560490130), U(200560490130),
59 U(7420738134810), U(7420738134810), U(7420738134810), U(7420738134810),
60 U(304250263527210), U(304250263527210), U(13082761331670030),
61 U(13082761331670030), U(13082761331670030), U(13082761331670030),
62 U(614889782588491410), U(614889782588491410), U(614889782588491410),
63 U(614889782588491410), U(614889782588491410), U(614889782588491410)
64 #endif
68 * determine the top 1 bit of a 8 bit value:
70 * topbit[0] == 0 by convention
71 * topbit[x] gives the highest 1 bit of x
73 STATIC CONST unsigned char topbit[256] = {
74 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
75 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
76 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
77 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
78 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
79 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
80 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
81 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
82 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
83 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
84 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
85 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
86 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
87 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
88 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
89 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
93 * integer square roots of powers of 2
95 * isqrt_pow2[x] == (int)(sqrt(2 to the x power)) (for 0 <= x < 64)
97 * We have enough table entries for a FULL that is 64 bits long.
99 STATIC CONST FULL isqrt_pow2[64] = {
100 1, 1, 2, 2, 4, 5, 8, 11, /* 0 .. 7 */
101 16, 22, 32, 45, 64, 90, 128, 181, /* 8 .. 15 */
102 256, 362, 512, 724, 1024, 1448, 2048, 2896, /* 16 .. 23 */
103 4096, 5792, 8192, 11585, 16384, 23170, 32768, 46340, /* 24 .. 31 */
104 65536, 92681, 131072, 185363, /* 32 .. 35 */
105 262144, 370727, 524288, 741455, /* 36 .. 39 */
106 1048576, 1482910, 2097152, 2965820, /* 40 .. 43 */
107 4194304, 5931641, 8388608, 11863283, /* 44 .. 47 */
108 16777216, 23726566, 33554432, 47453132, /* 48 .. 51 */
109 67108864, 94906265, 134217728, 189812531, /* 52 .. 55 */
110 268435456, 379625062, 536870912, 759250124, /* 56 .. 59 */
111 1073741824, 1518500249, 0x80000000, 0xb504f333 /* 60 .. 63 */
115 * static functions
117 S_FUNC FULL fsqrt(FULL v); /* quick square root of v */
118 S_FUNC long pix(FULL x); /* pi of x */
119 S_FUNC FULL small_factor(ZVALUE n, FULL limit); /* factor or 0 */
123 * Determine if a value is a small (32 bit) prime
125 * Returns:
126 * 1 z is a prime <= MAX_SM_VAL
127 * 0 z is not a prime <= MAX_SM_VAL
128 * -1 z > MAX_SM_VAL
130 FLAG
131 zisprime(ZVALUE z)
133 FULL n; /* number to test */
134 FULL isqr; /* factor limit */
135 CONST unsigned short *tp; /* pointer to a prime factor */
137 z.sign = 0;
138 if (zisleone(z)) {
139 return 0;
142 /* even numbers > 2 are not prime */
143 if (ziseven(z)) {
145 * "2 is the greatest odd prime because it is the least even!"
146 * - Dr. Dan Jurca 1978
148 return zisabstwo(z);
151 /* ignore non-small values */
152 if (zge32b(z)) {
153 return -1;
156 /* we now know that we are dealing with a value 0 <= n < 2^32 */
157 n = ztofull(z);
159 /* lookup small cases in pr_map */
160 if (n <= MAX_MAP_VAL) {
161 return (pr_map_bit(n) ? 1 : 0);
164 /* ignore Saber-C warning #530 about empty for statement */
165 /* ok to ignore in proc zisprime */
166 /* a number >=2^16 and < 2^32 */
167 for (isqr=fsqrt(n), tp=prime; (*tp <= isqr) && (n % *tp); ++tp) {
169 return ((*tp <= isqr && *tp != 1) ? 0 : 1);
174 * Determine the next small (32 bit) prime > a 32 bit value.
176 * given:
177 * z search point
179 * Returns:
180 * 0 next prime is 2^32+15
181 * 1 abs(z) >= 2^32
182 * smallest prime > abs(z) otherwise
184 FULL
185 znprime(ZVALUE z)
187 FULL n; /* search point */
189 z.sign = 0;
191 /* ignore large values */
192 if (zge32b(z)) {
193 return (FULL)1;
196 /* deal a search point of 0 or 1 */
197 if (zisabsleone(z)) {
198 return (FULL)2;
201 /* deal with returning a value that is beyond our reach */
202 n = ztofull(z);
203 if (n >= MAX_SM_PRIME) {
204 return (FULL)0;
207 /* return the next prime */
208 return next_prime(n);
213 * Compute the next prime beyond a small (32 bit) value.
215 * This function assumes that 2 <= n < 2^32-5.
217 * given:
218 * n search point
220 FULL
221 next_prime(FULL n)
223 CONST unsigned short *tp; /* pointer to a prime factor */
224 CONST unsigned char *j; /* current jump increment */
225 int tmp;
227 /* find our search point */
228 n = ((n & 0x1) ? n+2 : n+1);
230 /* if we can just search the bit map, then search it */
231 if (n <= MAX_MAP_PRIME) {
233 /* search until we find a 1 bit */
234 while (pr_map_bit(n) == 0) {
235 n += (FULL)2;
238 /* too large for our table, find the next prime the hard way */
239 } else {
240 FULL isqr; /* factor limit */
243 * Our search for a prime may cause us to increment n over
244 * a perfect square, but never two perfect squares. The largest
245 * prime gap <= 2614941711251 is 651. Shanks conjectures that
246 * the largest gap below P is about ln(P)^2.
248 * The value fsqrt(n)^2 will always be the perfect square
249 * that is <= n. Given the smallness of prime gaps we will
250 * deal with, we know that n could carry us across the next
251 * perfect square (fsqrt(n)+1)^2 but not the following
252 * perfect square (fsqrt(n)+2)^2.
254 * Now the factor search limit for values < (fsqrt(n)+2)^2
255 * is the same limit for (fsqrt(n)+1)^2; namely fsqrt(n)+1.
256 * Therefore setting our limit at fsqrt(n)+1 and never
257 * bothering with it after that is safe.
259 isqr = fsqrt(n)+1;
262 * If our factor limit is even, then we can reduce it to
263 * the next lowest odd value. We already tested if n
264 * was even and all of our remaining potential factors
265 * are odd.
267 if ((isqr & 0x1) == 0) {
268 --isqr;
272 * Skip to next value not divisible by a trivial prime.
274 n = firstjmp(n, tmp);
275 j = jmp + jmpptr(n);
278 * Look for tiny prime factors of increasing n until we
279 * find a prime.
281 do {
282 /* ignore Saber-C warning #530 - empty for statement */
283 /* ok to ignore in proc next_prime */
284 /* XXX - speed up test for large n by using gcds */
285 /* find a factor, or give up if not found */
286 for (tp=JPRIME; (*tp <= isqr) && (n % *tp); ++tp) {
288 } while (*tp <= isqr && *tp != 1 && (n += nxtjmp(j)));
291 /* return the prime that we found */
292 return n;
297 * Determine the previous small (32 bit) prime < a 32 bit value
299 * given:
300 * z search point
302 * Returns:
303 * 1 abs(z) >= 2^32
304 * 0 abs(z) <= 2
305 * greatest prime < abs(z) otherwise
307 FULL
308 zpprime(ZVALUE z)
310 CONST unsigned short *tp; /* pointer to a prime factor */
311 FULL isqr; /* isqrt(z) */
312 FULL n; /* search point */
313 CONST unsigned char *j; /* current jump increment */
314 int tmp;
316 z.sign = 0;
318 /* ignore large values */
319 if (zge32b(z)) {
320 return (FULL)1;
323 /* deal with special case small values */
324 n = ztofull(z);
325 switch ((int)n) {
326 case 0:
327 case 1:
328 case 2:
329 /* ignore values <= 2 */
330 return (FULL)0;
331 case 3:
332 /* 3 returns the only even prime */
333 return (FULL)2;
336 /* deal with values above the bit map */
337 if (n > NXT_MAP_PRIME) {
339 /* find our search point */
340 n = ((n & 0x1) ? n-2 : n-1);
342 /* our factor limit - see next_prime for why this works */
343 isqr = fsqrt(n)+1;
344 if ((isqr & 0x1) == 0) {
345 --isqr;
349 * Skip to previous value not divisible by a trivial prime.
351 tmp = jmpindxval(n);
352 if (tmp >= 0) {
354 /* find next value not divisible by a trivial prime */
355 n += tmp;
357 /* find the previous jump index */
358 j = jmp + jmpptr(n);
360 /* jump back */
361 n -= prevjmp(j);
363 /* already not divisible by a trivial prime */
364 } else {
365 /* find the current jump index */
366 j = jmp + jmpptr(n);
369 /* factor values until we find a prime */
370 do {
371 /* ignore Saber-C warning #530 - empty for statement */
372 /* ok to ignore in proc zpprime */
373 /* XXX - speed up test for large n by using gcds */
374 /* find a factor, or give up if not found */
375 for (tp=prime; (*tp <= isqr) && (n % *tp); ++tp) {
377 } while (*tp <= isqr && *tp != 1 && (n -= prevjmp(j)));
379 /* deal with values within the bit map */
380 } else if (n <= MAX_MAP_PRIME) {
382 /* find our search point */
383 n = ((n & 0x1) ? n-2 : n-1);
385 /* search until we find a 1 bit */
386 while (pr_map_bit(n) == 0) {
387 n -= (FULL)2;
390 /* deal with values that could cross into the bit map */
391 } else {
392 /* MAX_MAP_PRIME < n <= NXT_MAP_PRIME returns MAX_MAP_PRIME */
393 return MAX_MAP_PRIME;
396 /* return what we found */
397 return n;
402 * Compute the number of primes <= a ZVALUE that can fit into a FULL
404 * given:
405 * z compute primes <= z
407 * Returns:
408 * -1 error
409 * >=0 number of primes <= x
411 long
412 zpix(ZVALUE z)
414 /* pi(<0) is always 0 */
415 if (zisneg(z)) {
416 return (long)0;
419 /* firewall */
420 if (zge32b(z)) {
421 return (long)-1;
423 return pix(ztofull(z));
428 * Compute the number of primes <= a ZVALUE
430 * given:
431 * x value of z
433 * Returns:
434 * -1 error
435 * >=0 number of primes <= x
437 S_FUNC long
438 pix(FULL x)
440 long count; /* pi(x) */
441 FULL top; /* top of the range to test */
442 CONST unsigned short *tp; /* pointer to a tiny prime */
443 FULL i;
445 /* compute pi(x) using the 2^8 step table */
446 if (x <= MAX_PI10B) {
448 /* x within the prime table, so use it */
449 if (x < MAX_MAP_PRIME) {
450 /* firewall - pix(x) ==0 for x < 2 */
451 if (x < 2) {
452 count = 0;
454 } else {
455 /* determine how and where we will count */
456 if (x < 1024) {
457 count = 1;
458 tp = prime;
459 } else {
460 count = pi10b[x>>10];
461 tp = prime+count-1;
463 /* count primes in the table */
464 while (*tp++ <= x) {
465 ++count;
469 /* x is larger than the prime table, so count the hard way */
470 } else {
472 /* case: count down from pi18b entry to x */
473 if (x & 0x200) {
474 top = (x | 0x3ff);
475 count = pi10b[(top+1)>>10];
476 for (i=next_prime(x); i <= top;
477 i=next_prime(i)) {
478 --count;
481 /* case: count up from pi10b entry to x */
482 } else {
483 count = pi10b[x>>10];
484 for (i=next_prime(x&(~0x3ff));
485 i <= x; i = next_prime(i)) {
486 ++count;
491 /* compute pi(x) using the 2^18 interval table */
492 } else {
494 /* compute sum of intervals up to our interval */
495 for (count=0, i=0; i < (x>>18); ++i) {
496 count += pi18b[i];
499 /* case: count down from pi18b entry to x */
500 if (x & 0x20000) {
501 top = (x | 0x3ffff);
502 count += pi18b[i];
503 if (top > MAX_SM_PRIME) {
504 if (x < MAX_SM_PRIME) {
505 for (i=next_prime(x); i < MAX_SM_PRIME;
506 i=next_prime(i)) {
507 --count;
509 --count;
511 } else {
512 for (i=next_prime(x); i<=top; i=next_prime(i)) {
513 --count;
517 /* case: count up from pi18b entry to x */
518 } else {
519 for (i=next_prime(x&(~0x3ffff));
520 i <= x; i = next_prime(i)) {
521 ++count;
525 return count;
530 * Compute the smallest prime factor < limit
532 * given:
533 * n number to factor
534 * zlimit ending search point
535 * res factor, if found, or NULL
537 * Returns:
538 * -1 error, limit >= 2^32
539 * 0 no factor found, res is not changed
540 * 1 factor found, res (if non-NULL) is smallest prime factor
542 * NOTE: This routine will not return a factor == the test value
543 * except when the test value is 1 or -1.
545 FLAG
546 zfactor(ZVALUE n, ZVALUE zlimit, ZVALUE *res)
548 FULL f; /* factor found, or 0 */
551 * determine the limit
553 if (zge32b(zlimit)) {
554 /* limit is too large to be reasonable */
555 return -1;
557 n.sign = 0; /* ignore sign of n */
560 * find the smallest factor <= limit, if possible
562 f = small_factor(n, ztofull(zlimit));
565 * report the results
567 if (f > 0) {
568 /* return factor if requested */
569 if (res) {
570 utoz(f, res);
572 /* report a factor was found */
573 return 1;
575 /* no factor was found */
576 return 0;
581 * Find a smallest prime factor <= some small (32 bit) limit of a value
583 * given:
584 * z number to factor
585 * limit largest factor we will test
587 * Returns:
588 * 0 no prime <= the limit was found
589 * != 0 the smallest prime factor
591 S_FUNC FULL
592 small_factor(ZVALUE z, FULL limit)
594 FULL top; /* current max factor level */
595 CONST unsigned short *tp; /* pointer to a tiny prime */
596 FULL factlim; /* highest factor to test */
597 CONST unsigned short *p; /* test factor */
598 FULL factor; /* test factor */
599 HALF tlim; /* limit on prime table use */
600 HALF divval[2]; /* divisor value */
601 ZVALUE div; /* test factor/divisor */
602 ZVALUE tmp;
603 CONST unsigned char *j;
606 * catch impossible ranges
608 if (limit < 2) {
609 /* range is too small */
610 return 0;
614 * perform the even test
616 if (ziseven(z)) {
617 if (zistwo(z)) {
618 /* z is 2, so don't return 2 as a factor */
619 return 0;
621 return 2;
624 * value is odd
626 } else if (limit == 2) {
627 /* limit is 2, value is odd, no factors will ever be found */
628 return 0;
632 * force the factor limit to be odd
634 if ((limit & 0x1) == 0) {
635 --limit;
639 * case: number to factor fits into a FULL
641 if (!zgtmaxufull(z)) {
642 FULL val = ztofull(z); /* find the smallest factor of val */
643 FULL isqr; /* sqrt of val */
646 * special case: val is a prime <= MAX_MAP_PRIME
648 if (val <= MAX_MAP_PRIME && pr_map_bit(val)) {
649 /* z is prime, so no factors will be found */
650 return 0;
654 * we need not search above the sqrt of val
656 isqr = fsqrt(val);
657 if (limit > isqr) {
658 /* limit is largest odd value <= sqrt of val */
659 limit = ((isqr & 0x1) ? isqr : isqr-1);
663 * search for a small prime factor
665 top = ((limit < MAX_MAP_VAL) ? limit : MAX_MAP_VAL);
666 for (tp = prime; *tp <= top && *tp != 1; ++tp) {
667 if (val%(*tp) == 0) {
668 return ((FULL)*tp);
672 #if FULL_BITS == 64
674 * Our search will carry us beyond the prime table. We will
675 * continue to values until we reach our limit or until a
676 * factor is found.
678 * It is faster to simply test odd values and ignore non-prime
679 * factors because the work needed to find the next prime is
680 * more than the work one saves in not factor with non-prime
681 * values.
683 * We can improve on this method by skipping odd values that
684 * are a multiple of 3, 5, 7 and 11. We use a table of
685 * bytes that indicate the offsets between odd values that
686 * are not a multiple of 3,4,5,7 & 11.
688 /* XXX - speed up test for large z by using gcds */
689 j = jmp + jmpptr(NXT_MAP_PRIME);
690 for (top=NXT_MAP_PRIME; top <= limit; top += nxtjmp(j)) {
691 if ((val % top) == 0) {
692 return top;
695 #endif /* FULL_BITS == 64 */
697 /* no prime factors found */
698 return 0;
702 * Find a factor of a value that is too large to fit into a FULL.
704 * determine if/what our sqrt factor limit will be
706 if (zge64b(z)) {
707 /* we have no factor limit, avoid highest factor */
708 factlim = MAX_SM_PRIME-1;
709 } else if (zge32b(z)) {
710 /* determine if limit is too small to matter */
711 if (limit < BASE) {
712 factlim = limit;
713 } else {
714 /* find the isqrt(z) */
715 if (!zsqrt(z, &tmp, 0)) {
716 /* sqrt is exact */
717 factlim = ztofull(tmp);
718 } else {
719 /* sqrt is inexact */
720 factlim = ztofull(tmp)+1;
722 zfree(tmp);
724 /* avoid highest factor */
725 if (factlim >= MAX_SM_PRIME) {
726 factlim = MAX_SM_PRIME-1;
729 } else {
730 /* determine our factor limit */
731 factlim = fsqrt(ztofull(z));
732 if (factlim >= MAX_SM_PRIME) {
733 factlim = MAX_SM_PRIME-1;
736 if (factlim > limit) {
737 factlim = limit;
741 * walk the prime table looking for factors
743 * XXX - consider using gcd of products of primes to speed this
744 * section up
746 tlim = (HALF)((factlim >= MAX_MAP_PRIME) ? MAX_MAP_PRIME-1 : factlim);
747 div.sign = 0;
748 div.v = divval;
749 div.len = 1;
750 for (p=prime; (HALF)*p <= tlim; ++p) {
752 /* setup factor */
753 div.v[0] = (HALF)(*p);
755 if (zdivides(z, div))
756 return (FULL)(*p);
758 if ((FULL)*p > factlim) {
759 /* no factor found */
760 return (FULL)0;
764 * test the highest factor possible
766 div.v[0] = MAX_MAP_PRIME;
768 if (zdivides(z, div))
769 return (FULL)MAX_MAP_PRIME;
772 * generate higher test factors as needed
774 * XXX - consider using gcd of products of primes to speed this
775 * section up
777 #if BASEB == 16
778 div.len = 2;
779 #endif
780 factor = NXT_MAP_PRIME;
781 j = jmp + jmpptr(factor);
782 for(; factor <= factlim; factor += nxtjmp(j)) {
784 /* setup factor */
785 #if BASEB == 32
786 div.v[0] = (HALF)factor;
787 #else
788 div.v[0] = (HALF)(factor & BASE1);
789 div.v[1] = (HALF)(factor >> BASEB);
790 #endif
792 if (zdivides(z, div))
793 return (FULL)(factor);
795 if (factor >= factlim) {
796 /* no factor found */
797 return (FULL)0;
801 * test the highest factor possible
803 #if BASEB == 32
804 div.v[0] = MAX_SM_PRIME;
805 #else
806 div.v[0] = (MAX_SM_PRIME & BASE1);
807 div.v[1] = (MAX_SM_PRIME >> BASEB);
808 #endif
809 if (zdivides(z, div))
810 return (FULL)MAX_SM_PRIME;
813 * no factor found
815 return (FULL)0;
820 * Compute the product of the primes up to the specified number.
822 void
823 zpfact(ZVALUE z, ZVALUE *dest)
825 long n; /* limiting number to multiply by */
826 long p; /* current prime */
827 CONST unsigned short *tp; /* pointer to a tiny prime */
828 CONST unsigned char *j; /* current jump increment */
829 ZVALUE res, temp;
831 /* firewall */
832 if (zisneg(z)) {
833 math_error("Negative argument for factorial");
834 /*NOTREACHED*/
836 if (zge24b(z)) {
837 math_error("Very large factorial");
838 /*NOTREACHED*/
840 n = ztolong(z);
843 * Deal with table lookup pfact values
845 if (n <= MAX_PFACT_VAL) {
846 utoz(pfact_tbl[n], dest);
847 return;
851 * Multiply by the primes in the static table
853 utoz(pfact_tbl[MAX_PFACT_VAL], &res);
854 for (tp=(&prime[NXT_PFACT_VAL]); *tp != 1 && (long)(*tp) <= n; ++tp) {
855 zmuli(res, *tp, &temp);
856 zfree(res);
857 res = temp;
861 * if needed, multiply by primes beyond the static table
863 j = jmp + jmpptr(NXT_MAP_PRIME);
864 for (p = NXT_MAP_PRIME; p <= n; p += nxtjmp(j)) {
865 FULL isqr; /* isqrt(p) */
867 /* our factor limit - see next_prime for why this works */
868 isqr = fsqrt(p)+1;
869 if ((isqr & 0x1) == 0) {
870 --isqr;
873 /* ignore Saber-C warning #530 about empty for statement */
874 /* ok to ignore in proc zpfact */
875 /* find the next prime */
876 for (tp=prime; (*tp <= isqr) && (p % (long)(*tp)); ++tp) {
878 if (*tp <= isqr && *tp != 1) {
879 continue;
882 /* multiply by the next prime */
883 zmuli(res, p, &temp);
884 zfree(res);
885 res = temp;
887 *dest = res;
892 * Perform a probabilistic primality test (algorithm P in Knuth vol2, 4.5.4).
893 * Returns FALSE if definitely not prime, or TRUE if probably prime.
894 * Count determines how many times to check for primality.
895 * The chance of a non-prime passing this test is less than (1/4)^count.
896 * For example, a count of 100 fails for only 1 in 10^60 numbers.
898 * It is interesting to note that ptest(a,1,x) (for any x >= 0) of this
899 * test will always return TRUE for a prime, and rarely return TRUE for
900 * a non-prime. The 1/4 is appears in practice to be a poor upper
901 * bound. Even so the only result that is EXACT and TRUE is when
902 * this test returns FALSE for a non-prime. When ptest returns TRUE,
903 * one cannot determine if the value in question is prime, or the value
904 * is one of those rare non-primes that produces a false positive.
906 * The absolute value of count determines how many times to check
907 * for primality. If count < 0, then the trivial factor check is
908 * omitted.
909 * skip = 0 uses random bases
910 * skip = 1 uses prime bases 2, 3, 5, ...
911 * skip > 1 or < 0 uses bases skip, skip + 1, ...
913 BOOL
914 zprimetest(ZVALUE z, long count, ZVALUE skip)
916 long limit = 0; /* test odd values from skip up to limit */
917 ZVALUE zbase; /* base as a ZVALUE */
918 long i, ij, ik;
919 ZVALUE zm1, z1, z2, z3;
920 int type; /* random, prime or consecutive integers */
921 CONST unsigned short *pr; /* pointer to small prime */
924 * firewall - ignore sign of z, values 0 and 1 are not prime
926 z.sign = 0;
927 if (zisleone(z)) {
928 return 0;
932 * firewall - All even values, except 2, are not prime
934 if (ziseven(z))
935 return zistwo(z);
937 if (z.len == 1 && *z.v == 3)
938 return 1; /* 3 is prime */
941 * we know that z is an odd value > 1
945 * Perform trivial checks if count is not negative
947 if (count >= 0) {
950 * If the number is a small (32 bit) value, do a direct test
952 if (!zge32b(z)) {
953 return zisprime(z);
957 * See if the number has a tiny factor.
959 if (small_factor(z, PTEST_PRECHECK) != 0) {
960 /* a tiny factor was found */
961 return FALSE;
965 * If our count is zero, do nothing more
967 if (count == 0) {
968 /* no test was done, so no test failed! */
969 return TRUE;
972 } else {
973 /* use the absolute value of count */
974 count = -count;
976 if (z.len < conf->redc2) {
977 return zredcprimetest(z, count, skip);
980 if (ziszero(skip)) {
981 type = 0;
982 zbase = _zero_;
983 } else if (zisone(skip)) {
984 type = 1;
985 itoz(2, &zbase);
986 limit = 1 << 16;
987 if (!zge16b(z))
988 limit = ztolong(z);
989 } else {
990 type = 2;
991 if (zrel(skip, z) >= 0 || zisneg(skip))
992 zmod(skip, z, &zbase, 0);
993 else
994 zcopy(skip, &zbase);
997 * Loop over various bases, testing each one.
999 zsub(z, _one_, &zm1);
1000 ik = zlowbit(zm1);
1001 zshift(zm1, -ik, &z1);
1002 pr = prime;
1003 for (i = 0; i < count; i++) {
1004 switch (type) {
1005 case 0:
1006 zfree(zbase);
1007 zrandrange(_two_, zm1, &zbase);
1008 break;
1009 case 1:
1010 if (i == 0)
1011 break;
1012 zfree(zbase);
1013 if (*pr == 1 || (long)*pr >= limit) {
1014 zfree(z1);
1015 zfree(zm1);
1016 return TRUE;
1018 itoz((long) *pr++, &zbase);
1019 break;
1020 default:
1021 if (i == 0)
1022 break;
1023 zadd(zbase, _one_, &z3);
1024 zfree(zbase);
1025 zbase = z3;
1028 ij = 0;
1029 zpowermod(zbase, z1, z, &z3);
1030 for (;;) {
1031 if (zisone(z3)) {
1032 if (ij) {
1033 /* number is definitely not prime */
1034 zfree(z3);
1035 zfree(zm1);
1036 zfree(z1);
1037 zfree(zbase);
1038 return FALSE;
1040 break;
1042 if (!zcmp(z3, zm1))
1043 break;
1044 if (++ij >= ik) {
1045 /* number is definitely not prime */
1046 zfree(z3);
1047 zfree(zm1);
1048 zfree(z1);
1049 zfree(zbase);
1050 return FALSE;
1052 zsquare(z3, &z2);
1053 zfree(z3);
1054 zmod(z2, z, &z3, 0);
1055 zfree(z2);
1057 zfree(z3);
1059 zfree(zm1);
1060 zfree(z1);
1061 zfree(zbase);
1063 /* number might be prime */
1064 return TRUE;
1069 * Called by zprimetest when simple cases have been eliminated
1070 * and z.len < conf->redc2. Here count > 0, z is odd and > 3.
1072 BOOL
1073 zredcprimetest(ZVALUE z, long count, ZVALUE skip)
1075 long limit = 0; /* test odd values from skip up to limit */
1076 ZVALUE zbase; /* base as a ZVALUE */
1077 REDC *rp;
1078 long i, ij, ik;
1079 ZVALUE zm1, z1, z2, z3;
1080 ZVALUE zredcm1;
1081 int type; /* random, prime or consecutive integers */
1082 CONST unsigned short *pr; /* pointer to small prime */
1085 rp = zredcalloc(z);
1086 zsub(z, rp->one, &zredcm1);
1087 if (ziszero(skip)) {
1088 zbase = _zero_;
1089 type = 0;
1090 } else if (zisone(skip)) {
1091 itoz(2, &zbase);
1092 type = 1;
1093 limit = 1 << 16;
1094 if (!zge16b(z))
1095 limit = ztolong(z);
1096 } else {
1097 zredcencode(rp, skip, &zbase);
1098 type = 2;
1101 * Loop over various "random" numbers, testing each one.
1103 zsub(z, _one_, &zm1);
1104 ik = zlowbit(zm1);
1105 zshift(zm1, -ik, &z1);
1106 pr = prime;
1108 for (i = 0; i < count; i++) {
1109 switch (type) {
1110 case 0:
1111 do {
1112 zfree(zbase);
1113 zrandrange(_one_, z, &zbase);
1115 while (!zcmp(zbase, rp->one) ||
1116 !zcmp(zbase, zredcm1));
1117 break;
1118 case 1:
1119 if (i == 0) {
1120 break;
1122 zfree(zbase);
1123 if (*pr == 1 || (long)*pr >= limit) {
1124 zfree(z1);
1125 zfree(zm1);
1126 if (z.len < conf->redc2) {
1127 zredcfree(rp);
1128 zfree(zredcm1);
1130 return TRUE;
1132 itoz((long) *pr++, &z3);
1133 zredcencode(rp, z3, &zbase);
1134 zfree(z3);
1135 break;
1136 default:
1137 if (i == 0)
1138 break;
1139 zadd(zbase, rp->one, &z3);
1140 zfree(zbase);
1141 zbase = z3;
1142 if (zrel(zbase, z) >= 0) {
1143 zsub(zbase, z, &z3);
1144 zfree(zbase);
1145 zbase = z3;
1149 ij = 0;
1150 zredcpower(rp, zbase, z1, &z3);
1151 for (;;) {
1152 if (!zcmp(z3, rp->one)) {
1153 if (ij) {
1154 /* number is definitely not prime */
1155 zfree(z3);
1156 zfree(zm1);
1157 zfree(z1);
1158 zfree(zbase);
1159 zredcfree(rp);
1160 zfree(zredcm1);
1161 return FALSE;
1163 break;
1165 if (!zcmp(z3, zredcm1))
1166 break;
1167 if (++ij >= ik) {
1168 /* number is definitely not prime */
1169 zfree(z3);
1170 zfree(zm1);
1171 zfree(z1);
1172 zfree(zbase);
1173 zredcfree(rp);
1174 zfree(zredcm1);
1175 return FALSE;
1177 zredcsquare(rp, z3, &z2);
1178 zfree(z3);
1179 z3 = z2;
1181 zfree(z3);
1183 zfree(zbase);
1184 zredcfree(rp);
1185 zfree(zredcm1);
1186 zfree(zm1);
1187 zfree(z1);
1189 /* number might be prime */
1190 return TRUE;
1195 * znextcand - find the next integer that passes ptest().
1196 * The signs of z and mod are ignored. Result is the least integer
1197 * greater than abs(z) congruent to res modulo abs(mod), or if there
1198 * is no such integer, zero.
1200 * given:
1201 * z search point > 2
1202 * count ptests to perform per candidate
1203 * skip ptests to skip
1204 * res return congruent to res modulo abs(mod)
1205 * mod congruent to res modulo abs(mod)
1206 * cand candidate found
1208 BOOL
1209 znextcand(ZVALUE z, long count, ZVALUE skip, ZVALUE res, ZVALUE mod,
1210 ZVALUE *cand)
1212 ZVALUE tmp1;
1213 ZVALUE tmp2;
1215 z.sign = 0;
1216 mod.sign = 0;
1217 if (ziszero(mod)) {
1218 if (zrel(res, z) > 0 && zprimetest(res, count, skip)) {
1219 zcopy(res, cand);
1220 return TRUE;
1222 return FALSE;
1224 if (ziszero(z) && zisone(mod)) {
1225 zcopy(_two_, cand);
1226 return TRUE;
1228 zsub(res, z, &tmp1);
1229 if (zmod(tmp1, mod, &tmp2, 0))
1230 zadd(z, tmp2, cand);
1231 else
1232 zadd(z, mod, cand);
1235 * Now *cand is least integer greater than abs(z) and congruent
1236 * to res modulo mod.
1238 zfree(tmp1);
1239 zfree(tmp2);
1240 if (zprimetest(*cand, count, skip))
1241 return TRUE;
1242 zgcd(*cand, mod, &tmp1);
1243 if (!zisone(tmp1)) {
1244 zfree(tmp1);
1245 zfree(*cand);
1246 return FALSE;
1248 zfree(tmp1);
1249 if (ziseven(*cand)) {
1250 zadd(*cand, mod, &tmp1);
1251 zfree(*cand);
1252 *cand = tmp1;
1253 if (zprimetest(*cand, count, skip))
1254 return TRUE;
1257 * *cand is now least odd integer > abs(z) and congruent to
1258 * res modulo mod.
1260 if (zisodd(mod))
1261 zshift(mod, 1, &tmp1);
1262 else
1263 zcopy(mod, &tmp1);
1264 do {
1265 zadd(*cand, tmp1, &tmp2);
1266 zfree(*cand);
1267 *cand = tmp2;
1268 } while (!zprimetest(*cand, count, skip));
1269 zfree(tmp1);
1270 return TRUE;
1275 * zprevcand - find the nearest previous integer that passes ptest().
1276 * The signs of z and mod are ignored. Result is greatest positive integer
1277 * less than abs(z) congruent to res modulo abs(mod), or if there
1278 * is no such integer, zero.
1280 * given:
1281 * z search point > 2
1282 * count ptests to perform per candidate
1283 * skip ptests to skip
1284 * res return congruent to res modulo abs(mod)
1285 * mod congruent to res modulo abs(mod)
1286 * cand candidate found
1288 BOOL
1289 zprevcand(ZVALUE z, long count, ZVALUE skip, ZVALUE res, ZVALUE mod,
1290 ZVALUE *cand)
1292 ZVALUE tmp1;
1293 ZVALUE tmp2;
1295 z.sign = 0;
1296 mod.sign = 0;
1297 if (ziszero(mod)) {
1298 if (zispos(res)&&zrel(res, z)<0 && zprimetest(res,count,skip)) {
1299 zcopy(res, cand);
1300 return TRUE;
1302 return FALSE;
1304 zsub(z, res, &tmp1);
1305 if (zmod(tmp1, mod, &tmp2, 0))
1306 zsub(z, tmp2, cand);
1307 else
1308 zsub(z, mod, cand);
1310 * *cand is now the greatest integer < z that is congruent to res
1311 * modulo mod.
1313 zfree(tmp1);
1314 zfree(tmp2);
1315 if (zisneg(*cand)) {
1316 zfree(*cand);
1317 return FALSE;
1319 if (zprimetest(*cand, count, skip))
1320 return TRUE;
1321 zgcd(*cand, mod, &tmp1);
1322 if (!zisone(tmp1)) {
1323 zfree(tmp1);
1324 zmod(*cand, mod, &tmp1, 0);
1325 zfree(*cand);
1326 if (zprimetest(tmp1, count, skip)) {
1327 *cand = tmp1;
1328 return TRUE;
1330 if (ziszero(tmp1)) {
1331 zfree(tmp1);
1332 if (zprimetest(mod, count, skip)) {
1333 zcopy(mod, cand);
1334 return TRUE;
1336 return FALSE;
1338 zfree(tmp1);
1339 return FALSE;
1341 zfree(tmp1);
1342 if (ziseven(*cand)) {
1343 zsub(*cand, mod, &tmp1);
1344 zfree(*cand);
1345 if (zisneg(tmp1)) {
1346 zfree(tmp1);
1347 return FALSE;
1349 *cand = tmp1;
1350 if (zprimetest(*cand, count, skip))
1351 return TRUE;
1354 * *cand is now the greatest odd integer < z that is congruent to
1355 * res modulo mod.
1357 if (zisodd(mod))
1358 zshift(mod, 1, &tmp1);
1359 else
1360 zcopy(mod, &tmp1);
1362 do {
1363 zsub(*cand, tmp1, &tmp2);
1364 zfree(*cand);
1365 *cand = tmp2;
1366 } while (!zprimetest(*cand, count, skip) && !zisneg(*cand));
1367 zfree(tmp1);
1368 if (zisneg(*cand)) {
1369 zadd(*cand, mod, &tmp1);
1370 zfree(*cand);
1371 *cand = tmp1;
1372 if (zistwo(*cand))
1373 return TRUE;
1374 zfree(*cand);
1375 return FALSE;
1377 return TRUE;
1382 * Find the lowest prime factor of a number if one can be found.
1383 * Search is conducted for the first count primes.
1385 * Returns:
1386 * 1 no factor found or z < 3
1387 * >1 factor found
1389 FULL
1390 zlowfactor(ZVALUE z, long count)
1392 FULL factlim; /* highest factor to test */
1393 CONST unsigned short *p; /* test factor */
1394 FULL factor; /* test factor */
1395 HALF tlim; /* limit on prime table use */
1396 HALF divval[2]; /* divisor value */
1397 ZVALUE div; /* test factor/divisor */
1398 ZVALUE tmp;
1400 z.sign = 0;
1403 * firewall
1405 if (count <= 0 || zisleone(z) || zistwo(z)) {
1406 /* number is < 3 or count is <= 0 */
1407 return (FULL)1;
1411 * test for the first factor
1413 if (ziseven(z)) {
1414 return (FULL)2;
1416 if (count <= 1) {
1417 /* count was 1, tested the one and only factor */
1418 return (FULL)1;
1422 * determine if/what our sqrt factor limit will be
1424 if (zge64b(z)) {
1425 /* we have no factor limit, avoid highest factor */
1426 factlim = MAX_SM_PRIME-1;
1427 } else if (zge32b(z)) {
1428 /* find the isqrt(z) */
1429 if (!zsqrt(z, &tmp, 0)) {
1430 /* sqrt is exact */
1431 factlim = ztofull(tmp);
1432 } else {
1433 /* sqrt is inexact */
1434 factlim = ztofull(tmp)+1;
1436 zfree(tmp);
1438 /* avoid highest factor */
1439 if (factlim >= MAX_SM_PRIME) {
1440 factlim = MAX_SM_PRIME-1;
1442 } else {
1443 /* determine our factor limit */
1444 factlim = fsqrt(ztofull(z));
1446 if (factlim >= MAX_SM_PRIME) {
1447 factlim = MAX_SM_PRIME-1;
1451 * walk the prime table looking for factors
1453 tlim = (HALF)((factlim >= MAX_MAP_PRIME) ? MAX_MAP_PRIME-1 : factlim);
1454 div.sign = 0;
1455 div.v = divval;
1456 div.len = 1;
1457 for (p=prime, --count; count > 0 && (HALF)*p <= tlim; ++p, --count) {
1459 /* setup factor */
1460 div.v[0] = (HALF)(*p);
1462 if (zdivides(z, div))
1463 return (FULL)(*p);
1465 if (count <= 0 || (FULL)*p > factlim) {
1466 /* no factor found */
1467 return (FULL)1;
1471 * test the highest factor possible
1473 div.v[0] = MAX_MAP_PRIME;
1474 if (zdivides(z, div))
1475 return (FULL)MAX_MAP_PRIME;
1478 * generate higher test factors as needed
1480 #if BASEB == 16
1481 div.len = 2;
1482 #endif
1483 for(factor = NXT_MAP_PRIME;
1484 count > 0 && factor <= factlim;
1485 factor = next_prime(factor), --count) {
1487 /* setup factor */
1488 #if BASEB == 32
1489 div.v[0] = (HALF)factor;
1490 #else
1491 div.v[0] = (HALF)(factor & BASE1);
1492 div.v[1] = (HALF)(factor >> BASEB);
1493 #endif
1495 if (zdivides(z, div))
1496 return (FULL)(factor);
1498 if (count <= 0 || factor >= factlim) {
1499 /* no factor found */
1500 return (FULL)1;
1504 * test the highest factor possible
1506 #if BASEB == 32
1507 div.v[0] = MAX_SM_PRIME;
1508 #else
1509 div.v[0] = (MAX_SM_PRIME & BASE1);
1510 div.v[1] = (MAX_SM_PRIME >> BASEB);
1511 #endif
1512 if (zdivides(z, div))
1513 return (FULL)MAX_SM_PRIME;
1516 * no factor found
1518 return (FULL)1;
1523 * Compute the least common multiple of all the numbers up to the
1524 * specified number.
1526 void
1527 zlcmfact(ZVALUE z, ZVALUE *dest)
1529 long n; /* limiting number to multiply by */
1530 long p; /* current prime */
1531 long pp = 0; /* power of prime */
1532 long i; /* test value */
1533 CONST unsigned short *pr; /* pointer to a small prime */
1534 ZVALUE res, temp;
1536 if (zisneg(z) || ziszero(z)) {
1537 math_error("Non-positive argument for lcmfact");
1538 /*NOTREACHED*/
1540 if (zge24b(z)) {
1541 math_error("Very large lcmfact");
1542 /*NOTREACHED*/
1544 n = ztolong(z);
1546 * Multiply by powers of the necessary odd primes in order.
1547 * The power for each prime is the highest one which is not
1548 * more than the specified number.
1550 res = _one_;
1551 for (pr=prime; (long)(*pr) <= n && *pr > 1; ++pr) {
1552 i = p = *pr;
1553 while (i <= n) {
1554 pp = i;
1555 i *= p;
1557 zmuli(res, pp, &temp);
1558 zfree(res);
1559 res = temp;
1561 for (p = NXT_MAP_PRIME; p <= n; p = (long)next_prime(p)) {
1562 i = p;
1563 while (i <= n) {
1564 pp = i;
1565 i *= p;
1567 zmuli(res, pp, &temp);
1568 zfree(res);
1569 res = temp;
1572 * Finish by scaling by the necessary power of two.
1574 zshift(res, zhighbit(z), dest);
1575 zfree(res);
1580 * fsqrt - fast square root of a FULL value
1582 * We will determine the square root of a given value.
1583 * Starting with the integer square root of the largest power of
1584 * two <= the value, we will perform 3 Newton interations to
1585 * arive at our guess.
1587 * We have verified that fsqrt(x) == (FULL)sqrt((double)x), or
1588 * fsqrt(x)-1 == (FULL)sqrt((double)x) for all 0 <= x < 2^32.
1590 * given:
1591 * x compute the integer square root of x
1593 S_FUNC FULL
1594 fsqrt(FULL x)
1596 FULL y; /* (FULL)temporary value */
1597 int i;
1599 /* firewall - deal with 0 */
1600 if (x == 0) {
1601 return 0;
1604 /* ignore Saber-C warning #530 about empty for statement */
1605 /* ok to ignore in proc fsqrt */
1606 /* determine our initial guess */
1607 for (i=0, y=x; y >= (FULL)256; i+=8, y>>=8) {
1609 y = isqrt_pow2[i + topbit[y]];
1611 /* perform 3 Newton interations */
1612 y = (y+x/y)>>1;
1613 y = (y+x/y)>>1;
1614 y = (y+x/y)>>1;
1615 #if FULL_BITS == 64
1616 y = (y+x/y)>>1;
1617 #endif
1619 /* return the result */
1620 return y;