Fetch OEIS Sequences

May 21, 2019

I wrote a simple pipeline of Unix commands and stored it in a shell script:

$ cat >oeis
# oeis sequence -- fetch sequence from oeis.org
# write sequence items one per line to stdout
# example: to fetch prime numbers: oeis a000040
wget -qO- oeis.org/$1/internal | # retrieve sequence
grep "%[STU]" | # delete extra lines
sed 's/^.*%[STU] //' | # remove html command
awk '{printf "%s", $0}' | # join into one line
tr "," "\n" # each on its own line
CTRL-D
$ chmod +x oeis
$ oeis a000040 | head -5
2
3
5
7
11
$ oeis a000040 | tr "\n" ","
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271
$ primes=$(oeis a000040)
$ echo $primes
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271
$ for p in $(oeis a000040)
> do echo "$p \c"
> done
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271
$ ed
r !oeis a000040
202
,p
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271
w primes
q

You can see the program, but not run it, at https://ideone.com/PI73w2.

Advertisements

Pages: 1 2

2 Responses to “Fetch OEIS Sequences”

  1. Alex B. said

    Since OEIS offers a nice JSON format, I thought I’d take advantage:

    #! /usr/bin/env python3
    
    import sys
    
    import requests
    
    OEIS_URL = 'https://oeis.org/search'
    
    def get_seq(seq_name):
        """Fetch and return an OEIS sequence in JSON format"""
        params = {
            'q': f'id:{seq_name}',
            'fmt': 'json',
            }
        response = requests.get(OEIS_URL, params)
        response.raise_for_status()
        return response.json()
    
    def extract_seq(seq_json):
        """Extract and return the sequences from OEIS JSON data"""
        results = seq_json['results']
        seqs = dict()
        for result in results:
            number = result['number']
            a_id = f'A{number:06}'
            seqs[a_id] = result['data']
        if seqs:
            return seqs
        else:
            return None
    
    if __name__ == '__main__':
        seq_id = sys.argv[1]
        seq_json = get_seq(seq_id)
        seq_data = extract_seq(seq_json)
        if seq_data is not None:
            for seq in seq_data:
                print(f'{seq}: {seq_data[seq]}')
        else:
            print('No sequence found')
    

    get_seq adds the ‘id:’ prefix to the search term, so only one sequence is fetched. Next step is to add command line switches to control that behaviour, so multiple sequences can be fetched at once.

  2. Daniel said

    Here’s a solution in Python.

    import json
    import sys
    import urllib.request
    
    assert len(sys.argv) == 2, 'ID argument required'
    url = f'https://oeis.org/search?q=id:{sys.argv[1]}&fmt=json'
    with urllib.request.urlopen(url) as u:
        body = u.read()
    data = json.loads(body)['results'][0]['data'].split(',')
    print('\n'.join(data))
    

    Example Usage:

    $ python oeis.py A000045
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    ...
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: