Maxima Manual: 31. Number Theory

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 31. Number Theory

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 31.1 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

  Categories: Number theory

Function: bernpoly (x, n)

  Returns the n'th Bernoulli polynomial in the variable x.

  Categories: Number theory

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.

  Categories: Number theory · Numerical evaluation

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.

  Categories: Number theory · Numerical evaluation

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)

  Categories: Number theory

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.

  Categories: Number theory

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]

  Categories: Continued fractions

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

  Categories: Continued fractions

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

  Categories: Continued fractions

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]

  Categories: Continued fractions

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

  Categories: Number theory

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]

  Categories: Number theory

Constant: %gamma

  The Euler-Mascheroni constant, 0.5772156649015329 ....

  Categories: Constants

Function: factorial (x)

  Represents the factorial function. Maxima treats `factorial (x)` the same as `x!`. See `!`.

  Categories: Gamma and factorial functions

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]

  Categories: Number theory

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

  Categories: Number theory

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

  Categories: Number theory

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]

  Categories: Number theory

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

  Categories: Number theory

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]

  Categories: Number theory

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.

  Categories: Number theory

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)

  Categories: Number theory

Function: next_prime (n)

  Returns the smallest prime bigger than n.

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

  Categories: Number theory

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

  Categories: Number theory

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 10^16 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 10^16 `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.

  Categories: Predicate functions · Number theory

Option variable: primep_number_of_tests

  Default value: 25

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

  Categories: Predicate functions · Number theory

Function: prev_prime (n)

  Returns the greatest prime smaller than n.

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

  Categories: Number theory

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

  Categories: Number theory

Function: totient (n)

  Returns the number of integers less than or equal to n which are relatively prime to n.

  Categories: Number theory

Option variable: zerobern

  Default value: `true`

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

  Categories: Number theory

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

  Categories: Number theory

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)

  Categories: Number theory

[ << ] | [ >> ] |  |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated by Robert Dodier on August, 10 2008 using texi2html 1.76.