Math-AnyNum

 view release on metacpan or  search on metacpan

lib/Math/AnyNum.pod  view on Meta::CPAN


The integer part of the logarithm of C<n> to base C<2> or base C<10>, respectively.

=head2 and | or | xor | not | lsft | rsft

    x & y                                         #=> Int
    x | y                                         #=> Int
    x ^ y                                         #=> Int
    ~x                                            #=> Int
    x << y                                        #=> Int
    x >> y                                        #=> Int

The bitwise integer operations.

=head2 lcm

    lcm(@list)                                    #=> Int

The least common multiple of a list of integers.

=head2 gcd

    gcd(@list)                                    #=> Int

The greatest common divisor of a list of integers.

=head2 gcdext

    gcdext(n, k)                                  #=> (Int, Int, Int)

The extended greatest common divisor of C<n> and C<k>, returning C<(u, v, d)>, where C<d> is
the greatest common divisor of C<n> and C<k>, while C<u> and C<v> are the coefficients satisfying
C<u*n + v*k = d>. The value of C<d> is always non-negative.

=head2 valuation

    valuation(n, k)                               #=> Scalar

Returns the number of times C<n> is divisible by C<k>.

=head2 remdiv

    remdiv(n, k)                                  #=> Int

Removes all occurrences of the divisor C<k> from integer C<n>.

In general, the following statement holds true:

    remdiv(n, k) == n / k**(valuation(n, k))

=head2 kronecker

    kronecker(n, m)                               #=> Scalar

Returns the Kronecker symbol I<(n|m)>, which is a generalization of the Jacobi symbol for all integers I<m>.

=head2 faulhaber_sum

    faulhaber_sum(n, k)                           #=> Int | NaN

Computes the power sum C<1^k + 2^k + 3^k +...+ n^k>, using Faulhaber's formula.

The value for C<k> must be a non-negative integer. Returns NaN otherwise.

Example:

    faulhaber_sum(5, 2) = 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55

=head2 geometric_sum

    geometric_sum(n, r)                           #=> Any

Computes the geometric sum C<1 + r + r^2 + r^3 + ... + r^n>, using the following formula:

    geometric_sum(n, r) = (r^(n+1) - 1) / (r - 1)

Example:

    geometric_sum(5, 8) = 8^0 + 8^1 + 8^2 + 8^3 + 8^4 + 8^5 = 37449

=head2 dirichlet_sum

    dirichlet_sum(n, \&f, \&g, \&F, \&G)          #=> Int | NaN

Given two arithmetic functions C<f> and C<g>, it computes the following sum in C<O(sqrt(n))> steps:

    Sum_{k=1..n} Sum_{d|k} f(d) * g(k/d)

The C<F> and C<G> functions are the partial sums of C<f> and C<g>, respectively:

    F(n) = Sum_{k=1..n} f(k)
    G(n) = Sum_{k=1..n} g(k)

However, this method is fast only when C<F(n)> and C<G(n)> can be computed efficiently.

Example:

    # Computes:
    #   Sum_{k=1..10^9} sigma_2(k) (C.f. A188138)

    dirichlet_sum(
        10**9,                              # n
        sub { 1 },                          # f
        sub { $_[0]**2 },                   # g
        sub { $_[0] },                      # F(n) = Sum_{k=1..n} f(k)
        sub { faulhaber_sum($_[0], 2) },    # G(n) = Sum_{k=1..n} g(k)
    )

=head2 harmonic | harmfrac

    harmonic(n)                                   #=> Rat | NaN

Returns the n-th Harmonic number C<H_n>. The harmonic numbers are the sum of
reciprocals of the first C<n> natural numbers: C<1 + 1/2 + 1/3 + ... + 1/n>.

For values greater than 7000, binary splitting (Fredrik Johansson's elegant formulation) is used.

=head2 secant_number

    secant_number(n)                              #=> Int

Returns the n-th secant number (A000364), starting with C<secant_number(0) = 1>.

=head2 tangent_number

    tangent_number(n)                             #=> Int

Returns the n-th tangent number (A000182), starting with C<tangent_number(1) = 1>.

=head2 bernoulli_polynomial

    bernoulli_polynomial(n, x)                    #=> Any

Returns the n-th Bernoulli polynomial: C<B_n(x)>.

=head2 faulhaber_polynomial | faulhaber

    faulhaber_polynomial(n, x)                        #=> Any

Returns the n-th Faulhaber polynomial: C<F_n(x)>.

=head2 euler_polynomial

    euler_polynomial(n, x)                        #=> Any

Returns the n-th Euler polynomial: C<E_n(x)>.

=head2 bernoulli | bernfrac

    bernoulli(n)                                  #=> Rat | NaN
    bernoulli(n, x)                               #=> Any

Returns the n-th Bernoulli number C<B_n> as an exact fraction, with C<bernoulli(1) = 1/2>.

When an additional argument is provided, it returns the n-th Bernoulli polynomial: C<B_n(x)>.

=head2 euler

    euler(n)                                      #=> Rat | NaN
    euler(n, x)                                   #=> Any

Returns the n-th Euler number C<E_n>, starting with C<euler(0) = 1>.

When an additional argument is provided, it returns the n-th Euler polynomial: C<E_n(x)>.

=head2 lucas

    lucas(n)                                      #=> Int | NaN

The n-th Lucas number. Returns NaN when C<n> is negative.

=head2 lucasU

    lucasU(P, Q, n)                               #=> Int | NaN

The Lucas C<U_n(P, Q)> function.



( run in 0.956 second using v1.01-cache-2.11-cpan-e1769b4cff6 )