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/
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
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)
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 */
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
126 * 1 z is a prime <= MAX_SM_VAL
127 * 0 z is not a prime <= MAX_SM_VAL
133 FULL n
; /* number to test */
134 FULL isqr
; /* factor limit */
135 CONST
unsigned short *tp
; /* pointer to a prime factor */
142 /* even numbers > 2 are not prime */
145 * "2 is the greatest odd prime because it is the least even!"
146 * - Dr. Dan Jurca 1978
151 /* ignore non-small values */
156 /* we now know that we are dealing with a value 0 <= n < 2^32 */
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.
180 * 0 next prime is 2^32+15
182 * smallest prime > abs(z) otherwise
187 FULL n
; /* search point */
191 /* ignore large values */
196 /* deal a search point of 0 or 1 */
197 if (zisabsleone(z
)) {
201 /* deal with returning a value that is beyond our reach */
203 if (n
>= MAX_SM_PRIME
) {
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.
223 CONST
unsigned short *tp
; /* pointer to a prime factor */
224 CONST
unsigned char *j
; /* current jump increment */
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) {
238 /* too large for our table, find the next prime the hard way */
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.
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
267 if ((isqr
& 0x1) == 0) {
272 * Skip to next value not divisible by a trivial prime.
274 n
= firstjmp(n
, tmp
);
278 * Look for tiny prime factors of increasing n until we
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 */
297 * Determine the previous small (32 bit) prime < a 32 bit value
305 * greatest prime < abs(z) otherwise
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 */
318 /* ignore large values */
323 /* deal with special case small values */
329 /* ignore values <= 2 */
332 /* 3 returns the only even prime */
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 */
344 if ((isqr
& 0x1) == 0) {
349 * Skip to previous value not divisible by a trivial prime.
354 /* find next value not divisible by a trivial prime */
357 /* find the previous jump index */
363 /* already not divisible by a trivial prime */
365 /* find the current jump index */
369 /* factor values until we find a prime */
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) {
390 /* deal with values that could cross into the bit map */
392 /* MAX_MAP_PRIME < n <= NXT_MAP_PRIME returns MAX_MAP_PRIME */
393 return MAX_MAP_PRIME
;
396 /* return what we found */
402 * Compute the number of primes <= a ZVALUE that can fit into a FULL
405 * z compute primes <= z
409 * >=0 number of primes <= x
414 /* pi(<0) is always 0 */
423 return pix(ztofull(z
));
428 * Compute the number of primes <= a ZVALUE
435 * >=0 number of primes <= 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 */
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 */
455 /* determine how and where we will count */
460 count
= pi10b
[x
>>10];
463 /* count primes in the table */
469 /* x is larger than the prime table, so count the hard way */
472 /* case: count down from pi18b entry to x */
475 count
= pi10b
[(top
+1)>>10];
476 for (i
=next_prime(x
); i
<= top
;
481 /* case: count up from pi10b entry to x */
483 count
= pi10b
[x
>>10];
484 for (i
=next_prime(x
&(~0x3ff));
485 i
<= x
; i
= next_prime(i
)) {
491 /* compute pi(x) using the 2^18 interval table */
494 /* compute sum of intervals up to our interval */
495 for (count
=0, i
=0; i
< (x
>>18); ++i
) {
499 /* case: count down from pi18b entry to x */
503 if (top
> MAX_SM_PRIME
) {
504 if (x
< MAX_SM_PRIME
) {
505 for (i
=next_prime(x
); i
< MAX_SM_PRIME
;
512 for (i
=next_prime(x
); i
<=top
; i
=next_prime(i
)) {
517 /* case: count up from pi18b entry to x */
519 for (i
=next_prime(x
&(~0x3ffff));
520 i
<= x
; i
= next_prime(i
)) {
530 * Compute the smallest prime factor < limit
534 * zlimit ending search point
535 * res factor, if found, or NULL
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.
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 */
557 n
.sign
= 0; /* ignore sign of n */
560 * find the smallest factor <= limit, if possible
562 f
= small_factor(n
, ztofull(zlimit
));
568 /* return factor if requested */
572 /* report a factor was found */
575 /* no factor was found */
581 * Find a smallest prime factor <= some small (32 bit) limit of a value
585 * limit largest factor we will test
588 * 0 no prime <= the limit was found
589 * != 0 the smallest prime factor
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 */
603 CONST
unsigned char *j
;
606 * catch impossible ranges
609 /* range is too small */
614 * perform the even test
618 /* z is 2, so don't return 2 as a factor */
626 } else if (limit
== 2) {
627 /* limit is 2, value is odd, no factors will ever be found */
632 * force the factor limit to be odd
634 if ((limit
& 0x1) == 0) {
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 */
654 * we need not search above the sqrt of val
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) {
674 * Our search will carry us beyond the prime table. We will
675 * continue to values until we reach our limit or until a
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
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) {
695 #endif /* FULL_BITS == 64 */
697 /* no prime factors found */
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
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 */
714 /* find the isqrt(z) */
715 if (!zsqrt(z
, &tmp
, 0)) {
717 factlim
= ztofull(tmp
);
719 /* sqrt is inexact */
720 factlim
= ztofull(tmp
)+1;
724 /* avoid highest factor */
725 if (factlim
>= MAX_SM_PRIME
) {
726 factlim
= MAX_SM_PRIME
-1;
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
) {
741 * walk the prime table looking for factors
743 * XXX - consider using gcd of products of primes to speed this
746 tlim
= (HALF
)((factlim
>= MAX_MAP_PRIME
) ? MAX_MAP_PRIME
-1 : factlim
);
750 for (p
=prime
; (HALF
)*p
<= tlim
; ++p
) {
753 div
.v
[0] = (HALF
)(*p
);
755 if (zdivides(z
, div
))
758 if ((FULL
)*p
> factlim
) {
759 /* no factor found */
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
780 factor
= NXT_MAP_PRIME
;
781 j
= jmp
+ jmpptr(factor
);
782 for(; factor
<= factlim
; factor
+= nxtjmp(j
)) {
786 div
.v
[0] = (HALF
)factor
;
788 div
.v
[0] = (HALF
)(factor
& BASE1
);
789 div
.v
[1] = (HALF
)(factor
>> BASEB
);
792 if (zdivides(z
, div
))
793 return (FULL
)(factor
);
795 if (factor
>= factlim
) {
796 /* no factor found */
801 * test the highest factor possible
804 div
.v
[0] = MAX_SM_PRIME
;
806 div
.v
[0] = (MAX_SM_PRIME
& BASE1
);
807 div
.v
[1] = (MAX_SM_PRIME
>> BASEB
);
809 if (zdivides(z
, div
))
810 return (FULL
)MAX_SM_PRIME
;
820 * Compute the product of the primes up to the specified number.
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 */
833 math_error("Negative argument for factorial");
837 math_error("Very large factorial");
843 * Deal with table lookup pfact values
845 if (n
<= MAX_PFACT_VAL
) {
846 utoz(pfact_tbl
[n
], dest
);
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
);
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 */
869 if ((isqr
& 0x1) == 0) {
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) {
882 /* multiply by the next prime */
883 zmuli(res
, p
, &temp
);
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
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, ...
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 */
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
932 * firewall - All even values, except 2, are not prime
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
950 * If the number is a small (32 bit) value, do a direct test
957 * See if the number has a tiny factor.
959 if (small_factor(z
, PTEST_PRECHECK
) != 0) {
960 /* a tiny factor was found */
965 * If our count is zero, do nothing more
968 /* no test was done, so no test failed! */
973 /* use the absolute value of count */
976 if (z
.len
< conf
->redc2
) {
977 return zredcprimetest(z
, count
, skip
);
983 } else if (zisone(skip
)) {
991 if (zrel(skip
, z
) >= 0 || zisneg(skip
))
992 zmod(skip
, z
, &zbase
, 0);
997 * Loop over various bases, testing each one.
999 zsub(z
, _one_
, &zm1
);
1001 zshift(zm1
, -ik
, &z1
);
1003 for (i
= 0; i
< count
; i
++) {
1007 zrandrange(_two_
, zm1
, &zbase
);
1013 if (*pr
== 1 || (long)*pr
>= limit
) {
1018 itoz((long) *pr
++, &zbase
);
1023 zadd(zbase
, _one_
, &z3
);
1029 zpowermod(zbase
, z1
, z
, &z3
);
1033 /* number is definitely not prime */
1045 /* number is definitely not prime */
1054 zmod(z2
, z
, &z3
, 0);
1063 /* number might be prime */
1069 * Called by zprimetest when simple cases have been eliminated
1070 * and z.len < conf->redc2. Here count > 0, z is odd and > 3.
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 */
1079 ZVALUE zm1
, z1
, z2
, z3
;
1081 int type
; /* random, prime or consecutive integers */
1082 CONST
unsigned short *pr
; /* pointer to small prime */
1086 zsub(z
, rp
->one
, &zredcm1
);
1087 if (ziszero(skip
)) {
1090 } else if (zisone(skip
)) {
1097 zredcencode(rp
, skip
, &zbase
);
1101 * Loop over various "random" numbers, testing each one.
1103 zsub(z
, _one_
, &zm1
);
1105 zshift(zm1
, -ik
, &z1
);
1108 for (i
= 0; i
< count
; i
++) {
1113 zrandrange(_one_
, z
, &zbase
);
1115 while (!zcmp(zbase
, rp
->one
) ||
1116 !zcmp(zbase
, zredcm1
));
1123 if (*pr
== 1 || (long)*pr
>= limit
) {
1126 if (z
.len
< conf
->redc2
) {
1132 itoz((long) *pr
++, &z3
);
1133 zredcencode(rp
, z3
, &zbase
);
1139 zadd(zbase
, rp
->one
, &z3
);
1142 if (zrel(zbase
, z
) >= 0) {
1143 zsub(zbase
, z
, &z3
);
1150 zredcpower(rp
, zbase
, z1
, &z3
);
1152 if (!zcmp(z3
, rp
->one
)) {
1154 /* number is definitely not prime */
1165 if (!zcmp(z3
, zredcm1
))
1168 /* number is definitely not prime */
1177 zredcsquare(rp
, z3
, &z2
);
1189 /* number might be prime */
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.
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
1209 znextcand(ZVALUE z
, long count
, ZVALUE skip
, ZVALUE res
, ZVALUE mod
,
1218 if (zrel(res
, z
) > 0 && zprimetest(res
, count
, skip
)) {
1224 if (ziszero(z
) && zisone(mod
)) {
1228 zsub(res
, z
, &tmp1
);
1229 if (zmod(tmp1
, mod
, &tmp2
, 0))
1230 zadd(z
, tmp2
, cand
);
1235 * Now *cand is least integer greater than abs(z) and congruent
1236 * to res modulo mod.
1240 if (zprimetest(*cand
, count
, skip
))
1242 zgcd(*cand
, mod
, &tmp1
);
1243 if (!zisone(tmp1
)) {
1249 if (ziseven(*cand
)) {
1250 zadd(*cand
, mod
, &tmp1
);
1253 if (zprimetest(*cand
, count
, skip
))
1257 * *cand is now least odd integer > abs(z) and congruent to
1261 zshift(mod
, 1, &tmp1
);
1265 zadd(*cand
, tmp1
, &tmp2
);
1268 } while (!zprimetest(*cand
, count
, skip
));
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.
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
1289 zprevcand(ZVALUE z
, long count
, ZVALUE skip
, ZVALUE res
, ZVALUE mod
,
1298 if (zispos(res
)&&zrel(res
, z
)<0 && zprimetest(res
,count
,skip
)) {
1304 zsub(z
, res
, &tmp1
);
1305 if (zmod(tmp1
, mod
, &tmp2
, 0))
1306 zsub(z
, tmp2
, cand
);
1310 * *cand is now the greatest integer < z that is congruent to res
1315 if (zisneg(*cand
)) {
1319 if (zprimetest(*cand
, count
, skip
))
1321 zgcd(*cand
, mod
, &tmp1
);
1322 if (!zisone(tmp1
)) {
1324 zmod(*cand
, mod
, &tmp1
, 0);
1326 if (zprimetest(tmp1
, count
, skip
)) {
1330 if (ziszero(tmp1
)) {
1332 if (zprimetest(mod
, count
, skip
)) {
1342 if (ziseven(*cand
)) {
1343 zsub(*cand
, mod
, &tmp1
);
1350 if (zprimetest(*cand
, count
, skip
))
1354 * *cand is now the greatest odd integer < z that is congruent to
1358 zshift(mod
, 1, &tmp1
);
1363 zsub(*cand
, tmp1
, &tmp2
);
1366 } while (!zprimetest(*cand
, count
, skip
) && !zisneg(*cand
));
1368 if (zisneg(*cand
)) {
1369 zadd(*cand
, mod
, &tmp1
);
1382 * Find the lowest prime factor of a number if one can be found.
1383 * Search is conducted for the first count primes.
1386 * 1 no factor found or z < 3
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 */
1405 if (count
<= 0 || zisleone(z
) || zistwo(z
)) {
1406 /* number is < 3 or count is <= 0 */
1411 * test for the first factor
1417 /* count was 1, tested the one and only factor */
1422 * determine if/what our sqrt factor limit will be
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)) {
1431 factlim
= ztofull(tmp
);
1433 /* sqrt is inexact */
1434 factlim
= ztofull(tmp
)+1;
1438 /* avoid highest factor */
1439 if (factlim
>= MAX_SM_PRIME
) {
1440 factlim
= MAX_SM_PRIME
-1;
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
);
1457 for (p
=prime
, --count
; count
> 0 && (HALF
)*p
<= tlim
; ++p
, --count
) {
1460 div
.v
[0] = (HALF
)(*p
);
1462 if (zdivides(z
, div
))
1465 if (count
<= 0 || (FULL
)*p
> factlim
) {
1466 /* no factor found */
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
1483 for(factor
= NXT_MAP_PRIME
;
1484 count
> 0 && factor
<= factlim
;
1485 factor
= next_prime(factor
), --count
) {
1489 div
.v
[0] = (HALF
)factor
;
1491 div
.v
[0] = (HALF
)(factor
& BASE1
);
1492 div
.v
[1] = (HALF
)(factor
>> BASEB
);
1495 if (zdivides(z
, div
))
1496 return (FULL
)(factor
);
1498 if (count
<= 0 || factor
>= factlim
) {
1499 /* no factor found */
1504 * test the highest factor possible
1507 div
.v
[0] = MAX_SM_PRIME
;
1509 div
.v
[0] = (MAX_SM_PRIME
& BASE1
);
1510 div
.v
[1] = (MAX_SM_PRIME
>> BASEB
);
1512 if (zdivides(z
, div
))
1513 return (FULL
)MAX_SM_PRIME
;
1523 * Compute the least common multiple of all the numbers up to the
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 */
1536 if (zisneg(z
) || ziszero(z
)) {
1537 math_error("Non-positive argument for lcmfact");
1541 math_error("Very large lcmfact");
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.
1551 for (pr
=prime
; (long)(*pr
) <= n
&& *pr
> 1; ++pr
) {
1557 zmuli(res
, pp
, &temp
);
1561 for (p
= NXT_MAP_PRIME
; p
<= n
; p
= (long)next_prime(p
)) {
1567 zmuli(res
, pp
, &temp
);
1572 * Finish by scaling by the necessary power of two.
1574 zshift(res
, zhighbit(z
), dest
);
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.
1591 * x compute the integer square root of x
1596 FULL y
; /* (FULL)temporary value */
1599 /* firewall - deal with 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 */
1619 /* return the result */