Skip to content

Core API

oeis_code.core

Public entry point: get asks a registered sequence.

get(seq_id, n, backend='auto')

Get the n-th term of an OEIS sequence.

Parameters

seq_id : str The OEIS identifier (e.g., 'A000045'). n : int The index of the term to retrieve. backend : {'auto', 'pari', 'python'}, default 'auto' The backend to use. 'auto' will prioritize PARI/GP if available.

Returns

Any The n-th term of the sequence.

Raises

ValueError If the sequence ID is not found in the registry.

Source code in src/oeis_code/core.py
def get(seq_id: str, n: int, backend: BackendType = "auto") -> Any:
    """
    Get the n-th term of an OEIS sequence.

    Parameters
    ----------
    seq_id : str
        The OEIS identifier (e.g., 'A000045').
    n : int
        The index of the term to retrieve.
    backend : {'auto', 'pari', 'python'}, default 'auto'
        The backend to use. 'auto' will prioritize PARI/GP if available.

    Returns
    -------
    Any
        The n-th term of the sequence.

    Raises
    ------
    ValueError
        If the sequence ID is not found in the registry.
    """
    if seq_id not in registry:
        raise ValueError(f"Sequence {seq_id} not implemented")

    func = registry[seq_id]

    if backend == "auto":
        try:
            if pari_backend.AVAILABLE:
                return func(n, backend="pari")
        except Exception:
            pass
        return func(n, backend="python")

    return func(n, backend=backend)