# Is it possible to invoke the OEIS from Mathematica?

I had always wondered if there might be a way to write a function, which I'll call OEISData[], that more or less works as a curated data function for The On-Line Encyclopedia of Integer Sequences.

I would imagine that the usage might be a little something like this:

OEISData["A004001"][9]
5

OEISData["A003418"][Range[8, 15]]
840, 2520, 2520, 27720, 27720, 360360, 360360, 360360

OEISData["A005849", "Keywords"]
{"hard", "nonn", "nice", "more"}


An API or something to retrieve data from the OEIS site might be needed for an implementation of this function. Is a function like this possible, with what Mathematica is currently capable of?

-
You'll probably have to write a webscraper. At least according to stackoverflow.com/q/5991756/421225 –  Simon Jan 17 '12 at 23:06
...I wasn't specifically asking for a Wolfram Alpha solution... –  Ｊ. Ｍ. Jan 21 '12 at 6:03
There was a recent W|A blog post on identifying sequences, which makes a brief mention of the OEIS. –  Simon Jan 24 '12 at 1:37

There is a Mathematica package exactly for this at the OEIS wiki.

Somewhat related: there's also a package for formatting data into the OEIS format.

WolframAlpha also has some of this information, though I'm not sure how to get the $n^{\mathrm{th}}$ term of the sequence.

In[1] := WolframAlpha["A004001", {{"TermsPod:IntegerSequence", 1}, "ComputableData"}]

Out[1] = {1, 1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 7, 8, 8, 8, 8, 9, 10, 11,
12, 12, 13, 14, 14, 15}


Or:

In[1] := WolframAlpha["A018900", {{"Continuation", 1}, "ComputableData"}]

Out[1] = {3, 5, 6, 9, 10, 12, 17, 18, 20, 24, 33, 34, 36, 40, 48, 65, 66, 68, 72}

-
I was aware of the second one by Eric Weisstein, but not aware of the first. Thanks! –  Ｊ. Ｍ. Jan 18 '12 at 1:00
I've added another example as the first extraction method did not work for me. I hope you don't mind! –  István Zachar Sep 13 '13 at 22:26

A bit of a hack, could do with some polishing, but the basic idea will work:

OEISData[str_] :=
StringSplit[#, ","] & /@
Select[StringSplit[Import["http://oeis.org/search?q=" <> str]],
StringMatchQ[#, __ ~~ ","] &];

OEISData["A004001"][[9]]


If you just want the numbers, it could be even easier to just import from http://oeis.org/A004001/list (assuming that the input is a valid sequence identifier):

OEISSequence[str_] := ToExpression /@
First@StringCases[Import["http://oeis.org/" <> str <> "/list"],
"[" ~~ x__ ~~ "]" :> StringSplit[x, ","]];

Take[OEISSequence["A004001"], 20]

{1, 1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 7, 8, 8, 8, 8, 9, 10, 11, 12}

-
Nice piece of work –  niklasfi Jan 17 '12 at 23:22

It sounds like this might be helpful. The following notebook allows you to specify a sequence and automatically import a detailed list of matching entries from the OEIS:

http://www.brotherstechnology.com/math/oeis_mathematica.html

-