home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / unix / volume26 / calc / part13 < prev    next >
Encoding:
Text File  |  1992-05-09  |  29.9 KB  |  1,063 lines

  1. Newsgroups: comp.sources.unix
  2. From: dbell@pdact.pd.necisa.oz.au (David I. Bell)
  3. Subject: v26i039: CALC - An arbitrary precision C-like calculator, Part13/21
  4. Sender: unix-sources-moderator@pa.dec.com
  5. Approved: vixie@pa.dec.com
  6.  
  7. Submitted-By: dbell@pdact.pd.necisa.oz.au (David I. Bell)
  8. Posting-Number: Volume 26, Issue 39
  9. Archive-Name: calc/part13
  10.  
  11. #! /bin/sh
  12. # This is a shell archive.  Remove anything before this line, then unpack
  13. # it by saving it into a file and typing "sh file".  To overwrite existing
  14. # files, type "sh file -c".  You can also feed this as standard input via
  15. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  16. # will see the following message at the end:
  17. #        "End of archive 13 (of 21)."
  18. # Contents:  lib/lucas.cal
  19. # Wrapped by dbell@elm on Tue Feb 25 15:21:11 1992
  20. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  21. if test -f 'lib/lucas.cal' -a "${1}" != "-c" ; then 
  22.   echo shar: Will not clobber existing file \"'lib/lucas.cal'\"
  23. else
  24. echo shar: Extracting \"'lib/lucas.cal'\" \(27893 characters\)
  25. sed "s/^X//" >'lib/lucas.cal' <<'END_OF_FILE'
  26. X/*
  27. X * Copyright (c) 1992 Landon Curt Noll
  28. X * Permission is granted to use, distribute, or modify this source,
  29. X * provided that this copyright notice remains intact.
  30. X *
  31. X * By: Landon Curt Noll
  32. X *     chongo@toad.com  -or-  ...!{pyramid,sun,uunet}!sun!hoptoad!chongo
  33. X *
  34. X *
  35. X * lucas - perform a Lucas primality test on h*2^n-1
  36. X *
  37. X * HISTORICAL NOTE:
  38. X *
  39. X * On 6 August 1989 at 00:53 PDT, the 'Amdahl 6', a team consisting of
  40. X * John Brown, Landon Curt Noll, Bodo Parady, Gene Smith, Joel Smith and
  41. X * Sergio Zarantonello proved the following 65087 digit number to be prime:
  42. X *
  43. X *                  216193
  44. X *            391581 * 2      -1
  45. X *
  46. X * The primality was demonstrated by a program implementing the test
  47. X * found in these routines.  An Amdahl 1200 takes 1987 seconds to test
  48. X * the primality of this number.  A Cray 2 took several hours to
  49. X * confirm this prime.  As of 23 Jul 1991, this prime was still the
  50. X * largest known prime.
  51. X *
  52. X * The same team also discovered the following twin prime pair:
  53. X *
  54. X *               11235           11235
  55. X *        1706595 * 2     -1    1706595 * 2     +1
  56. X *
  57. X * As of 23 Jul 1991, these primes was still the largest known twin prime pair.
  58. X *
  59. X * ON GAINING A WORLD RECORD:
  60. X *
  61. X * The routines in calc were designed to be portable, and to work on
  62. X * numbers of 'sane' size.  The 'ultra-high speed multi-precision'
  63. X * package was a highly machine dependent collection of routines tuned
  64. X * to work with very large numbers.  The heart of this package was a
  65. X * multiplication and square routine that were based on Fast Fourier
  66. X * Transforms.  Details of the FFT are to be published in a up-coming
  67. X * paper to be written by the 'Amdahl 6'.
  68. X *
  69. X * Having a fast computer, and a good multi-precision package are
  70. X * critical, but one also needs to know where to look in order to have
  71. X * a good chance at a record.  Knowing what to test is beyond the scope
  72. X * of this routine.  However the following observations are noted:
  73. X *
  74. X *    test numbers of the form h*2^n-1
  75. X *    fix a value of n and vary the value h
  76. X *    n mod 128 == 0
  77. X *    h*2^n-1 is not divisible by any small prime < 2^40
  78. X *    0 < h < 2^39
  79. X *    h*2^n+1 is not divisible by any small prime < 2^40
  80. X *
  81. X * The Mersenne test for '2^n-1' is the fastest known primality test
  82. X * for a given large numbers.  However, it is faster to search for
  83. X * primes of the form 'h*2^n-1'.  When n is around 20000, one can find
  84. X * a prime of the form 'h*2^n-1' in about 1/2 the time.
  85. X *
  86. X * Critical to understanding why 'h*2^n-1' is to observe that primes of
  87. X * the form '2^n-1' seem to bunch around "islands".  Such "islands"
  88. X * seem to be getting fewer and farther in-between, forcing the time
  89. X * for each test to grow longer and longer (worse then O(n^2 log n)).
  90. X * On the other hand, when one tests 'h*2^n-1', fixes 'n' and varies
  91. X * 'h', the time to test each number remains relatively constant.
  92. X *
  93. X * It is clearly a win to eliminate potential test candidates by
  94. X * rejecting numbers that that are divisible by 'small' primes.  We
  95. X * (the "Amdahl 6") rejected all numbers that were divisible by primes
  96. X * less than '2^40'.  We stopped looking for small factors at '2^40'
  97. X * when the rate of candidates being eliminated was slowed down to
  98. X * just a trickle.
  99. X *
  100. X * The 'n mod 128 == 0' restriction allows one to test for divisability
  101. X * of small primes more quickly.  To test of 'q' is a factor of 'k*2^n-1',
  102. X * one check to see if 'k*2^n mod q' == 1, which is the same a checking
  103. X * if 'h*(2^n mod q) mod q' == 1.  One can compute '2^n mod q' by making
  104. X * use of the following:
  105. X *
  106. X *    if
  107. X *        y = 2^x mod q
  108. X *    then
  109. X *        2^(2x) mod q   == y^2 mod q        0 bit
  110. X *        2^(2x+1) mod q == 2*y^2 mod q        1 bit
  111. X *
  112. X * The choice of which expression depends on the binary pattern of 'n'.
  113. X * Since '1' bits require an extra step (multiply by 2), one should
  114. X * select value of 'n' that contain mostly '0' bits.  The restriction
  115. X * of 'n mod 128 == 0' ensures that the bottom 7 bits of 'n' are 0.
  116. X *
  117. X * By limiting 'h' to '2^39' and eliminating all values divisible by
  118. X * small primes < twice the 'h' limit (2^40), one knows that all
  119. X * remaining candidates are relatively prime.  Thus, when a candidate
  120. X * is proven to be composite (not prime) by the big test, one knows
  121. X * that the factors for that number (whatever they may be) will not
  122. X * be the factors of another candidate.
  123. X *
  124. X * Finally, one should eliminate all values of 'h*2^n-1' where
  125. X * 'h*2^n+1' is divisable by a small primes.  The ideas behind this 
  126. X * point is beyond the scope of this program and will be discussed
  127. X * in the same up-comming paper.
  128. X */
  129. X
  130. Xglobal pprod256;    /* product of  "primes up to 256" / "primes up to 46" */
  131. Xpprod256 = 0;
  132. X
  133. Xdbg = 0;        /* 1 => print debug statements */
  134. X
  135. X/*
  136. X * lucas - lucas primality test on h*2^n-1
  137. X *
  138. X * ABOUT THE TEST:
  139. X *
  140. X * This routine will perform a primality test on h*2^n-1 based on
  141. X * the mathematics of Lucas, Lehmer and Riesel.  One should read
  142. X * the following article:
  143. X *
  144. X * Ref1:
  145. X *    "Lucasian Criteria for the Primality of N=h*2^n-1", by Hans Riesel,
  146. X *    Mathematics of Computation, Vol 23 #108, pp. 869-875, Oct 1969
  147. X *
  148. X * The following book is also useful:
  149. X *
  150. X * Ref2:
  151. X *    "Prime numbers and Computer Methods for Factorization", by Hans Riesel,
  152. X *    Birkhauser, 1985, pp 131-134, 278-285, 438-444
  153. X *
  154. X * A few useful Legendre identities may be found in:
  155. X *
  156. X * Ref3:
  157. X *    "Introduction to Analytic Number Theory", by Tom A. Apostol,
  158. X *    Springer-Verlag, 1984, p 188.
  159. X *
  160. X * This test is performed as follows:    (see Ref1, Theorem 5)
  161. X *
  162. X *    a) generate u(0)         (see the function gen_u0() below)
  163. X *
  164. X *    b) generate u(n-2) according to the rule:
  165. X *
  166. X *        u(i+1) = u(i)^2-2 mod h*2^n-1
  167. X *
  168. X *    c) h*2^n-1 is prime if and only if u(n-2) == 0        Q.E.D. :-)
  169. X *
  170. X * Now the following conditions must be true for the test to work:
  171. X *
  172. X *      n >= 2
  173. X *    h >= 1
  174. X *      h < 2^n
  175. X *    h mod 2 == 1
  176. X *
  177. X * A few misc notes:
  178. X *
  179. X * In order to reduce the number of tests, as attempt to eliminate
  180. X * any number that is divisible by a prime less than 257.  Valid prime
  181. X * candidates less than 257 are declared prime as a special case.
  182. X *
  183. X * The condition 'h mod 2 == 1' is not a problem.  Say one is testing
  184. X * 'j*2^m-1', where j is even.  If we note that:
  185. X *
  186. X *      j mod 2^x == 0 for x>0   implies   j*2^m-1 == ((j/2^x)*2^(m+x))-1,
  187. X *
  188. X * then we can let h=j/2^x and n=m+x and test 'h*2^n-1' which is the value.
  189. X * We need only consider odd values of h because we can rewrite our numbers
  190. X * do make this so.
  191. X *
  192. X * input:
  193. X *    h    the h as in h*2^n-1
  194. X *    n    the n as in h*2^n-1
  195. X *
  196. X * returns:
  197. X *    1 => h*2^n-1 is prime
  198. X *    0 => h*2^n-1 is not prime
  199. X *     -1 => a test could not be formed, or h >= 2^n, h <= 0, n <= 0
  200. X */
  201. Xdefine
  202. Xlucas(h, n)
  203. X{
  204. X    local testval;        /* h*2^n-1 */
  205. X    local shiftdown;    /* the power of 2 that divides h */
  206. X    local u;        /* the u(i) sequence value */
  207. X    local v1;        /* the v(1) generator of u(0) */
  208. X    local i;        /* u sequence cycle number */
  209. X    local oldh;        /* pre-reduced h */
  210. X    local oldn;        /* pre-reduced n */
  211. X    local bits;        /* highbit of h*2^n-1 */
  212. X
  213. X    /*
  214. X     * check arg types
  215. X     */
  216. X    if (!isint(h)) {
  217. X        ldebug("lucas", "h is non-int");
  218. X        quit "FATAL: bad args: h must be an integer";
  219. X    }
  220. X    if (!isint(n)) {
  221. X        ldebug("lucas", "n is non-int");
  222. X        quit "FATAL: bad args: n must be an integer";
  223. X    }
  224. X
  225. X    /*
  226. X     * reduce h if even
  227. X     *
  228. X     * we will force h to be odd by moving powers of two over to 2^n
  229. X     */
  230. X    oldh = h;
  231. X    oldn = n;
  232. X    shiftdown = fcnt(h,2);  /* h % 2^shiftdown == 0, max shiftdown */
  233. X    if (shiftdown > 0) {
  234. X        h >>= shiftdown;
  235. X        n += shiftdown;
  236. X    }
  237. X
  238. X    /*
  239. X     * enforce the 0 < h < 2^n rule
  240. X     */
  241. X    if (h <= 0 || n <= 0) {
  242. X        print "ERROR: reduced args violate the rule: 0 < h < 2^n";
  243. X        print "    ERROR: h=":oldh, "n=":oldn, "reduced h=":h, "n=":n;
  244. X        ldebug("lucas", "unknown: h <= 0 || n <= 0");
  245. X        return -1;
  246. X    }
  247. X    if (highbit(h) >= n) {
  248. X        print "ERROR: reduced args violate the rule: h < 2^n";
  249. X        print "    ERROR: h=":oldh, "n=":oldn, "reduced h=":h, "n=":n;
  250. X        ldebug("lucas", "unknown: highbit(h) >= n");
  251. X        return -1;
  252. X    }
  253. X
  254. X    /*
  255. X     * catch the degenerate case of h*2^n-1 == 1
  256. X     */
  257. X    if (h == 1 && n == 1) {
  258. X        ldebug("lucas", "not prime: h == 1 && n == 1");
  259. X        return 0;    /* 1*2^1-1 == 1 is not prime */
  260. X    }
  261. X
  262. X    /*
  263. X     * catch the degenerate case of n==2
  264. X     *
  265. X     * n==2 and 0<h<2^n  ==>  0<h<4
  266. X     *
  267. X     * Since h is now odd  ==>  h==1 or h==3
  268. X     */
  269. X    if (h == 1 && n == 2) {
  270. X        ldebug("lucas", "prime: h == 1 && n == 2");
  271. X        return 1;    /* 1*2^2-1 == 3 is prime */
  272. X    }
  273. X    if (h == 3 && n == 2) {
  274. X        ldebug("lucas", "prime: h == 3 && n == 2");
  275. X        return 1;    /* 3*2^2-1 == 11 is prime */
  276. X    }
  277. X
  278. X    /*
  279. X     * catch small primes < 257
  280. X     *
  281. X     * We check for only a few primes because the other primes < 257
  282. X     * violate the checks above.
  283. X     */
  284. X    if (h == 1) {
  285. X        if (n == 3 || n == 5 || n == 7) {
  286. X            ldebug("lucas", "prime: 3, 7, 31, 127 are prime");
  287. X            return 1;    /* 3, 7, 31, 127 are prime */
  288. X        }
  289. X    }
  290. X    if (h == 3) {
  291. X        if (n == 2 || n == 3 || n == 4 || n == 6) {
  292. X            ldebug("lucas", "prime: 11, 23, 47, 191 are prime");
  293. X            return 1;    /* 11, 23, 47, 191 are prime */
  294. X        }
  295. X    }
  296. X    if (h == 5 && n == 4) {
  297. X        ldebug("lucas", "prime: 79 is prime");
  298. X        return 1;        /* 79 is prime */
  299. X    }
  300. X    if (h == 7 && n == 5) {
  301. X        ldebug("lucas", "prime: 223 is prime");
  302. X        return 1;        /* 223 is prime */
  303. X    }
  304. X    if (h == 15 && n == 4) {
  305. X        ldebug("lucas", "prime: 239 is prime");
  306. X        return 1;        /* 239 is prime */
  307. X    }
  308. X
  309. X    /*
  310. X     * Avoid any numbers divisable by small primes
  311. X     */
  312. X    /*
  313. X     * check for 3 <= prime factors < 29
  314. X     * pfact(28)/2 = 111546435
  315. X     */
  316. X    testval = h*2^n - 1;
  317. X    if (gcd(testval, 111546435) > 1) {
  318. X        /* a small 3 <= prime < 29 divides h*2^n-1 */
  319. X        ldebug("lucas","not-prime: 3<=prime<29 divides h*2^n-1");
  320. X        return 0;
  321. X    }
  322. X    /*
  323. X     * check for 29 <= prime factors < 47
  324. X     * pfact(46)/pfact(28) = 5864229
  325. X     */
  326. X    if (gcd(testval, 58642669) > 1) {
  327. X        /* a small 29 <= prime < 47 divides h*2^n-1 */
  328. X        ldebug("lucas","not-prime: 29<=prime<47 divides h*2^n-1");
  329. X        return 0;
  330. X    }
  331. X    /*
  332. X     * check for prime 47 <= factors < 257, if h*2^n-1 is large
  333. X     * 2^282 > pfact(256)/pfact(46) > 2^281
  334. X     */
  335. X    bits = highbit(testval);
  336. X    if (bits >= 281) {
  337. X        if (pprod256 <= 0) {
  338. X            pprod256 = pfact(256)/pfact(46);
  339. X        }
  340. X        if (gcd(testval, pprod256) > 1) {
  341. X            /* a small 47 <= prime < 257 divides h*2^n-1 */
  342. X            ldebug("lucas",\
  343. X                "not-prime: 47<=prime<257 divides h*2^n-1");
  344. X            return 0;
  345. X        }
  346. X    }
  347. X
  348. X    /*
  349. X     * try to compute u(0)
  350. X     *
  351. X     * We will use gen_v1() to give us a v(1) using the values
  352. X     * of 'h' and 'n'.  We will then use gen_u0() to convert
  353. X     * the v(1) into u(0).
  354. X     *
  355. X     * If gen_v1() returns a negative value, then we failed to
  356. X     * generate a test for h*2^n-1.  This is because h mod 3 == 0
  357. X     * is hard to do, and in rare cases, exceed the tables found
  358. X     * in this program.  We will generate an message and assume
  359. X     * the number is not prime, even though if we had a larger
  360. X     * table, we might have been able to show that it is prime.
  361. X     */
  362. X    v1 = gen_v1(h, n, testval);
  363. X    if (v1 < 0) {
  364. X        /* failure to test number */
  365. X        print "unable to compute v(1) for", h : "*2^" : n : "-1";
  366. X        ldebug("lucas", "unknown: no v(1)");
  367. X        return -1;
  368. X    }
  369. X    u = gen_u0(h, n, testval, v1);
  370. X
  371. X    /*
  372. X     * compute u(n-2)
  373. X     */
  374. X    for (i=3; i <= n; ++i) {
  375. X        u = (u^2 - 2) % testval;
  376. X    }
  377. X
  378. X    /*
  379. X     * return 1 if prime, 0 is not prime
  380. X     */
  381. X    if (u == 0) {
  382. X        ldebug("lucas", "prime: end of test");
  383. X        return 1;
  384. X    } else {
  385. X        ldebug("lucas", "not-prime: end of test");
  386. X        return 0;
  387. X    }
  388. X}
  389. X
  390. X/*
  391. X * gen_u0 - determine the initial Lucas sequence for h*2^n-1
  392. X *
  393. X * According to Ref1, Theorem 5:
  394. X *
  395. X *    u(0) = alpha^h + alpha^(-h)
  396. X *
  397. X * Now:
  398. X *
  399. X *    v(x) = alpha^x + alpha^(-x)    (Ref1, bottom of page 872)
  400. X *
  401. X * Therefore:
  402. X *
  403. X *    u(0) = v(h)
  404. X *
  405. X * We calculate v(h) as follows:    (Ref1, top of page 873)
  406. X *
  407. X *    v(0) = alpha^0 + alpha^(-0) = 2
  408. X *    v(1) = alpha^1 + alpha^(-1) = gen_v1(h,n)
  409. X *    v(n+2) = v(1)*v(n+1) - v(n)
  410. X *
  411. X * This function does not concern itself with the value of 'alpha'.
  412. X * The gen_v1() function is used to compute v(1), and identity
  413. X * functions take it from there.
  414. X *
  415. X * It can be shown that the following are true:
  416. X *
  417. X *    v(2*n) = v(n)^2 - 2
  418. X *    v(2*n+1) = v(n+1)*v(n) - v(1)
  419. X *
  420. X * To prevent v(x) from growing too large, one may replace v(x) with
  421. X * `v(x) mod h*2^n-1' at any time.
  422. X *
  423. X * See the function gen_v1() for details on the value of v(1).
  424. X *
  425. X * input:
  426. X *    h    - h as in h*2^n-1    (h mod 2 != 0)
  427. X *    n    - n as in h*2^n-1
  428. X *    testval    - h*2^n-1
  429. X *    v1    - gen_v1(h,n)        (see function below)
  430. X *
  431. X * returns:
  432. X *    u(0)    - initial value for Lucas test on h*2^n-1
  433. X *    -1    - failed to generate u(0)
  434. X */
  435. Xdefine
  436. Xgen_u0(h, n, testval, v1)
  437. X{
  438. X    local shiftdown;    /* the power of 2 that divides h */
  439. X    local r;        /* low value: v(n) */
  440. X    local s;        /* high value: v(n+1) */
  441. X    local hbits;        /* highest bit set in h */
  442. X    local i;
  443. X
  444. X    /*
  445. X     * check arg types
  446. X     */
  447. X    if (!isint(h)) {
  448. X        quit "bad args: h must be an integer";
  449. X    }
  450. X    if (!isint(n)) {
  451. X        quit "bad args: n must be an integer";
  452. X    }
  453. X    if (!isint(testval)) {
  454. X        quit "bad args: testval must be an integer";
  455. X    }
  456. X    if (!isint(v1)) {
  457. X        quit "bad args: v1 must be an integer";
  458. X    }
  459. X    if (testval <= 0) {
  460. X        quit "bogus arg: testval is <= 0";
  461. X    }
  462. X    if (v1 <= 0) {
  463. X        quit "bogus arg: v1 is <= 0";
  464. X    }
  465. X
  466. X    /*
  467. X     * enforce the h mod rules
  468. X     */
  469. X    if (h%2 == 0) {
  470. X        quit "h must not be even";
  471. X    }
  472. X
  473. X    /*
  474. X     * enforce the h > 0 and n >= 2 rules
  475. X     */
  476. X    if (h <= 0 || n < 1) {
  477. X        quit "reduced args violate the rule: 0 < h < 2^n";
  478. X    }
  479. X    hbits = highbit(h);
  480. X    if (hbits >= n) {
  481. X        quit "reduced args violate the rule: 0 < h < 2^n";
  482. X    }
  483. X
  484. X    /*
  485. X     * build up u2 based on the reversed bits of h
  486. X     */
  487. X    /* setup for bit loop */
  488. X    r = v1;
  489. X    s = (r^2 - 2);
  490. X
  491. X    /*
  492. X     * deal with small h as a special case
  493. X     *
  494. X     * The h value is odd > 0, and it needs to be
  495. X     * at least 2 bits long for the loop below to work.
  496. X     */
  497. X    if (h == 1) {
  498. X        ldebug("gen_u0", "quick h == 1 case");
  499. X        return r%testval;
  500. X    }
  501. X
  502. X    /* cycle from second highest bit to second lowest bit of h */
  503. X    for (i=hbits-1; i > 0; --i) {
  504. X
  505. X        /* bit(i) is 1 */
  506. X        if (isset(h,i)) {
  507. X
  508. X            /* compute v(2n+1) = v(r+1)*v(r)-v1 */
  509. X            r = (r*s - v1) % testval;
  510. X
  511. X            /* compute v(2n+2) = v(r+1)^2-2 */
  512. X            s = (s^2 - 2) % testval;
  513. X
  514. X        /* bit(i) is 0 */
  515. X        } else {
  516. X
  517. X            /* compute v(2n+1) = v(r+1)*v(r)-v1 */
  518. X            s = (r*s - v1) % testval;
  519. X
  520. X            /* compute v(2n) = v(r)^-2 */
  521. X            r = (r^2 - 2) % testval;
  522. X        }
  523. X    }
  524. X
  525. X    /* we know that h is odd, so the final bit(0) is 1 */
  526. X    r = (r*s - v1) % testval;
  527. X
  528. X    /* compute the final u2 return value */
  529. X    return r;
  530. X}
  531. X
  532. X/*
  533. X * Trial tables used by gen_v1()
  534. X *
  535. X * When h mod 3 == 0, one needs particular values of D, a and b (see gen_v1
  536. X * documentation) in order to find a value of v(1).
  537. X *
  538. X * This table defines 'quickmax' possible tests to be taken in ascending
  539. X * order.  The v1_qval[x] refers to a v(1) value from Ref1, Table 1.  A
  540. X * related D value is found in d_qval[x].  All D values expect d_qval[1]
  541. X * are also taken from Ref1, Table 1.  The case of D == 21 as listed in
  542. X * Ref1, Table 1 can be changed to D == 7 for the sake of the test because
  543. X * of {note 6}.
  544. X *
  545. X * It should be noted that the D values all satisfy the selection values
  546. X * as outlined in the gen_v1() function comments.  That is:
  547. X *
  548. X *       D == P*(2^f)*(3^g)
  549. X *
  550. X * where f == 0 and g == 0, P == D.  So we simply need to check that
  551. X * one of the following two cases are true:
  552. X *
  553. X *       P mod 4 ==  1  and  J(h*2^n-1 mod P, P) == -1
  554. X *       P mod 4 == -1  and  J(h*2^n-1 mod P, P) ==  1
  555. X *
  556. X * In all cases expect one case, the value of r is:
  557. X *
  558. X *       r == Q*(2^j)*(3^k)*(z^2)
  559. X *
  560. X * where Q == 1.  In these cases, no further checking is needed,
  561. X * and the v(1) value can be returned.
  562. X */
  563. Xquickmax = 8;
  564. Xmat d_qval[quickmax];
  565. Xmat v1_qval[quickmax];
  566. Xd_qval[0] = 5;        v1_qval[0] = 3;        /* a=1   b=1  r=4  */
  567. Xd_qval[1] = 7;        v1_qval[1] = 5;        /* a=3   b=1  r=12  D=21 */
  568. Xd_qval[2] = 13;        v1_qval[2] = 11;    /* a=3   b=1  r=4  */
  569. Xd_qval[3] = 11;        v1_qval[3] = 20;    /* a=3   b=1  r=2  */
  570. Xd_qval[4] = 29;        v1_qval[4] = 27;    /* a=5   b=1  r=4  */
  571. Xd_qval[5] = 53;        v1_qval[5] = 51;    /* a=53  b=1  r=4  */
  572. Xd_qval[6] = 17;        v1_qval[6] = 66;    /* a=17  b=1  r=1  */
  573. Xd_qval[7] = 19;        v1_qval[7] = 74;    /* a=38  b=1  r=2  */
  574. X
  575. X/*
  576. X * gen_v1 - compute the v(1) for a given h*2^n-1 if we can
  577. X *
  578. X * This function assumes:
  579. X *
  580. X *    n > 2            (n==2 has already been eliminated)
  581. X *    h mod 2 == 1
  582. X *    h < 2^n
  583. X *    h*2^n-1 mod 3 != 0    (h*2^n-1 has no small factors, such as 3)
  584. X *
  585. X * The generation of v(1) depends on the value of h.  There are two cases
  586. X * to consider, h mod 3 != 0, and h mod 3 == 0.
  587. X *
  588. X ***
  589. X *
  590. X * Case 1:    (h mod 3 != 0)
  591. X *
  592. X * This case is easy and always finds v(1).
  593. X *
  594. X * In Ref1, page 869, one finds that if:    (or see Ref2, page 131-132)
  595. X *
  596. X *    h mod 6 == +/-1
  597. X *    h*2^n-1 mod 3 != 0
  598. X *
  599. X * which translates, gives the functions assumptions, into the condition:
  600. X *
  601. X *    h mod 3 != 0
  602. X *
  603. X * If this case condition is true, then:
  604. X *
  605. X *    u(0) = (2+sqrt(3))^h + (2-sqrt(3))^h        (see Ref1, page 869)
  606. X *         = (2+sqrt(3))^h + (2+sqrt(3))^(-h)
  607. X *
  608. X * and since Ref1, Theorem 5 states:
  609. X *
  610. X *    u(0) = alpha^h + alpha^(-h)
  611. X *    r = abs(2^2 - 1^2*3) = 1
  612. X *
  613. X * and the bottom of Ref1, page 872 states:
  614. X *
  615. X *    v(x) = alpha^x + alpha^(-x)
  616. X *
  617. X * If we let:
  618. X *
  619. X *    alpha = (2+sqrt(3))
  620. X *
  621. X * then
  622. X *
  623. X *    u(0) = v(h)
  624. X *
  625. X * so we simply return
  626. X *
  627. X *    v(1) = alpha^1 + alpha^(-1)
  628. X *         = (2+sqrt(3)) + (2-sqrt(3))
  629. X *         = 4
  630. X *
  631. X ***
  632. X *
  633. X * Case 2:    (h mod 3 == 0)
  634. X *
  635. X * This case is not so easy and finds v(1) in most all cases.  In this
  636. X * version of this program, we will simply return -1 (failure) if we
  637. X * hit one of the cases that fall thru the cracks.  This does not happen
  638. X * often, so this is not too bad.
  639. X *
  640. X * Ref1, Theorem 5 contains the following definitions:
  641. X *
  642. X *        r = abs(a^2 - b^2*D)
  643. X *        alpha = (a + b*sqrt(D))^2/r
  644. X *
  645. X * where D is 'square free', and 'alpha = epsilon^s' (for some s>0) are units
  646. X * in the quadratic field K(sqrt(D)).
  647. X *
  648. X * One can find possible values for a, b and D in Ref1, Table 1 (page 872).
  649. X * (see the file lucas_tbl.cal)
  650. X *
  651. X * Now Ref1, Theorem 5 states that if:
  652. X *
  653. X *    L(D, h*2^n-1) = -1                [condition 1]
  654. X *    L(r, h*2^n-1) * (a^2 - b^2*D)/r = -1        [condition 2]
  655. X *
  656. X * where L(x,y) is the Legendre symbol (see below), then:
  657. X *
  658. X *    u(0) = alpha^h + alpha^(-h)
  659. X *
  660. X * The bottom of Ref1, page 872 states:
  661. X *
  662. X *    v(x) = alpha^x + alpha^(-x)
  663. X *
  664. X * thus since:
  665. X *
  666. X *    u(0) = v(h)
  667. X *
  668. X * so we want to return:
  669. X *
  670. X *    v(1) = alpha^1 + alpha^(-1)
  671. X *
  672. X * Therefore we need to take a given (D,a,b), determine if the two conditions
  673. X * are true, and return the related v(1).
  674. X *
  675. X * Before we address the two conditions, we need some background information
  676. X * on two symbols, Legendre and Jacobi.  In Ref 2, pp 278, 284-285, we find
  677. X * the following definitions of J(a,p) and L(a,n):
  678. X *
  679. X * The Legendre symbol L(a,p) takes the value:
  680. X *
  681. X *    L(a,p) == 1    => a is a quadratic residue of p
  682. X *    L(a,p) == -1    => a is NOT a quadratic residue of p
  683. X *
  684. X * when
  685. X *
  686. X *    p is prime
  687. X *    p mod 2 == 1
  688. X *    gcd(a,p) == 1
  689. X *
  690. X * The value x is a quadratic residue of y if there exists some integer z
  691. X * such that:
  692. X *
  693. X *    z^2 mod y == x
  694. X *
  695. X * The Jacobi symbol J(x,y) takes the value:
  696. X *
  697. X *    J(x,y) == 1    => y is not prime, or x is a quadratic residue of y
  698. X *    J(x,y) == -1    => x is NOT a quadratic residue of y
  699. X *
  700. X * when
  701. X *
  702. X *    y mod 2 == 1
  703. X *    gcd(x,y) == 1
  704. X *
  705. X * In the following comments on Legendre and Jacobi identities, we shall
  706. X * assume that the arguments to the symbolic are valid over the symbol
  707. X * definitions as stated above.
  708. X *
  709. X * In Ref2, pp 280-284, we find that:
  710. X *
  711. X *    L(a,p)*L(b,p) == L(a*b,p)                {A3.5}
  712. X *    J(x,y)*J(z,y) == J(x*z,y)                {A3.14}
  713. X *    L(a,p) == L(p,a) * (-1)^((a-1)*(p-1)/4)            {A3.8}
  714. X *    J(x,y) == J(y,x) * (-1)^((x-1)*(y-1)/4)            {A3.17}
  715. X *
  716. X * The equality L(a,p) == J(a,p) when:                {note 0}
  717. X *
  718. X *    p is prime
  719. X *    p mod 2 == 1
  720. X *    gcd(a,p) == 1
  721. X *
  722. X * It can be shown that (see Ref3):
  723. X *
  724. X *    L(a,p) == L(a mod p, p)                    {note 1}
  725. X *    L(z^2, p) == 1                        {note 2}
  726. X *
  727. X * From Ref2, table 32:
  728. X *
  729. X *    p mod 8 == +/-1   implies  L(2,p) == 1            {note 3}
  730. X *    p mod 12 == +/-1  implies  L(3,p) == 1            {note 4}
  731. X *
  732. X * Since h*2^n-1 mod 8 == -1, for n>2, note 3 implies:
  733. X *
  734. X *    L(2, h*2^n-1) == 1            (n>2)        {note 5}
  735. X *
  736. X * Since h=3*A, h*2^n-1 mod 12 == -1, for A>0, note 4 implies:
  737. X *
  738. X *    L(3, h*2^n-1) == 1                    {note 6}
  739. X *
  740. X * By use of {A3.5}, {note 2}, {note 5} and {note 6}, one can show:
  741. X *
  742. X *    L((2^g)*(3^l)*(z^2), h*2^n-1) == 1  (g>=0,l>=0,z>0,n>2)    {note 7}
  743. X *
  744. X * Returning to the testing of conditions, take condition 1:
  745. X *
  746. X *    L(D, h*2^n-1) == -1            [condition 1]
  747. X *
  748. X * In order for J(D, h*2^n-1) to be defined, we must ensure that D
  749. X * is not a factor of h*2^n-1.  This is done by pre-screening h*2^n-1 to
  750. X * not have small factors and selecting D less than that factor check limit.
  751. X *
  752. X * By use of {note 7}, we can show that when we choose D to be:
  753. X *
  754. X *    D is square free
  755. X *    D = P*(2^f)*(3^g)            (P is prime>2)
  756. X *
  757. X * The square free condition implies f = 0 or 1, g = 0 or 1.  If f and g
  758. X * are both 1, P must be a prime > 3.
  759. X *
  760. X * So given such a D value:
  761. X *
  762. X *    L(D, h*2^n-1) == L(P*(2^g)*(3^l), h*2^n-1)
  763. X *              == L(P, h*2^n-1) * L((2^g)*(3^l), h*2^n-1)       {A3.5}
  764. X *              == L(P, h*2^n-1) * 1                   {note 7}
  765. X *              == L(h*2^n-1, P)*(-1)^((h*2^n-2)*(P-1)/4)           {A3.8}
  766. X *              == L(h*2^n-1 mod P, P)*(-1)^((h*2^n-2)*(P-1)/4)  {note 1}
  767. X *              == J(h*2^n-1 mod P, P)*(-1)^((h*2^n-2)*(P-1)/4)  {note 0}
  768. X *
  769. X * When does J(h*2^n-1 mod P, P)*(-1)^((h*2^n-2)*(P-1)/4) take the value of -1,
  770. X * thus satisfy [condition 1]?  The answer depends on P.  Now P is a prime>2,
  771. X * thus P mod 4 == 1 or -1.
  772. X *
  773. X * Take P mod 4 == 1:
  774. X *
  775. X *    P mod 4 == 1  implies  (-1)^((h*2^n-2)*(P-1)/4) == 1
  776. X *
  777. X * Thus:
  778. X *
  779. X *    L(D, h*2^n-1) == L(h*2^n-1 mod P, P) * (-1)^((h*2^n-2)*(P-1)/4)
  780. X *              == L(h*2^n-1 mod P, P)
  781. X *              == J(h*2^n-1 mod P, P)
  782. X *
  783. X * Take P mod 4 == -1:
  784. X *
  785. X *    P mod 4 == -1  implies  (-1)^((h*2^n-2)*(P-1)/4) == -1
  786. X *
  787. X * Thus:
  788. X *
  789. X *    L(D, h*2^n-1) == L(h*2^n-1 mod P, P) * (-1)^((h*2^n-2)*(P-1)/4)
  790. X *              == L(h*2^n-1 mod P, P) * -1
  791. X *              == -J(h*2^n-1 mod P, P)
  792. X *
  793. X * Therefore [condition 1] is met if, and only if, one of the following
  794. X * to cases are true:
  795. X *
  796. X *    P mod 4 ==  1  and  J(h*2^n-1 mod P, P) == -1
  797. X *    P mod 4 == -1  and  J(h*2^n-1 mod P, P) ==  1
  798. X *
  799. X * Now consider [condition 2]:
  800. X *
  801. X *    L(r, h*2^n-1) * (a^2 - b^2*D)/r == -1    [condition 2]
  802. X *
  803. X * We select only a, b, r and D values where:
  804. X *
  805. X *    (a^2 - b^2*D)/r == -1
  806. X *
  807. X * Therefore in order for [condition 2] to be met, we must show that:
  808. X *
  809. X *    L(r, h*2^n-1) == 1
  810. X *
  811. X * If we select r to be of the form:
  812. X *
  813. X *    r == (2^j)*(3^k)*(z^2)            (j>=0, k>=0, z>0)
  814. X *
  815. X * then by use of {note 7}:
  816. X *
  817. X *    L(r, h*2^n-1) == L((2^j)*(3^k)*(z^2), h*2^n-1)
  818. X *              == 1                           {note 2}
  819. X *
  820. X * and thus, [condition 2] is met.
  821. X *
  822. X * If we select r to be of the form:
  823. X *
  824. X *    r == Q*(2^j)*(3^k)*(z^2)        (Q is prime>2, j>=0, k>=0, z>0)
  825. X *
  826. X * then by use of {note 7}:
  827. X *
  828. X *    L(r, h*2^n-1) == L(Q*(2^j)*(3^k)*(z^2), h*2^n-1)
  829. X *              == L(Q, h*2^n-1) * L((2^j)*(3^k)*(z^2), h*2^n-1) {A3.5}
  830. X *              == L(Q, h*2^n-1) * 1                   {note 2}
  831. X *              == L(h*2^n-1, Q) * (-1)^((h*2^n-2)*(Q-1)/4)      {A3.8}
  832. X *              == L(h*2^n-1 mod Q, Q)*(-1)^((h*2^n-2)*(Q-1)/4)  {note 1}
  833. X *              == J(h*2^n-1 mod Q, Q)*(-1)^((h*2^n-2)*(Q-1)/4)  {note 0}
  834. X *
  835. X * When does J(h*2^n-1 mod Q, Q)*(-1)^((h*2^n-2)*(Q-1)/4) take the value of 1,
  836. X * thus satisfy [condition 2]?  The answer depends on Q.  Now Q is a prime>2,
  837. X * thus Q mod 4 == 1 or -1.
  838. X *
  839. X * Take Q mod 4 == 1:
  840. X *
  841. X *    Q mod 4 == 1  implies  (-1)^((h*2^n-2)*(Q-1)/4) == 1
  842. X *
  843. X * Thus:
  844. X *
  845. X *    L(D, h*2^n-1) == L(h*2^n-1 mod Q, Q) * (-1)^((h*2^n-2)*(Q-1)/4)
  846. X *              == L(h*2^n-1 mod Q, Q)
  847. X *              == J(h*2^n-1 mod Q, Q)
  848. X *
  849. X * Take Q mod 4 == -1:
  850. X *
  851. X *    Q mod 4 == -1  implies  (-1)^((h*2^n-2)*(Q-1)/4) == -1
  852. X *
  853. X * Thus:
  854. X *
  855. X *    L(D, h*2^n-1) == L(h*2^n-1 mod Q, Q) * (-1)^((h*2^n-2)*(Q-1)/4)
  856. X *              == L(h*2^n-1 mod Q, Q) * -1
  857. X *              == -J(h*2^n-1 mod Q, Q)
  858. X *
  859. X * Therefore [condition 2] is met by selecting  D = Q*(2^j)*(3^k)*(z^2),
  860. X * where Q is prime>2, j>=0, k>=0, z>0; if and only if one of the following
  861. X * to cases are true:
  862. X *
  863. X *    Q mod 4 ==  1  and  J(h*2^n-1 mod Q, Q) == 1
  864. X *    Q mod 4 == -1  and  J(h*2^n-1 mod Q, Q) == -1
  865. X *
  866. X ***
  867. X *
  868. X * In conclusion, we can compute v(1) by attempting to do the following:
  869. X *
  870. X * h mod 3 != 0
  871. X *
  872. X *     we return:
  873. X *
  874. X *       v(1) == 4
  875. X *
  876. X * h mod 3 == 0
  877. X *
  878. X *     define:
  879. X *
  880. X *       r == abs(a^2 - b^2*D)
  881. X *       alpha == (a + b*sqrt(D))^2/r
  882. X *
  883. X *     we return:
  884. X *
  885. X *       v(1) = alpha^1 + alpha^(-1)
  886. X *
  887. X *     if and only if we can find a given a, b, D that obey all the
  888. X *     following selection rules:
  889. X *
  890. X *       D is square free
  891. X *
  892. X *       D == P*(2^f)*(3^g)        (P is prime>2, f,g == 0 or 1)
  893. X *
  894. X *       alpha = epsilon^s        (s>0, epsilon fundamental unit
  895. X *                          of K(sqrt(D)))
  896. X *
  897. X *       r == Q*(2^j)*(3^k)*(z^2)    (Q==1 or Q is prime>2, j>=0, k>=0, z>0)
  898. X *
  899. X *         one of the following is true:
  900. X *           P mod 4 ==  1  and  J(h*2^n-1 mod P, P) == -1
  901. X *           P mod 4 == -1  and  J(h*2^n-1 mod P, P) ==  1
  902. X *
  903. X *       if Q is prime, then one of the following is true:
  904. X *           Q mod 4 ==  1  and  J(h*2^n-1 mod Q, Q) == 1
  905. X *           Q mod 4 == -1  and  J(h*2^n-1 mod Q, Q) == -1
  906. X *
  907. X *     If we cannot find a v(1) quickly enough, then we will give up
  908. X *     testing h*2^n-1.  This does not happen too often, so this hack
  909. X *     is not too bad.
  910. X *
  911. X ***
  912. X *
  913. X * input:
  914. X *    h    h as in h*2^n-1
  915. X *    n    n as in h*2^n-1
  916. X *
  917. X * output:
  918. X *    returns v(1), or -1 is there is no quick way
  919. X */
  920. Xdefine
  921. Xgen_v1(h, n)
  922. X{
  923. X    local d;    /* the 'D' value to try */
  924. X    local val_mod;    /* h*2^n-1 mod 'D' */
  925. X    local i;
  926. X
  927. X    /*
  928. X     * check for case 1
  929. X     */
  930. X    if (h % 3 != 0) {
  931. X        /* v(1) is easy to compute */
  932. X        return 4;
  933. X    }
  934. X
  935. X    /*
  936. X     * We will try all 'D' values until we find a proper v(1)
  937. X     * or run out of 'D' values.
  938. X     */
  939. X    for (i=0; i < quickmax; ++i) {
  940. X
  941. X        /* grab our 'D' value */
  942. X        d = d_qval[i];
  943. X
  944. X        /* compute h*2^n-1 mod 'D' quickly */
  945. X        val_mod = (h*pmod(2,n%(d-1),d)-1) % d;
  946. X
  947. X        /*
  948. X         * if 'D' mod 4 == 1, then
  949. X         *    (h*2^n-1) mod 'D' can not be a quadratic residue of 'D'
  950. X         * else
  951. X         *    (h*2^n-1) mod 'D' must be a quadratic residue of 'D'
  952. X         */
  953. X        if (d%4 == 1) {
  954. X            /* D mod 4 == 1, so check for J(D, h*2^n-1) == -1 */
  955. X            if (jacobi(val_mod, d) == -1) {
  956. X                /* it worked, return the related v(1) value */
  957. X                return v1_qval[i];
  958. X            }
  959. X        } else {
  960. X            /* D mod 4 == -1, so check for J(D, h*2^n-1) == 1 */
  961. X            if (jacobi(val_mod, d) == 1) {
  962. X                /* it worked, return the related v(1) value */
  963. X                return v1_qval[i];
  964. X            }
  965. X        }
  966. X    }
  967. X
  968. X    /*
  969. X     * This is an example of a more complex proof construction.
  970. X     * The code above will not be able to find the v(1) for:
  971. X     *
  972. X     *            81*2^81-1
  973. X     *
  974. X     * We will check with:
  975. X     *
  976. X     *    v(1)=81      D=6557      a=79      b=1      r=31
  977. X     *
  978. X     * Now, D==79*83 and r=79*2^2.  If we show that:
  979. X     *
  980. X     *    J(h*2^n-1 mod 79, 79) == -1
  981. X     *    J(h*2^n-1 mod 83, 83) == 1
  982. X     *
  983. X     * then we will satisfy [condition 1].  Observe:
  984. X     *
  985. X      *    79 mod 4 == -1  implies  (-1)^((h*2^n-2)*(79-1)/4) == -1
  986. X      *    83 mod 4 == -1  implies  (-1)^((h*2^n-2)*(83-1)/4) == -1
  987. X     *
  988. X     *    J(D, h*2^n-1) == J(83, h*2^n-1) * J(79, h*2^n-1)
  989. X     *              == J(h*2^n-1, 83) * (-1)^((h*2^n-2)*(83-1)/4) *
  990. X     *                 J(h*2^n-1, 79) * (-1)^((h*2^n-2)*(79-1)/4)
  991. X     *              == J(h*2^n-1 mod 83, 83) * -1 *
  992. X     *                 J(h*2^n-1 mod 79, 79) * -1
  993. X     *              ==  1 * -1 *
  994. X     *             -1 * -1
  995. X     *              == -1
  996. X     *
  997. X     * We will also satisfy [condition 2].  Observe:
  998. X     *
  999. X     *    (a^2 - b^2*D)/r == (79^2 - 1^1*6557)/31
  1000. X     *            == -1
  1001. X     *
  1002. X     *    L(r, h*2^n-1) == L(Q*(2^j)*(3^k)*(z^2), h*2^n-1)
  1003. X     *              == L(79, h*2^n-1) * L(2^2, h*2^n-1)
  1004. X     *              == L(79, h*2^n-1) * 1
  1005. X     *              == L(h*2^n-1, 79) * (-1)^((h*2^n-2)*(79-1)/4)
  1006. X     *              == L(h*2^n-1, 79) * -1
  1007. X     *              == L(h*2^n-1 mod 79, 79) * -1
  1008. X     *              == J(h*2^n-1 mod 79, 79) * -1
  1009. X     *              == -1 * -1
  1010. X     *              == 1
  1011. X     */
  1012. X    if (jacobi( ((h*pmod(2,n%(79-1),79)-1)%79), 79 ) == -1 &&
  1013. X        jacobi( ((h*pmod(2,n%(83-1),83)-1)%83), 83 ) == 1) {
  1014. X        /* return the associated v(1)=81 */
  1015. X        return 81;
  1016. X    }
  1017. X
  1018. X    /* no quick and dirty v(1), so return -1 */
  1019. X    return -1;
  1020. X}
  1021. X
  1022. X/*
  1023. X * ldebug - print a debug statement
  1024. X *
  1025. X * input:
  1026. X *    funct    name of calling function
  1027. X *    str    string to print
  1028. X */
  1029. Xdefine
  1030. Xldebug(funct, str)
  1031. X{
  1032. X    if (dbg == 1) {
  1033. X        print "DEBUG:", funct:":", str;
  1034. X    }
  1035. X    return;
  1036. X}
  1037. X
  1038. Xglobal lib_debug;
  1039. Xif (!isnum(lib_debug) || lib_debug>0) print "lucas(h, n) defined";
  1040. END_OF_FILE
  1041. if test 27893 -ne `wc -c <'lib/lucas.cal'`; then
  1042.     echo shar: \"'lib/lucas.cal'\" unpacked with wrong size!
  1043. fi
  1044. # end of 'lib/lucas.cal'
  1045. fi
  1046. echo shar: End of archive 13 \(of 21\).
  1047. cp /dev/null ark13isdone
  1048. MISSING=""
  1049. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ; do
  1050.     if test ! -f ark${I}isdone ; then
  1051.     MISSING="${MISSING} ${I}"
  1052.     fi
  1053. done
  1054. if test "${MISSING}" = "" ; then
  1055.     echo You have unpacked all 21 archives.
  1056.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1057. else
  1058.     echo You still need to unpack the following archives:
  1059.     echo "        " ${MISSING}
  1060. fi
  1061. ##  End of shell archive.
  1062. exit 0
  1063.