zfactor.red 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. xmodule zfactor; % Integer factorization.
  2. % Author: Julian Padget.
  3. % Modifications by: Fran Burstall, John Abbott, Herbert Melenk,
  4. % Arthur Norman.
  5. exports nextprime, primep, zfactor, zfactor1, nrootnn;
  6. % nextprime - returns the next prime GREATER than its argument;
  7. % primep - determines whether argument is prime or not;
  8. % zfactor - returns an alist of factors dotted with their multiplicities
  9. imports evenp, gcdn, general!-modular!-expt, general!-modular!-times,
  10. idifference, igreaterp, ilessp, iplus2, iroot, isqrt, leq,
  11. modular!-expt, modular!-times, neq, prepf, prin2t, random, reversip,
  12. set!-general!-modulus, set!-modulus, set!-small!-modulus, typerr;
  13. % needs bigmod,smallmod;
  14. %
  15. % internal-functions add-factor, general-primep, mcfactor!*,
  16. % internal-primep, mcfactor, small-primep;
  17. % Parameters to this module are:
  18. %
  19. % !*confidence!* - controls the computation in the primality test.
  20. % Probability that a number is composite when test says it is
  21. % prime is 1/(2**(2*!*confidence!*)).
  22. %
  23. % !*maxtrys!* - controls the maximum number of attempts to be made
  24. % at factorisation (using mcfactor) whilst varying the polynomial
  25. % used as part of the Monte-Carlo technique. When !*maxtrys!* is
  26. % exceeded assumes n is prime (case will most likely occur when
  27. % primality test fails).
  28. %
  29. % !*mod!* - controls the modulus of the numbers emitted by the random
  30. % number generator. It is important that the number being tested
  31. % for primality should lie in [0,!*mod!*].
  32. %
  33. % Globals private to this module are:
  34. %
  35. % !*primelist!* - a list of the first xxx prime numbers used in the
  36. % first part of the factorization where trial division is
  37. % employed.
  38. %
  39. % !*last!-prime!-in!-list!* - the largest prime in the !*primelist!*
  40. fluid '(!*maxtrys!* !*confidence!*);
  41. !*maxtrys!*:=10; !*confidence!* := 40;
  42. global '(!*last!-prime!-squared!* !*primelist!*
  43. !*last!-prime!-in!-list!* largest!-small!-modulus);
  44. !*primelist!*:='(
  45. 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
  46. 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191
  47. 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283
  48. 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401
  49. 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509
  50. 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631
  51. 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751
  52. 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877
  53. 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1009
  54. 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093
  55. 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201
  56. 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297
  57. 1301 1303 1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427
  58. 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499
  59. 1511 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607
  60. 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 1697 1699 1709
  61. 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811 1823
  62. 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933
  63. 1949 1951 1973 1979 1987 1993 1997 1999 2003 2011 2017 2027 2029 2039
  64. 2053 2063 2069 2081 2083 2087 2089 2099 2111 2113 2129 2131 2137 2141
  65. 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 2251 2267 2269
  66. 2273 2281 2287 2293 2297 2309 2311 2333 2339 2341 2347 2351 2357 2371
  67. 2377 2381 2383 2389 2393 2399 2411 2417 2423 2437 2441 2447 2459 2467
  68. 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593 2609
  69. 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693 2699
  70. 2707 2711 2713 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797
  71. 2801 2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903 2909
  72. 2917 2927 2939 2953 2957 2963 2969 2971 2999 3001 3011 3019 3023 3037
  73. 3041 3049 3061 3067 3079 3083 3089 3109 3119 3121 3137 3163 3167 3169
  74. 3181 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257 3259 3271 3299
  75. 3301 3307 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 3373 3389
  76. 3391 3407 3413 3433 3449 3457 3461 3463 3467 3469 3491 3499 3511 3517
  77. 3527 3529 3533 3539 3541 3547 3557 3559 3571 )$
  78. !*last!-prime!-in!-list!* := car reverse !*primelist!*;
  79. !*last!-prime!-squared!* := !*last!-prime!-in!-list!*^2;
  80. symbolic procedure add!-factor(n,l);
  81. (lambda (p); if p then << rplacd(p,add1 cdr p); l>> else (n . 1) . l)
  82. if pairp l then if n>(caar l) then nil else assoc(n,l) else nil;
  83. symbolic procedure zfactor n; zfactor1(n,t);
  84. symbolic procedure zfactor1(n,bool);
  85. % If bool is NIL, mcfactor!* isn't used.
  86. if n<0 then ((-1) . 1) . zfactor1(-n,bool)
  87. else if n<4 then list(n . 1)
  88. else begin scalar primelist,factor!-list,p,qr;
  89. primelist := !*primelist!*;
  90. factor!-list := nil;
  91. while primelist do
  92. <<p := car primelist; primelist := cdr primelist;
  93. while cdr(qr := divide(n, p)) = 0 do
  94. <<n:= car qr; factor!-list:= add!-factor(p,factor!-list)>>;
  95. if n neq 1 and p*p>n
  96. then <<primelist := nil;
  97. factor!-list:=add!-factor(n,factor!-list);
  98. n := 1>>>>;
  99. return if n=1 then factor!-list
  100. else if null bool then (n . 1) . factor!-list
  101. else mcfactor!*(n,factor!-list)
  102. end;
  103. symbolic procedure mcfactor!*(n,factors!-so!-far);
  104. if internal!-primep n then add!-factor(n,factors!-so!-far)
  105. else <<n:=(lambda (p,tries); <<
  106. while (atom p) and (tries<!*maxtrys!*) do <<
  107. tries:=tries+1;
  108. p:=mcfactor(n,tries)>>;
  109. if tries>!*maxtrys!* then <<
  110. prin2 "ZFACTOR(mcfactor!*): Assuming ";
  111. prin2 n; prin2t " is prime";
  112. p:=list n>>
  113. else p>>)
  114. (mcfactor(n,1),1);
  115. if atom n then add!-factor(n,factors!-so!-far)
  116. else if car n < cdr n then
  117. mcfactor!*(cdr n,mcfactor!*(car n,factors!-so!-far))
  118. else mcfactor!*(car n,mcfactor!*(cdr n,factors!-so!-far))>>;
  119. symbolic procedure mcfactor(n,p);
  120. % Based on "An Improved Monte-Carlo Factorisation Algorithm" by
  121. % R.P.Brent in BIT 20 (1980) pp 176-184. Argument n is the number to
  122. % factor, p specifies the constant term of the polynomial. There are
  123. % supposed to be optimal p's for each n, but in general p=1 works well.
  124. begin scalar gg,k,m,q,r,x,y,ys;
  125. m := 20;
  126. y:=0; r:=q:=1;
  127. outer:
  128. x:=y;
  129. for i:=1:r do y:=remainder(y*y+p,n);
  130. k:=0;
  131. inner:
  132. ys:=y;
  133. for i:=1:(if m<(r-k) then m else r-k) do <<
  134. y:=remainder(y*y+p,n);
  135. q:=remainder(q*abs(x-y),n)
  136. >>;
  137. gg:=gcdn(q,n);
  138. k:=k+m;
  139. if (k<r) and (gg leq 1) then goto inner;
  140. r:=2*r;
  141. if gg leq 1 then goto outer;
  142. if gg=n then begin
  143. loop:
  144. ys:=remainder(ys*ys+p,n);
  145. gg:=gcdn(abs(x-ys),n);
  146. if gg leq 1 then goto loop
  147. end;
  148. return if gg=n then n else gg . (n/gg)
  149. end;
  150. symbolic procedure primep n;
  151. % Returns T if n is prime (an integer that is not zero or a unit).
  152. if not fixp n then typerr(n,"integer")
  153. % then <<lprim list("No primep function defined for",n); nil>>
  154. else if n<0 then primep(-n)
  155. else if n=1 then nil
  156. else if n<=!*last!-prime!-in!-list!* then n member !*primelist!*
  157. else if n<=!*last!-prime!-squared!*
  158. then begin scalar p;
  159. p := !*primelist!*;
  160. while p and remainder(n,car p) neq 0 do p := cdr p;
  161. return null p
  162. end
  163. else if n>largest!-small!-modulus then general!-primep n
  164. else small!-primep n;
  165. flag('(primep),'boolean);
  166. symbolic procedure internal!-primep n;
  167. if n>largest!-small!-modulus then general!-primep n
  168. else small!-primep n;
  169. % This is a version of primep written by FEB for inclusion in zfactor.
  170. % It provides small-primep and general-primep with the following
  171. % corrections of the distribution versions:
  172. % (1) random number zero excluded as a potential witness
  173. % (2) correct range of powers of seed provided
  174. % (3) inspection for -1 replacing gcd's.
  175. symbolic procedure small!-primep n;
  176. % Based on an algorithm of M.Rabin published in the Journal of Number
  177. % Theory Vol 12, pp 128-138 (1980).
  178. begin integer i,l,m,x,y,w,save; scalar result;
  179. % Filter out some easy cases first
  180. if evenp n or remainder(n,3) = 0 then return nil;
  181. m := n-1;
  182. save := set!-small!-modulus n;
  183. % Express n-1 = (2^l)*m
  184. l:=0;
  185. while evenp m do <<m := m/2; l := l+1>>;
  186. i:=1;
  187. result:=t;
  188. while result and i<=!*confidence!* do <<
  189. % Select a potential witness, noting 0, 1 and -1 are not liable to help.
  190. w := 1 + random(n-2);
  191. % Raise to the odd power.
  192. x := modular!-expt(w, m);
  193. % From here I can complete the calculation of w^(n-1) by doing a
  194. % sequence of squaring operations. While I do that I check to see if I
  195. % come across a non-trivial square root of 1, and if I do then I know n
  196. % could not have been prime. In fact in that case I could exhibit a
  197. % factor, but that does not concern me here.
  198. if x neq 1 then <<
  199. for k:=1:l do <<
  200. y := modular!-times(x,x);
  201. % It is tolerable to continue round the loop after setting result=nil
  202. % because I will then be repeating a squaring of 1, which is cheap.
  203. if y=1 and x neq (n-1) and x neq 1 then result := nil
  204. else x := y >>;
  205. % Also if I do not get to 1 at the end then the number is composite, but
  206. % I have no clue as to any factor.
  207. if x neq 1 then result := nil >>;
  208. i:=i+1 >>;
  209. set!-small!-modulus save;
  210. return result
  211. end;
  212. symbolic procedure general!-primep n;
  213. % Based on an algorithm of M.Rabin published in the Journal of Number
  214. % Theory Vol 12, pp 128-138 (1980).
  215. begin integer i,l,m,x,y,w,save; scalar result;
  216. % Filter out some easy cases first
  217. if evenp n or remainder(n,3) = 0 then return nil;
  218. m := n-1;
  219. save := set!-general!-modulus n;
  220. % Express n-1 = (2^l)*m
  221. l:=0;
  222. while evenp m do <<m := m/2; l := l+1>>;
  223. i:=1;
  224. result:=t;
  225. while result and i<=!*confidence!* do <<
  226. % Select a potential witness, noting 0, 1 and -1 are not liable to help.
  227. w := 1 + random(n-2);
  228. % Raise to the odd power.
  229. x:=general!-modular!-expt(w, m);
  230. % From here I can complete the calculation of w^(n-1) by doing a
  231. % sequence of squaring operations. While I do that I check to see if I
  232. % come across a non-trivial square root of 1, and if I do then I know n
  233. % could not have been prime. In fact in that case I could exhibit a
  234. % factor, but that does not concern me here.
  235. if x neq 1 then <<
  236. for k:=1:l do <<
  237. y:=general!-modular!-times(x,x);
  238. % It is tolerable to continue round the loop after setting result=nil
  239. % because I will then be repeating a squaring of 1, which is cheap.
  240. if y=1 and x neq (n-1) and x neq 1 then result := nil
  241. else x := y >>;
  242. % Also if I do not get to 1 at the end then the number is composite, but
  243. % I have no clue as to any factor.
  244. if x neq 1 then result := nil >>;
  245. i:=i+1 >>;
  246. set!-general!-modulus save;
  247. return result
  248. end;
  249. % The next function comes from J.H. Davenport.
  250. symbolic procedure nextprime p;
  251. % Returns the next prime number bigger than p.
  252. if null p or p=0 or p=1 or p=-1 or p=-2 then 2
  253. else if p=-3 then -2
  254. else if not fixp p then typerr(!*f2a p,"integer")
  255. else begin
  256. if evenp p then p:=p+1 else p:=p+2;
  257. while not primep p do p:=p+2;
  258. return p
  259. end;
  260. put('nextprime,'polyfn,'nextprime);
  261. % The following definition has been added by Herbert Melenk.
  262. symbolic procedure nrootnn(n,x);
  263. % N is an integer, x a positive integer. Value is a pair
  264. % of integers r,s such that r*s**(1/x)=n**(1/x). The decomposition
  265. % may be incomplete if the number is too big. The extraction of
  266. % the members of primelist* is complete.
  267. begin scalar pl,signn,qr,w; integer r,s,p,q;
  268. r := 1; s := 1;
  269. if n<0 then <<n := -n; if evenp x then signn := t else r := -1>>;
  270. pl:= !*primelist!*;
  271. loop:
  272. p:=car pl; pl:=cdr pl; q:=0;
  273. while cdr (qr:=divide(n,p))=0 do <<n:=car qr; q:=q #+ 1>>;
  274. if not (q #< x) then
  275. <<w:=divide(q,x); r:=r*(p**car w); q:=cdr w>>;
  276. while q #> 0 do <<s:=s*p; q:=q #- 1>>;
  277. if car qr < p then << s:=n*s; goto done>>;
  278. if pl then goto loop;
  279. % heuristic bound for complete factorization.
  280. if 10^20 > n then
  281. <<q:=mcfactor!*(n,nil);
  282. for each j in q do
  283. <<w := divide(cdr j,x);
  284. r := car j**car w*r;
  285. s := car j**cdr w*s>>;
  286. >>
  287. else if (q:=iroot(n,x)) then r:=r*q
  288. else s:=n*s;
  289. done:
  290. if signn then s := -s;
  291. return r . s
  292. end;
  293. endmodule;
  294. end;