Skip to content

Add functionality for fetching OEIS sequence terms from b-files #41005

@maxale

Description

@maxale

Problem Description

OEIS module provides a function first_terms(), which gets terms of a given sequence id from OEIS. However, it can get only terms that are listed in the OEIS entry itself, which is typically limited to storing "3 lines" of data. At the same time, all sequences in the OEIS are supplemented with b-files, which are are not limited in size and can store thousands of terms.

For example, for the sequence A000001 such file is b-file is available at https://oeis.org/A000001/b000001.txt and it contains 2048 terms, while this is what we can get from first_terms():

sage: len(oeis('A000001').first_terms())
94

Format of b-files is simple and described at https://oeis.org/wiki/B-files

It will be helpful to have a built-in functionality for downloading b-files and getting all terms from there.

Proposed Solution

For example, provide an parameter download= to first_terms(), setting which to True will instruct to download b-file and parse terms from it.

Alternatives Considered

My current workaround goes like this:

from urllib.request import *

# downloads a b-file for a given sequence id, and return it in the form of a dict: { index : term }
@cached_function
def oeis_all_terms(seq_id):
    terms = dict()                      # map each term index to its value
    seq_id = str(seq_id).upper()        # make sure seq_id is an uppercase string
    num_id = (seq_id[1:] if seq_id[0]=='A' else seq_id).zfill(6)
    for l in urlopen(Request(f"https://oeis.org/b{num_id}.txt", headers={'User-Agent': 'bfileFetcher/1.0'})):
        v = [e.decode() for e in l.strip().split()]
        if len(v)==2 and all(e.lstrip('-').isdigit() for e in v):
            terms[int(v[0])] = int(v[1])
    assert len(terms) == max(terms.keys()) - min(terms.keys()) + 1
    return terms

I hope it can be incorporated into Sage.

Additional Information

No response

Is there an existing issue for this?

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions