Functions and Variables for Number Theory
=========================================

 -- Function: bern (N)
     Returns the N'th Bernoulli number for integer N. Bernoulli numbers equal to zero are suppressed if zerobern is false.

     See also burn.

          (%i1) zerobern: true$
          (%i2) map (bern, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
                            1  1       1      1        1
          (%o2)       [1, - -, -, 0, - --, 0, --, 0, - --]
                            2  6       30     42       30
          (%i3) zerobern: false$
          (%i4) map (bern, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
                      1  1    1   5     691   7    3617  43867
          (%o4) [1, - -, -, - --, --, - ----, -, - ----, -----]
                      2  6    30  66    2730  6    510    798

 -- Function: bernpoly (X, N)
     Returns the N'th Bernoulli polynomial in the variable X.

 -- Function: bfzeta (S, N)
     Returns the Riemann zeta function for the argument S. The return value is a big float (bfloat); N is the number of digits in the return value.

     load ("bffac") loads this function.

 -- Function: bfhzeta (S, H, N)
     Returns the Hurwitz zeta function for the arguments S and H. The return value is a big float (bfloat); N is the number of digits in the return value.

     The Hurwitz zeta function is defined as

          sum ((k+h)^-s, k, 0, inf)

     load ("bffac") loads this function.

 -- Function: binomial (X, Y)
     The binomial coefficient X!/(Y! (X - Y)!). If X and Y are integers, then the numerical value of the binomial coefficient is computed. If Y, or X - Y, is an integer, the binomial coefficient is expressed as a polynomial.

     Examples:

          (%i1) binomial (11, 7);
          (%o1)                          330
          (%i2) 11! / 7! / (11 - 7)!;
          (%o2)                          330
          (%i3) binomial (x, 7);
                  (x - 6) (x - 5) (x - 4) (x - 3) (x - 2) (x - 1) x
          (%o3)   -------------------------------------------------
                                        5040
          (%i4) binomial (x + 7, x);
                (x + 1) (x + 2) (x + 3) (x + 4) (x + 5) (x + 6) (x + 7)
          (%o4) -------------------------------------------------------
                                         5040
          (%i5) binomial (11, y);
          (%o5)                    binomial(11, y)

 -- Function: burn (N)
     Returns the N'th Bernoulli number for integer N. burn may be more efficient than bern for large, isolated N (perhaps N greater than 105 or so), as bern computes all the Bernoulli numbers up to index N before returning.

     burn exploits the observation that (rational) Bernoulli numbers can be approximated by (transcendental) zetas with tolerable efficiency.

     load ("bffac") loads this function.

 -- Function: cf (EXPR)
     Converts EXPR into a continued fraction. EXPR is an expression comprising continued fractions and square roots of integers. Operands in the expression may be combined with arithmetic operators. Aside from continued fractions and square roots, factors in the expression must be integer or rational numbers. Maxima does not know about operations on continued fractions outside of cf.

     cf evaluates its arguments after binding listarith to false. cf returns a continued fraction, represented as a list.

     A continued fraction a + 1/(b + 1/(c + ...)) is represented by the list [a, b, c, ...]. The list elements a, b, c, ... must evaluate to integers. EXPR may also contain sqrt (n) where n is an integer. In this case cf will give as many terms of the continued fraction as the value of the variable cflength times the period.

     A continued fraction can be evaluated to a number by evaluating the arithmetic representation returned by cfdisrep. See also cfexpand for another way to evaluate a continued fraction.

     See also cfdisrep, cfexpand, and cflength.

     Examples:

       * EXPR is an expression comprising continued fractions and square roots of integers.

               (%i1) cf ([5, 3, 1]*[11, 9, 7] + [3, 7]/[4, 3, 2]);
               (%o1)               [59, 17, 2, 1, 1, 1, 27]
               (%i2) cf ((3/17)*[1, -2, 5]/sqrt(11) + (8/13));
               (%o2)        [0, 1, 1, 1, 3, 2, 1, 4, 1, 9, 1, 9, 2]

       * cflength controls how many periods of the continued fraction are computed for algebraic, irrational numbers.

               (%i1) cflength: 1$
               (%i2) cf ((1 + sqrt(5))/2);
               (%o2)                    [1, 1, 1, 1, 2]
               (%i3) cflength: 2$
               (%i4) cf ((1 + sqrt(5))/2);
               (%o4)               [1, 1, 1, 1, 1, 1, 1, 2]
               (%i5) cflength: 3$
               (%i6) cf ((1 + sqrt(5))/2);
               (%o6)           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2]

       * A continued fraction can be evaluated by evaluating the arithmetic representation returned by cfdisrep.

               (%i1) cflength: 3$
               (%i2) cfdisrep (cf (sqrt (3)))$
               (%i3) ev (%, numer);
               (%o3)                   1.731707317073171

       * Maxima does not know about operations on continued fractions outside of cf.

               (%i1) cf ([1,1,1,1,1,2] * 3);
               (%o1)                     [4, 1, 5, 2]
               (%i2) cf ([1,1,1,1,1,2]) * 3;
               (%o2)                  [3, 3, 3, 3, 3, 6]

 -- Function: cfdisrep (LIST)
     Constructs and returns an ordinary arithmetic expression of the form a + 1/(b + 1/(c + ...)) from the list representation of a continued fraction [a, b, c, ...].

          (%i1) cf ([1, 2, -3] + [1, -2, 1]);
          (%o1)                     [1, 1, 1, 2]
          (%i2) cfdisrep (%);
                                            1
          (%o2)                     1 + ---------
                                              1
                                        1 + -----
                                                1
                                            1 + -
                                                2

 -- Function: cfexpand (X)
     Returns a matrix of the numerators and denominators of the last (column 1) and next-to-last (column 2) convergents of the continued fraction X.

          (%i1) cf (rat (ev (%pi, numer)));

          `rat' replaced 3.141592653589793 by 103993/33102 =3.141592653011902
          (%o1)                  [3, 7, 15, 1, 292]
          (%i2) cfexpand (%);
                                   [ 103993  355 ]
          (%o2)                    [             ]
                                   [ 33102   113 ]
          (%i3) %[1,1]/%[2,1], numer;
          (%o3)                   3.141592653011902

 -- Option variable: cflength
     Default value: 1

     cflength controls the number of terms of the continued fraction the function cf will give, as the value cflength times the period.  Thus the default is to give one period.

          (%i1) cflength: 1$
          (%i2) cf ((1 + sqrt(5))/2);
          (%o2)                    [1, 1, 1, 1, 2]
          (%i3) cflength: 2$
          (%i4) cf ((1 + sqrt(5))/2);
          (%o4)               [1, 1, 1, 1, 1, 1, 1, 2]
          (%i5) cflength: 3$
          (%i6) cf ((1 + sqrt(5))/2);
          (%o6)           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2]

 -- Function: divsum (N, K)
 -- Function: divsum (N)

     divsum (N, K) returns the sum of the divisors of N raised to the K'th power.

     divsum (N) returns the sum of the divisors of N.

          (%i1) divsum (12);
          (%o1)                          28
          (%i2) 1 + 2 + 3 + 4 + 6 + 12;
          (%o2)                          28
          (%i3) divsum (12, 2);
          (%o3)                          210
          (%i4) 1^2 + 2^2 + 3^2 + 4^2 + 6^2 + 12^2;
          (%o4)                          210

 -- Function: euler (N)
     Returns the N'th Euler number for nonnegative integer N.

     For the Euler-Mascheroni constant, see %gamma.

          (%i1) map (euler, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
          (%o1)    [1, 0, - 1, 0, 5, 0, - 61, 0, 1385, 0, - 50521]

 -- Constant: %gamma
     The Euler-Mascheroni constant, 0.5772156649015329 ....

 -- Function: factorial (X)
     Represents the factorial function. Maxima treats factorial (X) the same as X!. See !.

 -- Function: fib (N)
     Returns the N'th Fibonacci number. fib(0) equal to 0 and fib(1) equal to 1, and fib (-N) equal to (-1)^(N + 1) * fib(N).

     After calling fib, prevfib is equal to fib (X - 1), the Fibonacci number preceding the last one computed.

          (%i1) map (fib, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
          (%o1)         [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

 -- Function: fibtophi (EXPR)
     Expresses Fibonacci numbers in EXPR in terms of the constant %phi, which is (1 + sqrt(5))/2, approximately 1.61803399.

     Examples:

          (%i1) fibtophi (fib (n));
                                     n             n
                                 %phi  - (1 - %phi)
          (%o1)                  -------------------
                                     2 %phi - 1
          (%i2) fib (n-1) + fib (n) - fib (n+1);
          (%o2)          - fib(n + 1) + fib(n) + fib(n - 1)
          (%i3) fibtophi (%);
                      n + 1             n + 1       n             n
                  %phi      - (1 - %phi)        %phi  - (1 - %phi)
          (%o3) - --------------------------- + -------------------
                          2 %phi - 1                2 %phi - 1
                                                    n - 1             n - 1
                                                %phi      - (1 - %phi)
                                              + ---------------------------
                                                        2 %phi - 1
          (%i4) ratsimp (%);
          (%o4)                           0

 -- Function: ifactors (N)
     For a positive integer N returns the factorization of N. If n=p1^e1..pk^nk is the decomposition of N into prime factors, ifactors returns [[p1, e1], ... , [pk, ek]].

     Factorization methods used are trial divisions by primes up to 9973, Pollard's rho method and elliptic curve method.

          (%i1) ifactors(51575319651600);
          (%o1)     [[2, 4], [3, 2], [5, 2], [1583, 1], [9050207, 1]]
          (%i2) apply("*", map(lambda([u], u[1]^u[2]), %));
          (%o2)                        51575319651600

 -- Function: inrt (X, N)
     Returns the integer N'th root of the absolute value of X.

          (%i1) l: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]$
          (%i2) map (lambda ([a], inrt (10^a, 3)), l);
          (%o2) [2, 4, 10, 21, 46, 100, 215, 464, 1000, 2154, 4641, 10000]

 -- Function: inv_mod (N, M)
     Computes the inverse of N modulo M. inv_mod (n,m) returns false, if N is a zero divisor modulo M.

          (%i1) inv_mod(3, 41);
          (%o1)                           14
          (%i2) ratsimp(3^-1), modulus=41;
          (%o2)                           14
          (%i3) inv_mod(3, 42);
          (%o3)                          false

 -- Function: jacobi (P, Q)
     Returns the Jacobi symbol of P and Q.

          (%i1) l: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]$
          (%i2) map (lambda ([a], jacobi (a, 9)), l);
          (%o2)         [1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0]

 -- Function: lcm (EXPR_1, ..., EXPR_N)
     Returns the least common multiple of its arguments. The arguments may be general expressions as well as integers.

     load ("functs") loads this function.

 -- Function: minfactorial (EXPR)
     Examines EXPR for occurrences of two factorials which differ by an integer. minfactorial then turns one into a polynomial times the other.

          (%i1) n!/(n+2)!;
                                         n!
          (%o1)                       --------
                                      (n + 2)!
          (%i2) minfactorial (%);
                                          1
          (%o2)                    ---------------
                                   (n + 1) (n + 2)

 -- Function: next_prime (N)
     Returns the smallest prime bigger than N.

          (%i1) next_prime(27);
          (%o1)                       29

 -- Function: partfrac (EXPR, VAR)
     Expands the expression EXPR in partial fractions with respect to the main variable VAR.  partfrac does a complete partial fraction decomposition.  The algorithm employed is based on the fact that the denominators of the partial fraction expansion (the factors of the original denominator) are relatively prime.  The numerators can be written as linear combinations of denominators, and the expansion falls out.

          (%i1) 1/(1+x)^2 - 2/(1+x) + 2/(2+x);
                                2       2        1
          (%o1)               ----- - ----- + --------
                              x + 2   x + 1          2
                                              (x + 1)
          (%i2) ratsimp (%);
                                           x
          (%o2)                 - -------------------
                                   3      2
                                  x  + 4 x  + 5 x + 2
          (%i3) partfrac (%, x);
                                2       2        1
          (%o3)               ----- - ----- + --------
                              x + 2   x + 1          2
                                              (x + 1)

 -- Function: power_mod (A, N, M)
     Uses a modular algorithm to compute a^n mod m where A and N are integers and M is a positive integer. If N is negative, inv_mod is used to find the modular inverse.

          (%i1) power_mod(3, 15, 5);
          (%o1)                          2
          (%i2) mod(3^15,5);
          (%o2)                          2
          (%i3) power_mod(2, -1, 5);
          (%o3)                          3
          (%i4) inv_mod(2,5);
          (%o4)                          3

 -- Function: primep (N)
     Primality test. If primep (n) returns false, N is a composite number and if it returns true, N is a prime number with very high probability.

     For N less than 341550071728321 a deterministic version of Miller-Rabin's test is used. If primep (n) returns true, then N is a prime number.

     For N bigger than 34155071728321 primep uses primep_number_of_tests Miller-Rabin's pseudo-primality tests and one Lucas pseudo-primality test. The probability that N will pass one Miller-Rabin test is less than 1/4. Using the default value 25 for primep_number_of_tests, the probability of N beeing composite is much smaller that 10^-15.

 -- Option variable: primep_number_of_tests
     Default value: 25

     Number of Miller-Rabin's tests used in primep.

 -- Function: prev_prime (N)
     Returns the greatest prime smaller than N.

          (%i1) prev_prime(27);
          (%o1)                       23

 -- Function: qunit (N)
     Returns the principal unit of the real quadratic number field sqrt (N) where N is an integer, i.e., the element whose norm is unity. This amounts to solving Pell's equation a^2 - N b^2 = 1.

          (%i1) qunit (17);
          (%o1)                     sqrt(17) + 4
          (%i2) expand (% * (sqrt(17) - 4));
          (%o2)                           1

 -- Function: totient (N)
     Returns the number of integers less than or equal to N which are relatively prime to N.

 -- Option variable: zerobern
     Default value: true

     When zerobern is false, bern excludes the Bernoulli numbers which are equal to zero. See bern.

 -- Function: zeta (N)
     Returns the Riemann zeta function if X is a negative integer, 0, 1, or a positive even number, and returns a noun form zeta (N) for all other arguments, including rational noninteger, floating point, and complex arguments.

     See also bfzeta and zeta%pi.

          (%i1) map (zeta, [-4, -3, -2, -1, 0, 1, 2, 3, 4, 5]);
                                               2              4
                     1        1     1       %pi            %pi
          (%o1) [0, ---, 0, - --, - -, inf, ----, zeta(3), ----, zeta(5)]
                    120       12    2        6              90

 -- Option variable: zeta%pi
     Default value: true

     When zeta%pi is true, zeta returns an expression proportional to %pi^n for even integer n. Otherwise, zeta returns a noun form zeta (n) for even integer n.

          (%i1) zeta%pi: true$
          (%i2) zeta (4);
                                           4
                                        %pi
          (%o2)                         ----
                                         90
          (%i3) zeta%pi: false$
          (%i4) zeta (4);
          (%o4)                        zeta(4)