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

29. Number Theory

0


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

29.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
·

@ref{Category: Number theory}

Function: bernpoly (x, n)

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

·

@ref{Category: 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.

·

@ref{Category: Number theory} · @ref{Category: 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

                        inf
                        ====
                        \        1
         zeta (s,h)  =   >    --------
                        /            s
                        ====  (k + h)
                        k = 0

load ("bffac") loads this function.

·

@ref{Category: Number theory} · @ref{Category: Numerical evaluation}

Function: burn (n)

Returns a rational number, which is an approximation of the n'th Bernoulli number for integer n. burn exploits the observation that (rational) Bernoulli numbers can be approximated by (transcendental) zetas with tolerable efficiency:

                   n - 1  1 - 2 n
              (- 1)      2        zeta(2 n) (2 n)!
     B(2 n) = ------------------------------------
                                2 n
                             %pi

burn may be more efficient than bern for large, isolated n as bern computes all the Bernoulli numbers up to index n before returning. burn invokes the approximation for even integers n > 255. For odd integers and n <= 255 the function bern is called.

load ("bffac") loads this function. See also bern.

·

@ref{Category: 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:

·

@ref{Category: 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
·

@ref{Category: 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
·

@ref{Category: 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]
·

@ref{Category: 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
·

@ref{Category: 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]
·

@ref{Category: Number theory}

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]
·

@ref{Category: 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
·

@ref{Category: 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
·

@ref{Category: Number theory}

Function: igcdex (n, k)

Returns a list [a, b, u] where u is the greatest common divisor of n and k, and u is equal to a n + b k. The arguments n and k must be integers.

igcdex implements the Euclidean algorithm. See also gcdex.

The command load(gcdex) loads the function.

Examples:

(%i1) load(gcdex)$

(%i2) igcdex(30,18);
(%o2)                      [- 1, 2, 6]
(%i3) igcdex(1526757668, 7835626735736);
(%o3)            [845922341123, - 164826435, 4]
(%i4) igcdex(fib(20), fib(21));
(%o4)                   [4181, - 2584, 1]
·

@ref{Category: 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]
·

@ref{Category: 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
·

@ref{Category: Number theory}

Function: isqrt (x)

Returns the "integer square root" of the absolute value of x, which is an integer.

·

@ref{Category: Mathematical functions}

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]
·

@ref{Category: 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.

·

@ref{Category: Number theory}

Function: mod (x, y)

If x and y are real numbers and y is nonzero, return x - y * floor(x / y). Further for all real x, we have mod (x, 0) = x. For a discussion of the definition mod (x, 0) = x, see Section 3.4, of "Concrete Mathematics," by Graham, Knuth, and Patashnik. The function mod (x, 1) is a sawtooth function with period 1 with mod (1, 1) = 0 and mod (0, 1) = 0.

To find the principal argument (a number in the interval (-%pi, %pi]) of a complex number, use the function x |-> %pi - mod (%pi - x, 2*%pi), where x is an argument.

When x and y are constant expressions (10 * %pi, for example), mod uses the same big float evaluation scheme that floor and ceiling uses. Again, it's possible, although unlikely, that mod could return an erroneous value in such cases.

For nonnumerical arguments x or y, mod knows several simplification rules:

(%i1) mod (x, 0);
(%o1)                           x
(%i2) mod (a*x, a*y);
(%o2)                      a mod(x, y)
(%i3) mod (0, x);
(%o3)                           0
·

@ref{Category: Mathematical functions}

Function: next_prime (n)

Returns the smallest prime bigger than n.

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

@ref{Category: 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
·

@ref{Category: 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 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 341550071728321 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.

·

@ref{Category: Predicate functions} · @ref{Category: Number theory}

Option variable: primep_number_of_tests

Default value: 25

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

·

@ref{Category: Predicate functions} · @ref{Category: Number theory}

Function: prev_prime (n)

Returns the greatest prime smaller than n.

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

@ref{Category: 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
·

@ref{Category: Number theory}

Function: totient (n)

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

·

@ref{Category: Number theory}

Option variable: zerobern

Default value: true

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

·

@ref{Category: Number theory}

Function: zeta (n)

Returns the Riemann zeta function. If n is a negative integer, 0, or a positive even integer, the Riemann zeta function simplifies to an exact value. For a positive even integer the option variable zeta%pi has to be true in addition (See zeta%pi). For a floating point or bigfloat number the Riemann zeta function is evaluated numerically. Maxima returns a noun form zeta (n) for all other arguments, including rational noninteger, and complex arguments, or for even integers, if zeta%pi has the value false.

zeta(1) is undefined, but Maxima knows the limit limit(zeta(x), x, 1) from above and below.

The Riemann zeta function distributes over lists, matrices, and equations.

See also bfzeta and zeta%pi.

Examples:

(%i1) zeta([-2, -1, 0, 0.5, 2, 3, 1+%i]);
                                             2
            1     1                       %pi
(%o1) [0, - --, - -, - 1.460354508809586, ----, zeta(3), 
            12    2                        6
                                                    zeta(%i + 1)]
(%i2) limit(zeta(x),x,1,plus);
(%o2)                          inf
(%i3) limit(zeta(x),x,1,minus);
(%o3)                         minf
·

@ref{Category: 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.

Examples:

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

@ref{Category: Number theory}

0


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

This document was generated by Charlie & on February, 13 2012 using texi2html 1.76.