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

  1. Newsgroups: comp.sources.unix
  2. From: dbell@pdact.pd.necisa.oz.au (David I. Bell)
  3. Subject: v26i044: CALC - An arbitrary precision C-like calculator, Part18/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 44
  9. Archive-Name: calc/part18
  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 18 (of 21)."
  18. # Contents:  zfunc.c
  19. # Wrapped by dbell@elm on Tue Feb 25 15:21:16 1992
  20. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  21. if test -f 'zfunc.c' -a "${1}" != "-c" ; then 
  22.   echo shar: Will not clobber existing file \"'zfunc.c'\"
  23. else
  24. echo shar: Extracting \"'zfunc.c'\" \(34345 characters\)
  25. sed "s/^X//" >'zfunc.c' <<'END_OF_FILE'
  26. X/*
  27. X * Copyright (c) 1992 David I. Bell
  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 * Extended precision integral arithmetic non-primitive routines
  32. X */
  33. X
  34. X#include "math.h"
  35. X
  36. Xstatic ZVALUE primeprod;        /* product of primes under 100 */
  37. XZVALUE _tenpowers_[32];            /* table of 10^2^n */
  38. X
  39. X#if 0
  40. Xstatic char *abortmsg = "Calculation aborted";
  41. Xstatic char *memmsg = "Not enough memory";
  42. X#endif
  43. X
  44. X
  45. X/*
  46. X * Compute the factorial of a number.
  47. X */
  48. Xvoid
  49. Xzfact(z, dest)
  50. X    ZVALUE z, *dest;
  51. X{
  52. X    long ptwo;        /* count of powers of two */
  53. X    long n;            /* current multiplication value */
  54. X    long m;            /* reduced multiplication value */
  55. X    long mul;        /* collected value to multiply by */
  56. X    ZVALUE res, temp;
  57. X
  58. X    if (isneg(z))
  59. X        error("Negative argument for factorial");
  60. X    if (isbig(z))
  61. X        error("Very large factorial");
  62. X    n = (istiny(z) ? z1tol(z) : z2tol(z));
  63. X    ptwo = 0;
  64. X    mul = 1;
  65. X    res = _one_;
  66. X    /*
  67. X     * Multiply numbers together, but squeeze out all powers of two.
  68. X     * We will put them back in at the end.  Also collect multiple
  69. X     * numbers together until there is a risk of overflow.
  70. X     */
  71. X    for (; n > 1; n--) {
  72. X        for (m = n; ((m & 0x1) == 0); m >>= 1)
  73. X            ptwo++;
  74. X        mul *= m;
  75. X        if (mul < BASE1/2)
  76. X            continue;
  77. X        zmuli(res, mul, &temp);
  78. X        freeh(res.v);
  79. X        res = temp;
  80. X        mul = 1;
  81. X    }
  82. X    /*
  83. X     * Multiply by the remaining value, then scale result by
  84. X     * the proper power of two.
  85. X     */
  86. X    if (mul > 1) {
  87. X        zmuli(res, mul, &temp);
  88. X        freeh(res.v);
  89. X        res = temp;
  90. X    }
  91. X    zshift(res, ptwo, &temp);
  92. X    freeh(res.v);
  93. X    *dest = temp;
  94. X}
  95. X
  96. X
  97. X/*
  98. X * Compute the product of the primes up to the specified number.
  99. X */
  100. Xvoid
  101. Xzpfact(z, dest)
  102. X    ZVALUE z, *dest;
  103. X{
  104. X    long n;            /* limiting number to multiply by */
  105. X    long p;            /* current prime */
  106. X    long i;            /* test value */
  107. X    long mul;        /* collected value to multiply by */
  108. X    ZVALUE res, temp;
  109. X
  110. X    if (isneg(z))
  111. X        error("Negative argument for factorial");
  112. X    if (isbig(z))
  113. X        error("Very large factorial");
  114. X    n = (istiny(z) ? z1tol(z) : z2tol(z));
  115. X    /*
  116. X     * Multiply by the primes in order, collecting multiple numbers
  117. X     * together until there is a change of overflow.
  118. X     */
  119. X    mul = 1 + (n > 1);
  120. X    res = _one_;
  121. X    for (p = 3; p <= n; p += 2) {
  122. X        for (i = 3; (i * i) <= p; i += 2) {
  123. X            if ((p % i) == 0)
  124. X                goto next;
  125. X        }
  126. X        mul *= p;
  127. X        if (mul < BASE1/2)
  128. X            continue;
  129. X        zmuli(res, mul, &temp);
  130. X        freeh(res.v);
  131. X        res = temp;
  132. X        mul = 1;
  133. Xnext: ;
  134. X    }
  135. X    /*
  136. X     * Multiply by the final value if any.
  137. X     */
  138. X    if (mul > 1) {
  139. X        zmuli(res, mul, &temp);
  140. X        freeh(res.v);
  141. X        res = temp;
  142. X    }
  143. X    *dest = res;
  144. X}
  145. X
  146. X
  147. X/*
  148. X * Compute the least common multiple of all the numbers up to the
  149. X * specified number.
  150. X */
  151. Xvoid
  152. Xzlcmfact(z, dest)
  153. X    ZVALUE z, *dest;
  154. X{
  155. X    long n;            /* limiting number to multiply by */
  156. X    long p;            /* current prime */
  157. X    long pp;        /* power of prime */
  158. X    long i;            /* test value */
  159. X    ZVALUE res, temp;
  160. X
  161. X    if (isneg(z) || iszero(z))
  162. X        error("Non-positive argument for lcmfact");
  163. X    if (isbig(z))
  164. X        error("Very large lcmfact");
  165. X    n = (istiny(z) ? z1tol(z) : z2tol(z));
  166. X    /*
  167. X     * Multiply by powers of the necessary odd primes in order.
  168. X     * The power for each prime is the highest one which is not
  169. X     * more than the specified number.
  170. X     */
  171. X    res = _one_;
  172. X    for (p = 3; p <= n; p += 2) {
  173. X        for (i = 3; (i * i) <= p; i += 2) {
  174. X            if ((p % i) == 0)
  175. X                goto next;
  176. X        }
  177. X        i = p;
  178. X        while (i <= n) {
  179. X            pp = i;
  180. X            i *= p;
  181. X        }
  182. X        zmuli(res, pp, &temp);
  183. X        freeh(res.v);
  184. X        res = temp;
  185. Xnext: ;
  186. X    }
  187. X    /*
  188. X     * Finish by scaling by the necessary power of two.
  189. X     */
  190. X    zshift(res, zhighbit(z), dest);
  191. X    freeh(res.v);
  192. X}
  193. X
  194. X
  195. X/*
  196. X * Compute the permuation function  M! / (M - N)!.
  197. X */
  198. Xvoid
  199. Xzperm(z1, z2, res)
  200. X    ZVALUE z1, z2, *res;
  201. X{
  202. X    long count;
  203. X    ZVALUE cur, tmp, ans;
  204. X
  205. X    if (isneg(z1) || isneg(z2))
  206. X        error("Negative argument for permutation");
  207. X    if (zrel(z1, z2) < 0)
  208. X        error("Second arg larger than first in permutation");
  209. X    if (isbig(z2))
  210. X        error("Very large permutation");
  211. X    count = (istiny(z2) ? z1tol(z2) : z2tol(z2));
  212. X    zcopy(z1, &ans);
  213. X    zsub(z1, _one_, &cur);
  214. X    while (--count > 0) {
  215. X        zmul(ans, cur, &tmp);
  216. X        freeh(ans.v);
  217. X        ans = tmp;
  218. X        zsub(cur, _one_, &tmp);
  219. X        freeh(cur.v);
  220. X        cur = tmp;
  221. X    }
  222. X    freeh(cur.v);
  223. X    *res = ans;
  224. X}
  225. X
  226. X
  227. X/*
  228. X * Compute the combinatorial function  M! / ( N! * (M - N)! ).
  229. X */
  230. Xvoid
  231. Xzcomb(z1, z2, res)
  232. X    ZVALUE z1, z2, *res;
  233. X{
  234. X    ZVALUE ans;
  235. X    ZVALUE mul, div, temp;
  236. X    FULL count, i;
  237. X    HALF dh[2];
  238. X
  239. X    if (isneg(z1) || isneg(z2))
  240. X        error("Negative argument for combinatorial");
  241. X    zsub(z1, z2, &temp);
  242. X    if (isneg(temp)) {
  243. X        freeh(temp.v);
  244. X        error("Second arg larger than first for combinatorial");
  245. X    }
  246. X    if (isbig(z2) && isbig(temp)) {
  247. X        freeh(temp.v);
  248. X        error("Very large combinatorial");
  249. X    }
  250. X    count = (istiny(z2) ? z1tol(z2) : z2tol(z2));
  251. X    i = (istiny(temp) ? z1tol(temp) : z2tol(temp));
  252. X    if (isbig(z2) || (!isbig(temp) && (i < count)))
  253. X        count = i;
  254. X    freeh(temp.v);
  255. X    mul = z1;
  256. X    div.sign = 0;
  257. X    div.v = dh;
  258. X    ans = _one_;
  259. X    for (i = 1; i <= count; i++) {
  260. X        dh[0] = i & BASE1;
  261. X        dh[1] = i / BASE;
  262. X        div.len = 1 + (dh[1] != 0);
  263. X        zmul(ans, mul, &temp);
  264. X        freeh(ans.v);
  265. X        zquo(temp, div, &ans);
  266. X        freeh(temp.v);
  267. X        zsub(mul, _one_, &temp);
  268. X        if (mul.v != z1.v)
  269. X            freeh(mul.v);
  270. X        mul = temp;
  271. X    }
  272. X    if (mul.v != z1.v)
  273. X        freeh(mul.v);
  274. X    *res = ans;
  275. X}
  276. X
  277. X
  278. X/*
  279. X * Perform a probabilistic primality test (algorithm P in Knuth).
  280. X * Returns FALSE if definitely not prime, or TRUE if probably prime.
  281. X * Count determines how many times to check for primality.
  282. X * The chance of a non-prime passing this test is less than (1/4)^count.
  283. X * For example, a count of 100 fails for only 1 in 10^60 numbers.
  284. X */
  285. XBOOL
  286. Xzprimetest(z, count)
  287. X    ZVALUE z;        /* number to test for primeness */
  288. X    long count;
  289. X{
  290. X    long ij, ik, ix;
  291. X    ZVALUE zm1, z1, z2, z3, ztmp;
  292. X    HALF val[2];
  293. X
  294. X    z.sign = 0;
  295. X    if (iseven(z))        /* if even, not prime if not 2 */
  296. X        return (istwo(z) != 0);
  297. X    /*
  298. X     * See if the number is small, and is either a small prime,
  299. X     * or is divisible by a small prime.
  300. X     */
  301. X    if (istiny(z) && (*z.v <= (HALF)(101*101-1))) {
  302. X        ix = *z.v;
  303. X        for (ik = 3; (ik <= 97) && ((ik * ik) <= ix); ik += 2)
  304. X            if ((ix % ik) == 0)
  305. X                return FALSE;
  306. X        return TRUE;
  307. X    }
  308. X    /*
  309. X     * See if the number is divisible by one of the primes 3, 5,
  310. X     * 7, 11, or 13.  This is a very easy check.
  311. X     */
  312. X    ij = zmodi(z, 15015L);
  313. X    if (!(ij % 3) || !(ij % 5) || !(ij % 7) || !(ij % 11) || !(ij % 13))
  314. X        return FALSE;
  315. X    /*
  316. X     * Check the gcd of the number and the product of more of the first
  317. X     * few odd primes.  We must build the prime product on the first call.
  318. X     */
  319. X    ztmp.sign = 0;
  320. X    ztmp.len = 1;
  321. X    ztmp.v = val;
  322. X    if (primeprod.len == 0) {
  323. X        val[0] = 101;
  324. X        zpfact(ztmp, &primeprod);
  325. X    }
  326. X    zgcd(z, primeprod, &z1);
  327. X    if (!isunit(z1)) {
  328. X        freeh(z1.v);
  329. X        return FALSE;
  330. X    }
  331. X    freeh(z1.v);
  332. X    /*
  333. X     * Not divisible by a small prime, so onward with the real test.
  334. X     * Make sure the count is limited by the number of odd numbers between
  335. X     * three and the number being tested.
  336. X     */
  337. X    ix = ((istiny(z) ? z1tol(z) : z2tol(z) - 3) / 2);
  338. X    if (count > ix) count = ix;
  339. X    zsub(z, _one_, &zm1);
  340. X    ik = zlowbit(zm1);
  341. X    zshift(zm1, -ik, &z1);
  342. X    /*
  343. X     * Loop over various "random" numbers, testing each one.
  344. X     * These numbers are the odd numbers starting from three.
  345. X     */
  346. X    for (ix = 0; ix < count; ix++) {
  347. X        val[0] = (ix * 2) + 3;
  348. X        ij = 0;
  349. X        zpowermod(ztmp, z1, z, &z3);
  350. X        for (;;) {
  351. X            if (isone(z3)) {
  352. X                if (ij)    /* number is definitely not prime */
  353. X                    goto notprime;
  354. X                break;
  355. X            }
  356. X            if (zcmp(z3, zm1) == 0)
  357. X                break;
  358. X            if (++ij >= ik)
  359. X                goto notprime;    /* number is definitely not prime */
  360. X            zsquare(z3, &z2);
  361. X            freeh(z3.v);
  362. X            zmod(z2, z, &z3);
  363. X            freeh(z2.v);
  364. X        }
  365. X        freeh(z3.v);
  366. X    }
  367. X    freeh(zm1.v);
  368. X    freeh(z1.v);
  369. X    return TRUE;    /* number might be prime */
  370. X
  371. Xnotprime:
  372. X    freeh(z3.v);
  373. X    freeh(zm1.v);
  374. X    freeh(z1.v);
  375. X    return FALSE;
  376. X}
  377. X
  378. X
  379. X/*
  380. X * Compute the Jacobi function (p / q) for odd q.
  381. X * If q is prime then the result is:
  382. X *    1 if p == x^2 (mod q) for some x.
  383. X *    -1 otherwise.
  384. X * If q is not prime, then the result is not meaningful if it is 1.
  385. X * This function returns 0 if q is even or q < 0.
  386. X */
  387. XFLAG
  388. Xzjacobi(z1, z2)
  389. X    ZVALUE z1, z2;
  390. X{
  391. X    ZVALUE p, q, tmp;
  392. X    long lowbit;
  393. X    int val;
  394. X
  395. X    if (iseven(z2) || isneg(z2))
  396. X        return 0;
  397. X    val = 1;
  398. X    if (iszero(z1) || isone(z1))
  399. X        return val;
  400. X    if (isunit(z1)) {
  401. X        if ((*z2.v - 1) & 0x2)
  402. X            val = -val;
  403. X        return val;
  404. X    }
  405. X    zcopy(z1, &p);
  406. X    zcopy(z2, &q);
  407. X    for (;;) {
  408. X        zmod(p, q, &tmp);
  409. X        freeh(p.v);
  410. X        p = tmp;
  411. X        if (iszero(p)) {
  412. X            freeh(p.v);
  413. X            p = _one_;
  414. X        }
  415. X        if (iseven(p)) {
  416. X            lowbit = zlowbit(p);
  417. X            zshift(p, -lowbit, &tmp);
  418. X            freeh(p.v);
  419. X            p = tmp;
  420. X            if ((lowbit & 1) && (((*q.v & 0x7) == 3) || ((*q.v & 0x7) == 5)))
  421. X                val = -val;
  422. X        }
  423. X        if (isunit(p)) {
  424. X            freeh(p.v);
  425. X            freeh(q.v);
  426. X            return val;
  427. X        }
  428. X        if ((*p.v & *q.v & 0x3) == 3)
  429. X            val = -val;
  430. X        tmp = q;
  431. X        q = p;
  432. X        p = tmp;
  433. X    }
  434. X}
  435. X
  436. X
  437. X/*
  438. X * Return the Fibonacci number F(n).
  439. X * This is evaluated by recursively using the formulas:
  440. X *    F(2N+1) = F(N+1)^2 + F(N)^2
  441. X * and
  442. X *    F(2N) = F(N+1)^2 - F(N-1)^2
  443. X */
  444. Xvoid
  445. Xzfib(z, res)
  446. X    ZVALUE z, *res;
  447. X{
  448. X    unsigned long i;
  449. X    long n;
  450. X    int sign;
  451. X    ZVALUE fnm1, fn, fnp1;        /* consecutive fibonacci values */
  452. X    ZVALUE t1, t2, t3;
  453. X
  454. X    if (isbig(z))
  455. X        error("Very large Fibonacci number");
  456. X    n = (istiny(z) ? z1tol(z) : z2tol(z));
  457. X    if (n == 0) {
  458. X        *res = _zero_;
  459. X        return;
  460. X    }
  461. X    sign = z.sign && ((n & 0x1) == 0);
  462. X    if (n <= 2) {
  463. X        *res = _one_;
  464. X        res->sign = (BOOL)sign;
  465. X        return;
  466. X    }
  467. X    i = TOPFULL;
  468. X    while ((i & n) == 0)
  469. X        i >>= 1L;
  470. X    i >>= 1L;
  471. X    fnm1 = _zero_;
  472. X    fn = _one_;
  473. X    fnp1 = _one_;
  474. X    while (i) {
  475. X        zsquare(fnm1, &t1);
  476. X        zsquare(fn, &t2);
  477. X        zsquare(fnp1, &t3);
  478. X        freeh(fnm1.v);
  479. X        freeh(fn.v);
  480. X        freeh(fnp1.v);
  481. X        zadd(t2, t3, &fnp1);
  482. X        zsub(t3, t1, &fn);
  483. X        freeh(t1.v);
  484. X        freeh(t2.v);
  485. X        freeh(t3.v);
  486. X        if (i & n) {
  487. X            fnm1 = fn;
  488. X            fn = fnp1;
  489. X            zadd(fnm1, fn, &fnp1);
  490. X        } else
  491. X            zsub(fnp1, fn, &fnm1);
  492. X        i >>= 1L;
  493. X    }
  494. X    freeh(fnm1.v);
  495. X    freeh(fnp1.v);
  496. X    *res = fn;
  497. X    res->sign = (BOOL)sign;
  498. X}
  499. X
  500. X
  501. X/*
  502. X * Compute the result of raising one number to the power of another
  503. X * The second number is assumed to be non-negative.
  504. X * It cannot be too large except for trivial cases.
  505. X */
  506. Xvoid
  507. Xzpowi(z1, z2, res)
  508. X    ZVALUE z1, z2, *res;
  509. X{
  510. X    int sign;        /* final sign of number */
  511. X    unsigned long power;    /* power to raise to */
  512. X    unsigned long bit;    /* current bit value */
  513. X    long twos;        /* count of times 2 is in result */
  514. X    ZVALUE ans, temp;
  515. X
  516. X    sign = (z1.sign && isodd(z2));
  517. X    z1.sign = 0;
  518. X    z2.sign = 0;
  519. X    if (iszero(z2)) {    /* number raised to power 0 */
  520. X        if (iszero(z1))
  521. X            error("Zero raised to zero power");
  522. X        *res = _one_;
  523. X        return;
  524. X    }
  525. X    if (isleone(z1)) {    /* 0, 1, or -1 raised to a power */
  526. X        ans = _one_;
  527. X        ans.sign = (BOOL)sign;
  528. X        if (*z1.v == 0)
  529. X            ans = _zero_;
  530. X        *res = ans;
  531. X        return;
  532. X    }
  533. X    if (isbig(z2))
  534. X        error("Raising to very large power");
  535. X    power = (istiny(z2) ? z1tol(z2) : z2tol(z2));
  536. X    if (istwo(z1)) {    /* two raised to a power */
  537. X        zbitvalue((long) power, res);
  538. X        return;
  539. X    }
  540. X    /*
  541. X     * See if this is a power of ten
  542. X     */
  543. X    if (istiny(z1) && (*z1.v == 10)) {
  544. X        ztenpow((long) power, res);
  545. X        res->sign = (BOOL)sign;
  546. X        return;
  547. X    }
  548. X    /*
  549. X     * Handle low powers specially
  550. X     */
  551. X    if (power <= 4) {
  552. X        switch ((int) power) {
  553. X            case 1:
  554. X                ans.len = z1.len;
  555. X                ans.v = alloc(ans.len);
  556. X                copyval(z1, ans);
  557. X                ans.sign = (BOOL)sign;
  558. X                *res = ans;
  559. X                return;
  560. X            case 2:
  561. X                zsquare(z1, res);
  562. X                return;
  563. X            case 3:
  564. X                zsquare(z1, &temp);
  565. X                zmul(z1, temp, res);
  566. X                freeh(temp.v);
  567. X                res->sign = (BOOL)sign;
  568. X                return;
  569. X            case 4:
  570. X                zsquare(z1, &temp);
  571. X                zsquare(temp, res);
  572. X                freeh(temp.v);
  573. X                return;
  574. X        }
  575. X    }
  576. X    /*
  577. X     * Shift out all powers of twos so the multiplies are smaller.
  578. X     * We will shift back the right amount when done.
  579. X     */
  580. X    twos = 0;
  581. X    if (iseven(z1)) {
  582. X        twos = zlowbit(z1);
  583. X        ans.v = alloc(z1.len);
  584. X        ans.len = z1.len;
  585. X        copyval(z1, ans);
  586. X        shiftr(ans, twos);
  587. X        trim(&ans);
  588. X        z1 = ans;
  589. X        twos *= power;
  590. X    }
  591. X    /*
  592. X     * Compute the power by squaring and multiplying.
  593. X     * This uses the left to right method of power raising.
  594. X     */
  595. X    bit = TOPFULL;
  596. X    while ((bit & power) == 0)
  597. X        bit >>= 1L;
  598. X    bit >>= 1L;
  599. X    zsquare(z1, &ans);
  600. X    if (bit & power) {
  601. X        zmul(ans, z1, &temp);
  602. X        freeh(ans.v);
  603. X        ans = temp;
  604. X    }
  605. X    bit >>= 1L;
  606. X    while (bit) {
  607. X        zsquare(ans, &temp);
  608. X        freeh(ans.v);
  609. X        ans = temp;
  610. X        if (bit & power) {
  611. X            zmul(ans, z1, &temp);
  612. X            freeh(ans.v);
  613. X            ans = temp;
  614. X        }
  615. X        bit >>= 1L;
  616. X    }
  617. X    /*
  618. X     * Scale back up by proper power of two
  619. X     */
  620. X    if (twos) {
  621. X        zshift(ans, twos, &temp);
  622. X        freeh(ans.v);
  623. X        ans = temp;
  624. X        freeh(z1.v);
  625. X    }
  626. X    ans.sign = (BOOL)sign;
  627. X    *res = ans;
  628. X}
  629. X
  630. X
  631. X/*
  632. X * Compute ten to the specified power
  633. X * This saves some work since the squares of ten are saved.
  634. X */
  635. Xvoid
  636. Xztenpow(power, res)
  637. X    long power;
  638. X    ZVALUE *res;
  639. X{
  640. X    long i;
  641. X    ZVALUE ans;
  642. X    ZVALUE temp;
  643. X
  644. X    if (power <= 0) {
  645. X        *res = _one_;
  646. X        return;
  647. X    }
  648. X    ans = _one_;
  649. X    _tenpowers_[0] = _ten_;
  650. X    for (i = 0; power; i++) {
  651. X        if (_tenpowers_[i].len == 0)
  652. X            zsquare(_tenpowers_[i-1], &_tenpowers_[i]);
  653. X        if (power & 0x1) {
  654. X            zmul(ans, _tenpowers_[i], &temp);
  655. X            freeh(ans.v);
  656. X            ans = temp;
  657. X        }
  658. X        power /= 2;
  659. X    }
  660. X    *res = ans;
  661. X}
  662. X
  663. X
  664. X/*
  665. X * Calculate modular inverse suppressing unnecessary divisions.
  666. X * This is based on the Euclidian algorithm for large numbers.
  667. X * (Algorithm X from Knuth Vol 2, section 4.5.2. and exercise 17)
  668. X * Returns TRUE if there is no solution because the numbers
  669. X * are not relatively prime.
  670. X */
  671. XBOOL
  672. Xzmodinv(u, v, res)
  673. X    ZVALUE u, v;
  674. X    ZVALUE *res;
  675. X{
  676. X    FULL    q1, q2, ui3, vi3, uh, vh, A, B, C, D, T;
  677. X    ZVALUE    u2, u3, v2, v3, qz, tmp1, tmp2, tmp3;
  678. X
  679. X    if (isneg(u) || isneg(v) || (zrel(u, v) >= 0))
  680. X        zmod(u, v, &v3);
  681. X    else
  682. X        zcopy(u, &v3);
  683. X    zcopy(v, &u3);
  684. X    u2 = _zero_;
  685. X    v2 = _one_;
  686. X
  687. X    /*
  688. X     * Loop here while the size of the numbers remain above
  689. X     * the size of a FULL.  Throughout this loop u3 >= v3.
  690. X     */
  691. X    while ((u3.len > 1) && !iszero(v3)) {
  692. X        uh = (((FULL) u3.v[u3.len - 1]) << BASEB) + u3.v[u3.len - 2];
  693. X        vh = 0;
  694. X        if ((v3.len + 1) >= u3.len)
  695. X            vh = v3.v[v3.len - 1];
  696. X        if (v3.len == u3.len)
  697. X            vh = (vh << BASEB) + v3.v[v3.len - 2];
  698. X        A = 1;
  699. X        B = 0;
  700. X        C = 0;
  701. X        D = 1;
  702. X
  703. X        /*
  704. X         * Calculate successive quotients of the continued fraction
  705. X         * expansion using only single precision arithmetic until
  706. X         * greater precision is required.
  707. X         */
  708. X        while ((vh + C) && (vh + D)) {
  709. X            q1 = (uh + A) / (vh + C);
  710. X            q2 = (uh + B) / (vh + D);
  711. X            if (q1 != q2)
  712. X                break;
  713. X            T = A - q1 * C;
  714. X            A = C;
  715. X            C = T;
  716. X            T = B - q1 * D;
  717. X            B = D;
  718. X            D = T;
  719. X            T = uh - q1 * vh;
  720. X            uh = vh;
  721. X            vh = T;
  722. X        }
  723. X    
  724. X        /*
  725. X         * If B is zero, then we made no progress because
  726. X         * the calculation requires a very large quotient.
  727. X         * So we must do this step of the calculation in
  728. X         * full precision
  729. X         */
  730. X        if (B == 0) {
  731. X            zquo(u3, v3, &qz);
  732. X            zmul(qz, v2, &tmp1);
  733. X            zsub(u2, tmp1, &tmp2);
  734. X            freeh(tmp1.v);
  735. X            freeh(u2.v);
  736. X            u2 = v2;
  737. X            v2 = tmp2;
  738. X            zmul(qz, v3, &tmp1);
  739. X            zsub(u3, tmp1, &tmp2);
  740. X            freeh(tmp1.v);
  741. X            freeh(u3.v);
  742. X            u3 = v3;
  743. X            v3 = tmp2;
  744. X            freeh(qz.v);
  745. X            continue;
  746. X        }
  747. X        /*
  748. X         * Apply the calculated A,B,C,D numbers to the current
  749. X         * values to update them as if the full precision
  750. X         * calculations had been carried out.
  751. X         */
  752. X        zmuli(u2, (long) A, &tmp1);
  753. X        zmuli(v2, (long) B, &tmp2);
  754. X        zadd(tmp1, tmp2, &tmp3);
  755. X        freeh(tmp1.v);
  756. X        freeh(tmp2.v);
  757. X        zmuli(u2, (long) C, &tmp1);
  758. X        zmuli(v2, (long) D, &tmp2);
  759. X        freeh(u2.v);
  760. X        freeh(v2.v);
  761. X        u2 = tmp3;
  762. X        zadd(tmp1, tmp2, &v2);
  763. X        freeh(tmp1.v);
  764. X        freeh(tmp2.v);
  765. X        zmuli(u3, (long) A, &tmp1);
  766. X        zmuli(v3, (long) B, &tmp2);
  767. X        zadd(tmp1, tmp2, &tmp3);
  768. X        freeh(tmp1.v);
  769. X        freeh(tmp2.v);
  770. X        zmuli(u3, (long) C, &tmp1);
  771. X        zmuli(v3, (long) D, &tmp2);
  772. X        freeh(u3.v);
  773. X        freeh(v3.v);
  774. X        u3 = tmp3;
  775. X        zadd(tmp1, tmp2, &v3);
  776. X        freeh(tmp1.v);
  777. X        freeh(tmp2.v);
  778. X    }
  779. X
  780. X    /*
  781. X     * Here when the remaining numbers become single precision in size.
  782. X     * Finish the procedure using single precision calculations.
  783. X     */
  784. X    if (iszero(v3) && !isone(u3)) {
  785. X        freeh(u3.v);
  786. X        freeh(v3.v);
  787. X        freeh(u2.v);
  788. X        freeh(v2.v);
  789. X        return TRUE;
  790. X    }
  791. X    ui3 = (istiny(u3) ? z1tol(u3) : z2tol(u3));
  792. X    vi3 = (istiny(v3) ? z1tol(v3) : z2tol(v3));
  793. X    freeh(u3.v);
  794. X    freeh(v3.v);
  795. X    while (vi3) {
  796. X        q1 = ui3 / vi3;
  797. X        zmuli(v2, (long) q1, &tmp1);
  798. X        zsub(u2, tmp1, &tmp2);
  799. X        freeh(tmp1.v);
  800. X        freeh(u2.v);
  801. X        u2 = v2;
  802. X        v2 = tmp2;
  803. X        q2 = ui3 - q1 * vi3;
  804. X        ui3 = vi3;
  805. X        vi3 = q2;
  806. X    }
  807. X    freeh(v2.v);
  808. X    if (ui3 != 1) {
  809. X        freeh(u2.v);
  810. X        return TRUE;
  811. X    }
  812. X    if (isneg(u2)) {
  813. X        zadd(v, u2, res);
  814. X        freeh(u2.v);
  815. X        return FALSE;
  816. X    }
  817. X    *res = u2;
  818. X    return FALSE;
  819. X}
  820. X
  821. X
  822. X#if 0
  823. X/*
  824. X * Approximate the quotient of two integers by another set of smaller
  825. X * integers.  This uses continued fractions to determine the smaller set.
  826. X */
  827. Xvoid
  828. Xzapprox(z1, z2, res1, res2)
  829. X    ZVALUE z1, z2, *res1, *res2;
  830. X{
  831. X    int sign;
  832. X    ZVALUE u1, v1, u3, v3, q, t1, t2, t3;
  833. X
  834. X    sign = ((z1.sign != 0) ^ (z2.sign != 0));
  835. X    z1.sign = 0;
  836. X    z2.sign = 0;
  837. X    v3 = z2;
  838. X    u3 = z1;
  839. X    u1 = _one_;
  840. X    v1 = _zero_;
  841. X    while (!iszero(v3)) {
  842. X        zdiv(u3, v3, &q, &t1);
  843. X        zmul(v1, q, &t2);
  844. X        zsub(u1, t2, &t3);
  845. X        freeh(q.v);
  846. X        freeh(t2.v);
  847. X        freeh(u1.v);
  848. X        if ((u3.v != z1.v) && (u3.v != z2.v))
  849. X            freeh(u3.v);
  850. X        u1 = v1;
  851. X        u3 = v3;
  852. X        v1 = t3;
  853. X        v3 = t1;
  854. X    }
  855. X    if (!isunit(u3))
  856. X        error("Non-relativly prime numbers for approx");
  857. X    if ((u3.v != z1.v) && (u3.v != z2.v))
  858. X        freeh(u3.v);
  859. X    if ((v3.v != z1.v) && (v3.v != z2.v))
  860. X        freeh(v3.v);
  861. X    freeh(v1.v);
  862. X    zmul(u1, z1, &t1);
  863. X    zsub(t1, _one_, &t2);
  864. X    freeh(t1.v);
  865. X    zquo(t2, z2, &t1);
  866. X    freeh(t2.v);
  867. X    u1.sign = (BOOL)sign;
  868. X    t1.sign = 0;
  869. X    *res1 = t1;
  870. X    *res2 = u1;
  871. X}
  872. X#endif
  873. X
  874. X
  875. X/*
  876. X * Binary gcd algorithm
  877. X * This algorithm taken from Knuth
  878. X */
  879. Xvoid
  880. Xzgcd(z1, z2, res)
  881. X    ZVALUE z1, z2, *res;
  882. X{
  883. X    ZVALUE u, v, t;
  884. X    register long j, k, olen, mask;
  885. X    register HALF h;
  886. X    HALF *oldv1, *oldv2;
  887. X
  888. X    /*
  889. X     * First see if one number is very much larger than the other.
  890. X     * If so, then divide as necessary to get the numbers near each other.
  891. X     */
  892. X    z1.sign = 0;
  893. X    z2.sign = 0;
  894. X    oldv1 = z1.v;
  895. X    oldv2 = z2.v;
  896. X    if (z1.len < z2.len) {
  897. X        t = z1;
  898. X        z1 = z2;
  899. X        z2 = t;
  900. X    }
  901. X    while ((z1.len > (z2.len + 5)) && !iszero(z2)) {
  902. X        zmod(z1, z2, &t);
  903. X        if ((z1.v != oldv1) && (z1.v != oldv2))
  904. X            freeh(z1.v);
  905. X        z1 = z2;
  906. X        z2 = t;
  907. X    }
  908. X    /*
  909. X     * Ok, now do the binary method proper
  910. X     */
  911. X    u.len = z1.len;
  912. X    v.len = z2.len;
  913. X    u.sign = 0;
  914. X    v.sign = 0;
  915. X    if (!ztest(z1)) {
  916. X        v.v = alloc(v.len);
  917. X        copyval(z2, v);
  918. X        *res = v;
  919. X        goto done;
  920. X    }
  921. X    if (!ztest(z2)) {
  922. X        u.v = alloc(u.len);
  923. X        copyval(z1, u);
  924. X        *res = u;
  925. X        goto done;
  926. X    }
  927. X    u.v = alloc(u.len);
  928. X    v.v = alloc(v.len);
  929. X    copyval(z1, u);
  930. X    copyval(z2, v);
  931. X    k = 0;
  932. X    while (u.v[k] == 0 && v.v[k] == 0)
  933. X        ++k;
  934. X    mask = 01;
  935. X    h = u.v[k] | v.v[k];
  936. X    k *= BASEB;
  937. X    while (!(h & mask)) {
  938. X        mask <<= 1;
  939. X        k++;
  940. X    }
  941. X    shiftr(u, k);
  942. X    shiftr(v, k);
  943. X    trim(&u);
  944. X    trim(&v);
  945. X    if (isodd(u)) {
  946. X        t.v = alloc(v.len);
  947. X        t.len = v.len;
  948. X        copyval(v, t);
  949. X        t.sign = !v.sign;
  950. X    } else {
  951. X        t.v = alloc(u.len);
  952. X        t.len = u.len;
  953. X        copyval(u, t);
  954. X        t.sign = u.sign;
  955. X    }
  956. X    while (ztest(t)) {
  957. X        j = 0;
  958. X        while (!t.v[j])
  959. X            ++j;
  960. X        mask = 01;
  961. X        h = t.v[j];
  962. X        j *= BASEB;
  963. X        while (!(h & mask)) {
  964. X            mask <<= 1;
  965. X            j++;
  966. X        }
  967. X        shiftr(t, j);
  968. X        trim(&t);
  969. X        if (ztest(t) > 0) {
  970. X            freeh(u.v);
  971. X            u = t;
  972. X        } else {
  973. X            freeh(v.v);
  974. X            v = t;
  975. X            v.sign = !t.sign;
  976. X        }
  977. X        zsub(u, v, &t);
  978. X    }
  979. X    freeh(t.v);
  980. X    freeh(v.v);
  981. X    if (k) {
  982. X        olen = u.len;
  983. X        u.len += k / BASEB + 1;
  984. X        u.v = (HALF *) realloc(u.v, u.len * sizeof(HALF));
  985. X        while (olen != u.len)
  986. X            u.v[olen++] = 0;
  987. X        shiftl(u, k);
  988. X    }
  989. X    trim(&u);
  990. X    *res = u;
  991. X
  992. Xdone:
  993. X    if ((z1.v != oldv1) && (z1.v != oldv2))
  994. X        freeh(z1.v);
  995. X    if ((z2.v != oldv1) && (z2.v != oldv2))
  996. X        freeh(z2.v);
  997. X}
  998. X
  999. X
  1000. X/*
  1001. X * Compute the lcm of two integers (least common multiple).
  1002. X * This is done using the formula:  gcd(a,b) * lcm(a,b) = a * b.
  1003. X */
  1004. Xvoid
  1005. Xzlcm(z1, z2, res)
  1006. X    ZVALUE z1, z2, *res;
  1007. X{
  1008. X    ZVALUE temp1, temp2;
  1009. X
  1010. X    zgcd(z1, z2, &temp1);
  1011. X    zquo(z1, temp1, &temp2);
  1012. X    freeh(temp1.v);
  1013. X    zmul(temp2, z2, res);
  1014. X    freeh(temp2.v);
  1015. X}
  1016. X
  1017. X
  1018. X/*
  1019. X * Return whether or not two numbers are relatively prime to each other.
  1020. X */
  1021. XBOOL
  1022. Xzrelprime(z1, z2)
  1023. X    ZVALUE z1, z2;            /* numbers to be tested */
  1024. X{
  1025. X    FULL rem1, rem2;        /* remainders */
  1026. X    ZVALUE rem;
  1027. X    BOOL result;
  1028. X
  1029. X    z1.sign = 0;
  1030. X    z2.sign = 0;
  1031. X    if (iseven(z1) && iseven(z2))    /* false if both even */
  1032. X        return FALSE;
  1033. X    if (isunit(z1) || isunit(z2))    /* true if either is a unit */
  1034. X        return TRUE;
  1035. X    if (iszero(z1) || iszero(z2))    /* false if either is zero */
  1036. X        return FALSE;
  1037. X    if (istwo(z1) || istwo(z2))    /* true if either is two */
  1038. X        return TRUE;
  1039. X    /*
  1040. X     * Try reducing each number by the product of the first few odd primes
  1041. X     * to see if any of them are a common factor.
  1042. X     */
  1043. X    rem1 = zmodi(z1, 3L * 5 * 7 * 11 * 13);
  1044. X    rem2 = zmodi(z2, 3L * 5 * 7 * 11 * 13);
  1045. X    if (((rem1 % 3) == 0) && ((rem2 % 3) == 0))
  1046. X        return FALSE;
  1047. X    if (((rem1 % 5) == 0) && ((rem2 % 5) == 0))
  1048. X        return FALSE;
  1049. X    if (((rem1 % 7) == 0) && ((rem2 % 7) == 0))
  1050. X        return FALSE;
  1051. X    if (((rem1 % 11) == 0) && ((rem2 % 11) == 0))
  1052. X        return FALSE;
  1053. X    if (((rem1 % 13) == 0) && ((rem2 % 13) == 0))
  1054. X        return FALSE;
  1055. X    /*
  1056. X     * Try a new batch of primes now
  1057. X     */
  1058. X    rem1 = zmodi(z1, 17L * 19 * 23);
  1059. X    rem2 = zmodi(z2, 17L * 19 * 23);
  1060. X    if (((rem1 % 17) == 0) && ((rem2 % 17) == 0))
  1061. X        return FALSE;
  1062. X    if (((rem1 % 19) == 0) && ((rem2 % 19) == 0))
  1063. X        return FALSE;
  1064. X    if (((rem1 % 23) == 0) && ((rem2 % 23) == 0))
  1065. X        return FALSE;
  1066. X    /*
  1067. X     * Yuk, we must actually compute the gcd to know the answer
  1068. X     */
  1069. X    zgcd(z1, z2, &rem);
  1070. X    result = isunit(rem);
  1071. X    freeh(rem.v);
  1072. X    return result;
  1073. X}
  1074. X
  1075. X
  1076. X/*
  1077. X * Compute the log of one number base another, to the closest integer.
  1078. X * This is the largest integer which when the second number is raised to it,
  1079. X * the resulting value is less than or equal to the first number.
  1080. X * Example:  zlog(123456, 10) = 5.
  1081. X */
  1082. Xlong
  1083. Xzlog(z1, z2)
  1084. X    ZVALUE z1, z2;
  1085. X{
  1086. X    register ZVALUE *zp;        /* current square */
  1087. X    long power;            /* current power */
  1088. X    long worth;            /* worth of current square */
  1089. X    ZVALUE val;            /* current value of power */
  1090. X    ZVALUE temp;            /* temporary */
  1091. X    ZVALUE squares[32];        /* table of squares of base */
  1092. X
  1093. X    /*
  1094. X     * Make sure that the numbers are nonzero and the base is greater than one.
  1095. X     */
  1096. X    if (isneg(z1) || iszero(z1) || isneg(z2) || isleone(z2))
  1097. X        error("Bad arguments for log");
  1098. X    /*
  1099. X     * Reject trivial cases.
  1100. X     */
  1101. X    if (z1.len < z2.len)
  1102. X        return 0;
  1103. X    if ((z1.len == z2.len) && (z1.v[z1.len-1] < z2.v[z2.len-1]))
  1104. X        return 0;
  1105. X    power = zrel(z1, z2);
  1106. X    if (power <= 0)
  1107. X        return (power + 1);
  1108. X    /*
  1109. X     * Handle any power of two special.
  1110. X     */
  1111. X    if (zisonebit(z2))
  1112. X        return (zhighbit(z1) / zlowbit(z2));
  1113. X    /*
  1114. X     * Handle base 10 special
  1115. X     */
  1116. X    if ((z2.len == 1) && (*z2.v == 10))
  1117. X        return zlog10(z1);
  1118. X    /*
  1119. X     * Now loop by squaring the base each time, and see whether or
  1120. X     * not each successive square is still smaller than the number.
  1121. X     */
  1122. X    worth = 1;
  1123. X    zp = &squares[0];
  1124. X    *zp = z2;
  1125. X    while (((zp->len * 2) - 1) <= z1.len) {    /* while square not too large */
  1126. X        zsquare(*zp, zp + 1);
  1127. X        zp++;
  1128. X        worth *= 2;
  1129. X    }
  1130. X    /*
  1131. X     * Now back down the squares, and multiply them together to see
  1132. X     * exactly how many times the base can be raised by.
  1133. X     */
  1134. X    val = _one_;
  1135. X    power = 0;
  1136. X    for (; zp >= squares; zp--, worth /= 2) {
  1137. X        if ((val.len + zp->len - 1) <= z1.len) {
  1138. X            zmul(val, *zp, &temp);
  1139. X            if (zrel(z1, temp) >= 0) {
  1140. X                freeh(val.v);
  1141. X                val = temp;
  1142. X                power += worth;
  1143. X            } else
  1144. X                freeh(temp.v);
  1145. X        }
  1146. X        if (zp != squares)
  1147. X            freeh(zp->v);
  1148. X    }
  1149. X    return power;
  1150. X}
  1151. X
  1152. X
  1153. X/*
  1154. X * Return the integral log base 10 of a number.
  1155. X */
  1156. Xlong
  1157. Xzlog10(z)
  1158. X    ZVALUE z;
  1159. X{
  1160. X    register ZVALUE *zp;        /* current square */
  1161. X    long power;            /* current power */
  1162. X    long worth;            /* worth of current square */
  1163. X    ZVALUE val;            /* current value of power */
  1164. X    ZVALUE temp;            /* temporary */
  1165. X
  1166. X    if (!ispos(z))
  1167. X        error("Non-positive number for log10");
  1168. X    /*
  1169. X     * Loop by squaring the base each time, and see whether or
  1170. X     * not each successive square is still smaller than the number.
  1171. X     */
  1172. X    worth = 1;
  1173. X    zp = &_tenpowers_[0];
  1174. X    *zp = _ten_;
  1175. X    while (((zp->len * 2) - 1) <= z.len) {    /* while square not too large */
  1176. X        if (zp[1].len == 0)
  1177. X            zsquare(*zp, zp + 1);
  1178. X        zp++;
  1179. X        worth *= 2;
  1180. X    }
  1181. X    /*
  1182. X     * Now back down the squares, and multiply them together to see
  1183. X     * exactly how many times the base can be raised by.
  1184. X     */
  1185. X    val = _one_;
  1186. X    power = 0;
  1187. X    for (; zp >= _tenpowers_; zp--, worth /= 2) {
  1188. X        if ((val.len + zp->len - 1) <= z.len) {
  1189. X            zmul(val, *zp, &temp);
  1190. X            if (zrel(z, temp) >= 0) {
  1191. X                freeh(val.v);
  1192. X                val = temp;
  1193. X                power += worth;
  1194. X            } else
  1195. X                freeh(temp.v);
  1196. X        }
  1197. X    }
  1198. X    return power;
  1199. X}
  1200. X
  1201. X
  1202. X/*
  1203. X * Return the number of times that one number will divide another.
  1204. X * This works similarly to zlog, except that divisions must be exact.
  1205. X * For example, zdivcount(540, 3) = 3, since 3^3 divides 540 but 3^4 won't.
  1206. X */
  1207. Xlong
  1208. Xzdivcount(z1, z2)
  1209. X    ZVALUE z1, z2;
  1210. X{
  1211. X    long count;        /* number of factors removed */
  1212. X    ZVALUE tmp;        /* ignored return value */
  1213. X
  1214. X    count = zfacrem(z1, z2, &tmp);
  1215. X    freeh(tmp.v);
  1216. X    return count;
  1217. X}
  1218. X
  1219. X
  1220. X/*
  1221. X * Remove all occurances of the specified factor from a number.
  1222. X * Also returns the number of factors removed as a function return value.
  1223. X * Example:  zfacrem(540, 3, &x) returns 3 and sets x to 20.
  1224. X */
  1225. Xlong
  1226. Xzfacrem(z1, z2, rem)
  1227. X    ZVALUE z1, z2, *rem;
  1228. X{
  1229. X    register ZVALUE *zp;        /* current square */
  1230. X    long count;            /* total count of divisions */
  1231. X    long worth;            /* worth of current square */
  1232. X    ZVALUE temp1, temp2, temp3;    /* temporaries */
  1233. X    ZVALUE squares[32];        /* table of squares of factor */
  1234. X
  1235. X    z1.sign = 0;
  1236. X    z2.sign = 0;
  1237. X    /*
  1238. X     * Make sure factor isn't 0 or 1.
  1239. X     */
  1240. X    if (isleone(z2))
  1241. X        error("Bad argument for facrem");
  1242. X    /*
  1243. X     * Reject trivial cases.
  1244. X     */
  1245. X    if ((z1.len < z2.len) || (isodd(z1) && iseven(z2)) ||
  1246. X        ((z1.len == z2.len) && (z1.v[z1.len-1] < z2.v[z2.len-1]))) {
  1247. X        rem->v = alloc(z1.len);
  1248. X        rem->len = z1.len;
  1249. X        rem->sign = 0;
  1250. X        copyval(z1, *rem);
  1251. X        return 0;
  1252. X    }
  1253. X    /*
  1254. X     * Handle any power of two special.
  1255. X     */
  1256. X    if (zisonebit(z2)) {
  1257. X        count = zlowbit(z1) / zlowbit(z2);
  1258. X        rem->v = alloc(z1.len);
  1259. X        rem->len = z1.len;
  1260. X        rem->sign = 0;
  1261. X        copyval(z1, *rem);
  1262. X        shiftr(*rem, count);
  1263. X        trim(rem);
  1264. X        return count;
  1265. X    }
  1266. X    /*
  1267. X     * See if the factor goes in even once.
  1268. X     */
  1269. X    zdiv(z1, z2, &temp1, &temp2);
  1270. X    if (!iszero(temp2)) {
  1271. X        freeh(temp1.v);
  1272. X        freeh(temp2.v);
  1273. X        rem->v = alloc(z1.len);
  1274. X        rem->len = z1.len;
  1275. X        rem->sign = 0;
  1276. X        copyval(z1, *rem);
  1277. X        return 0;
  1278. X    }
  1279. X    freeh(temp2.v);
  1280. X    z1 = temp1;
  1281. X    /*
  1282. X     * Now loop by squaring the factor each time, and see whether
  1283. X     * or not each successive square will still divide the number.
  1284. X     */
  1285. X    count = 1;
  1286. X    worth = 1;
  1287. X    zp = &squares[0];
  1288. X    *zp = z2;
  1289. X    while (((zp->len * 2) - 1) <= z1.len) {    /* while square not too large */
  1290. X        zsquare(*zp, &temp1);
  1291. X        zdiv(z1, temp1, &temp2, &temp3);
  1292. X        if (!iszero(temp3)) {
  1293. X            freeh(temp1.v);
  1294. X            freeh(temp2.v);
  1295. X            freeh(temp3.v);
  1296. X            break;
  1297. X        }
  1298. X        freeh(temp3.v);
  1299. X        freeh(z1.v);
  1300. X        z1 = temp2;
  1301. X        *++zp = temp1;
  1302. X        worth *= 2;
  1303. X        count += worth;
  1304. X    }
  1305. X    /*
  1306. X     * Now back down the list of squares, and see if the lower powers
  1307. X     * will divide any more times.
  1308. X     */
  1309. X    for (; zp >= squares; zp--, worth /= 2) {
  1310. X        if (zp->len <= z1.len) {
  1311. X            zdiv(z1, *zp, &temp1, &temp2);
  1312. X            if (iszero(temp2)) {
  1313. X                temp3 = z1;
  1314. X                z1 = temp1;
  1315. X                temp1 = temp3;
  1316. X                count += worth;
  1317. X            }
  1318. X            freeh(temp1.v);
  1319. X            freeh(temp2.v);
  1320. X        }
  1321. X        if (zp != squares)
  1322. X            freeh(zp->v);
  1323. X    }
  1324. X    *rem = z1;
  1325. X    return count;
  1326. X}
  1327. X
  1328. X
  1329. X/*
  1330. X * Keep dividing a number by the gcd of it with another number until the
  1331. X * result is relatively prime to the second number.
  1332. X */
  1333. Xvoid
  1334. Xzgcdrem(z1, z2, res)
  1335. X    ZVALUE z1, z2, *res;
  1336. X{
  1337. X    ZVALUE tmp1, tmp2;
  1338. X
  1339. X    /*
  1340. X     * Begin by taking the gcd for the first time.
  1341. X     * If the number is already relatively prime, then we are done.
  1342. X     */
  1343. X    zgcd(z1, z2, &tmp1);
  1344. X    if (isunit(tmp1) || iszero(tmp1)) {
  1345. X        res->len = z1.len;
  1346. X        res->v = alloc(z1.len);
  1347. X        res->sign = z1.sign;
  1348. X        copyval(z1, *res);
  1349. X        return;
  1350. X    }
  1351. X    zquo(z1, tmp1, &tmp2);
  1352. X    z1 = tmp2;
  1353. X    z2 = tmp1;
  1354. X    /*
  1355. X     * Now keep alternately taking the gcd and removing factors until
  1356. X     * the gcd becomes one.
  1357. X     */
  1358. X    while (!isunit(z2)) {
  1359. X        (void) zfacrem(z1, z2, &tmp1);
  1360. X        freeh(z1.v);
  1361. X        z1 = tmp1;
  1362. X        zgcd(z1, z2, &tmp1);
  1363. X        freeh(z2.v);
  1364. X        z2 = tmp1;
  1365. X    }
  1366. X    *res = z1;
  1367. X}
  1368. X
  1369. X
  1370. X/*
  1371. X * Find the lowest prime factor of a number if one can be found.
  1372. X * Search is conducted for the first count primes.
  1373. X * Returns 1 if no factor was found.
  1374. X */
  1375. Xlong
  1376. Xzlowfactor(z, count)
  1377. X    ZVALUE z;
  1378. X    long count;
  1379. X{
  1380. X    FULL p, d;
  1381. X    ZVALUE div, tmp;
  1382. X    HALF divval[2];
  1383. X
  1384. X    if ((--count < 0) || iszero(z))
  1385. X        return 1;
  1386. X    if (iseven(z))
  1387. X        return 2;
  1388. X    div.sign = 0;
  1389. X    div.v = divval;
  1390. X    for (p = 3; (count > 0); p += 2) {
  1391. X        for (d = 3; (d * d) <= p; d += 2)
  1392. X            if ((p % d) == 0)
  1393. X                goto next;
  1394. X        divval[0] = (p & BASE1);
  1395. X        divval[1] = (p / BASE);
  1396. X        div.len = 1 + (p >= BASE);
  1397. X        zmod(z, div, &tmp);
  1398. X        if (iszero(tmp))
  1399. X            return p;
  1400. X        freeh(tmp.v);
  1401. X        count--;
  1402. Xnext:;
  1403. X    }
  1404. X    return 1;
  1405. X}
  1406. X
  1407. X
  1408. X/*
  1409. X * Return the number of digits (base 10) in a number, ignoring the sign.
  1410. X */
  1411. Xlong
  1412. Xzdigits(z1)
  1413. X    ZVALUE z1;
  1414. X{
  1415. X    long count, val;
  1416. X
  1417. X    z1.sign = 0;
  1418. X    if (istiny(z1)) {    /* do small numbers ourself */
  1419. X        count = 1;
  1420. X        val = 10;
  1421. X        while (*z1.v >= (HALF)val) {
  1422. X            count++;
  1423. X            val *= 10;
  1424. X        }
  1425. X        return count;
  1426. X    }
  1427. X    return (zlog10(z1) + 1);
  1428. X}
  1429. X
  1430. X
  1431. X/*
  1432. X * Return the single digit at the specified decimal place of a number,
  1433. X * where 0 means the rightmost digit.  Example:  zdigit(1234, 1) = 3.
  1434. X */
  1435. XFLAG
  1436. Xzdigit(z1, n)
  1437. X    ZVALUE z1;
  1438. X    long n;
  1439. X{
  1440. X    ZVALUE tmp1, tmp2;
  1441. X    FLAG res;
  1442. X
  1443. X    z1.sign = 0;
  1444. X    if (iszero(z1) || (n < 0) || (n / BASEDIG >= z1.len))
  1445. X        return 0;
  1446. X    if (n == 0)
  1447. X        return zmodi(z1, 10L);
  1448. X    if (n == 1)
  1449. X        return zmodi(z1, 100L) / 10;
  1450. X    if (n == 2)
  1451. X        return zmodi(z1, 1000L) / 100;
  1452. X    if (n == 3)
  1453. X        return zmodi(z1, 10000L) / 1000;
  1454. X    ztenpow(n, &tmp1);
  1455. X    zquo(z1, tmp1, &tmp2);
  1456. X    res = zmodi(tmp2, 10L);
  1457. X    freeh(tmp1.v);
  1458. X    freeh(tmp2.v);
  1459. X    return res;
  1460. X}
  1461. X
  1462. X
  1463. X/*
  1464. X * Find the square root of a number.  This is the greatest integer whose
  1465. X * square is less than or equal to the number. Returns TRUE if the
  1466. X * square root is exact.
  1467. X */
  1468. XBOOL
  1469. Xzsqrt(z1, dest)
  1470. X    ZVALUE z1, *dest;
  1471. X{
  1472. X    ZVALUE try, quo, rem, old, temp;
  1473. X    FULL iquo, val;
  1474. X    long i,j;
  1475. X
  1476. X    if (z1.sign)
  1477. X        error("Square root of negative number");
  1478. X    if (iszero(z1)) {
  1479. X        *dest = _zero_;
  1480. X        return TRUE;
  1481. X    }
  1482. X    if ((*z1.v < 4) && istiny(z1)) {
  1483. X        *dest = _one_;
  1484. X        return (*z1.v == 1);
  1485. X    }
  1486. X    /*
  1487. X     * Pick the square root of the leading one or two digits as a first guess.
  1488. X     */
  1489. X    val = z1.v[z1.len-1];
  1490. X    if ((z1.len & 0x1) == 0)
  1491. X        val = (val * BASE) + z1.v[z1.len-2];
  1492. X
  1493. X    /*
  1494. X     * Find the largest power of 2 that when squared
  1495. X     * is <= val > 0.  Avoid multiply overflow by doing 
  1496. X     * a careful check at the BASE boundary.
  1497. X     */
  1498. X    j = 1<<(BASEB+BASEB-2);
  1499. X    if (val > j) {
  1500. X        iquo = BASE;
  1501. X    } else {
  1502. X        i = BASEB-1;
  1503. X        while (j > val) {
  1504. X            --i;
  1505. X            j >>= 2;
  1506. X        }
  1507. X        iquo = bitmask[i];
  1508. X    }
  1509. X
  1510. X    for (i = 8; i > 0; i--)
  1511. X        iquo = (iquo + (val / iquo)) / 2;
  1512. X    if (iquo > BASE1)
  1513. X        iquo = BASE1;
  1514. X    /*
  1515. X     * Allocate the numbers to use for the main loop.
  1516. X     * The size and high bits of the final result are correctly set here.
  1517. X     * Notice that the remainder of the test value is rubbish, but this
  1518. X     * is unimportant.
  1519. X     */
  1520. X    try.sign = 0;
  1521. X    try.len = (z1.len + 1) / 2;
  1522. X    try.v = alloc(try.len);
  1523. X    try.v[try.len-1] = (HALF)iquo;
  1524. X    old.sign = 0;
  1525. X    old.v = alloc(try.len);
  1526. X    old.len = 1;
  1527. X    *old.v = 0;
  1528. X    /*
  1529. X     * Main divide and average loop
  1530. X     */
  1531. X    for (;;) {
  1532. X        zdiv(z1, try, &quo, &rem);
  1533. X        i = zrel(try, quo);
  1534. X        if ((i == 0) && iszero(rem)) {    /* exact square root */
  1535. X            freeh(rem.v);
  1536. X            freeh(quo.v);
  1537. X            freeh(old.v);
  1538. X            *dest = try;
  1539. X            return TRUE;
  1540. X        }
  1541. X        freeh(rem.v);
  1542. X        if (i <= 0) {
  1543. X            /*
  1544. X            * Current try is less than or equal to the square root since
  1545. X            * it is less than the quotient.  If the quotient is equal to
  1546. X            * the try, we are all done.  Also, if the try is equal to the
  1547. X            * old try value, we are done since no improvement occurred.
  1548. X            * If not, save the improved value and loop some more.
  1549. X            */
  1550. X            if ((i == 0) || (zcmp(old, try) == 0)) {
  1551. X                freeh(quo.v);
  1552. X                freeh(old.v);
  1553. X                *dest = try;
  1554. X                return FALSE;
  1555. X            }
  1556. X            old.len = try.len;
  1557. X            copyval(try, old);
  1558. X        }
  1559. X        /* average current try and quotent for the new try */
  1560. X        zadd(try, quo, &temp);
  1561. X        freeh(quo.v);
  1562. X        freeh(try.v);
  1563. X        try = temp;
  1564. X        shiftr(try, 1L);
  1565. X        quicktrim(try);
  1566. X    }
  1567. X}
  1568. X
  1569. X
  1570. X/*
  1571. X * Take an arbitrary root of a number (to the greatest integer).
  1572. X * This uses the following iteration to get the Kth root of N:
  1573. X *    x = ((K-1) * x + N / x^(K-1)) / K
  1574. X */
  1575. Xvoid
  1576. Xzroot(z1, z2, dest)
  1577. X    ZVALUE z1, z2, *dest;
  1578. X{
  1579. X    ZVALUE try, quo, old, temp, temp2;
  1580. X    ZVALUE k1;            /* holds k - 1 */
  1581. X    int sign;
  1582. X    long i, k, highbit;
  1583. X    SIUNION sival;
  1584. X
  1585. X    sign = z1.sign;
  1586. X    if (sign && iseven(z2))
  1587. X        error("Even root of negative number");
  1588. X    if (iszero(z2) || isneg(z2))
  1589. X        error("Non-positive root");
  1590. X    if (iszero(z1)) {    /* root of zero */
  1591. X        *dest = _zero_;
  1592. X        return;
  1593. X    }
  1594. X    if (isunit(z2)) {    /* first root */
  1595. X        zcopy(z1, dest);
  1596. X        return;
  1597. X    }
  1598. X    if (isbig(z2)) {    /* humongous root */
  1599. X        *dest = _one_;
  1600. X        dest->sign = (HALF)sign;
  1601. X        return;
  1602. X    }
  1603. X    k = (istiny(z2) ? z1tol(z2) : z2tol(z2));
  1604. X    highbit = zhighbit(z1);
  1605. X    if (highbit < k) {    /* too high a root */
  1606. X        *dest = _one_;
  1607. X        dest->sign = (HALF)sign;
  1608. X        return;
  1609. X    }
  1610. X    sival.ivalue = k - 1;
  1611. X    k1.v = &sival.silow;
  1612. X    k1.len = 1 + (sival.sihigh != 0);
  1613. X    k1.sign = 0;
  1614. X    z1.sign = 0;
  1615. X    /*
  1616. X     * Allocate the numbers to use for the main loop.
  1617. X     * The size and high bits of the final result are correctly set here.
  1618. X     * Notice that the remainder of the test value is rubbish, but this
  1619. X     * is unimportant.
  1620. X     */
  1621. X    highbit = (highbit + k - 1) / k;
  1622. X    try.len = (highbit / BASEB) + 1;
  1623. X    try.v = alloc(try.len);
  1624. X    try.v[try.len-1] = ((HALF)1 << (highbit % BASEB));
  1625. X    try.sign = 0;
  1626. X    old.v = alloc(try.len);
  1627. X    old.len = 1;
  1628. X    *old.v = 0;
  1629. X    old.sign = 0;
  1630. X    /*
  1631. X     * Main divide and average loop
  1632. X     */
  1633. X    for (;;) {
  1634. X        zpowi(try, k1, &temp);
  1635. X        zquo(z1, temp, &quo);
  1636. X        freeh(temp.v);
  1637. X        i = zrel(try, quo);
  1638. X        if (i <= 0) {
  1639. X            /*
  1640. X             * Current try is less than or equal to the root since it is
  1641. X             * less than the quotient. If the quotient is equal to the try,
  1642. X             * we are all done.  Also, if the try is equal to the old value,
  1643. X             * we are done since no improvement occurred.
  1644. X             * If not, save the improved value and loop some more.
  1645. X             */
  1646. X            if ((i == 0) || (zcmp(old, try) == 0)) {
  1647. X                freeh(quo.v);
  1648. X                freeh(old.v);
  1649. X                try.sign = (HALF)sign;
  1650. X                quicktrim(try);
  1651. X                *dest = try;
  1652. X                return;
  1653. X            }
  1654. X            old.len = try.len;
  1655. X            copyval(try, old);
  1656. X        }
  1657. X        /* average current try and quotent for the new try */
  1658. X        zmul(try, k1, &temp);
  1659. X        freeh(try.v);
  1660. X        zadd(quo, temp, &temp2);
  1661. X        freeh(temp.v);
  1662. X        freeh(quo.v);
  1663. X        zquo(temp2, z2, &try);
  1664. X        freeh(temp2.v);
  1665. X    }
  1666. X}
  1667. X
  1668. X
  1669. X/*
  1670. X * Test to see if a number is an exact square or not.
  1671. X */
  1672. XBOOL
  1673. Xzissquare(z)
  1674. X    ZVALUE z;        /* number to be tested */
  1675. X{
  1676. X    long n, i;
  1677. X    ZVALUE tmp;
  1678. X
  1679. X    if (z.sign)        /* negative */
  1680. X        return FALSE;
  1681. X    while ((z.len > 1) && (*z.v == 0)) {    /* ignore trailing zero words */
  1682. X        z.len--;
  1683. X        z.v++;
  1684. X    }
  1685. X    if (isleone(z))        /* zero or one */
  1686. X        return TRUE;
  1687. X    n = *z.v & 0xf;        /* check mod 16 values */
  1688. X    if ((n != 0) && (n != 1) && (n != 4) && (n != 9))
  1689. X        return FALSE;
  1690. X    n = *z.v & 0xff;    /* check mod 256 values */
  1691. X    i = 0x80;
  1692. X    while (((i * i) & 0xff) != n)
  1693. X        if (--i <= 0)
  1694. X            return FALSE;
  1695. X    n = zsqrt(z, &tmp);    /* must do full square root test now */
  1696. X    freeh(tmp.v);
  1697. X    return n;
  1698. X}
  1699. X
  1700. X/* END CODE */
  1701. END_OF_FILE
  1702. if test 34345 -ne `wc -c <'zfunc.c'`; then
  1703.     echo shar: \"'zfunc.c'\" unpacked with wrong size!
  1704. fi
  1705. # end of 'zfunc.c'
  1706. fi
  1707. echo shar: End of archive 18 \(of 21\).
  1708. cp /dev/null ark18isdone
  1709. MISSING=""
  1710. 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
  1711.     if test ! -f ark${I}isdone ; then
  1712.     MISSING="${MISSING} ${I}"
  1713.     fi
  1714. done
  1715. if test "${MISSING}" = "" ; then
  1716.     echo You have unpacked all 21 archives.
  1717.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1718. else
  1719.     echo You still need to unpack the following archives:
  1720.     echo "        " ${MISSING}
  1721. fi
  1722. ##  End of shell archive.
  1723. exit 0
  1724.