Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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["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?

share|improve this question
You'll probably have to write a webscraper. At least according to – Simon Jan 17 '12 at 23:06
...I wasn't specifically asking for a Wolfram Alpha solution... – J. M. 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
I don't think OEIS has standardized, ready-to-use Mathematica code for computing the n-th or the first n elements of any sequence. Many entries do have Mathematica code though, maybe it wouldn't be too hard to generalize these examples. – masterxilo Jun 25 at 23:19
@master, certainly; the "difficult" sequences come to mind. Regarding the code posted there: the code there is of varying quality, and a number could stand to be tuned. – J. M. Jun 26 at 1:48
up vote 33 down vote accepted

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}


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}
share|improve this answer
I was aware of the second one by Eric Weisstein, but not aware of the first. Thanks! – J. M. 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["" <> str]], 
  StringMatchQ[#, __ ~~ ","] &];


If you just want the numbers, it could be even easier to just import from (assuming that the input is a valid sequence identifier):

OEISSequence[str_] := ToExpression /@ 
        First@StringCases[Import["" <> 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}
share|improve this answer
Nice piece of work – niklasfi Jan 17 '12 at 23:22

I liked Szabolcs’ answer but would like to remind about free form input here. We get so much information using it for very little typing. Plus we get native to M. format. For those who does not know this yet - at the beginning of new input line press equal sign “=” twice to get orange spiky and then type in free form. In this case you see result below. This is NOT web browser but M. notebook. Of course you can get the same on W|A website. But additionally here you can get the data. For example go to “Sequence terms” pod and click “more” to get a few more terms. Then press little plus sign in the top right corner and then and from the menu choose “computable data”. This pastes in M. notebook what you see here at the lower part of the image the image. And this also partially answers Szabolcs’ question about more terms ;-) This is also a good way to learn tricks of WolframAlpha[] function.

enter image description here

share|improve this answer
A disadvantage of this is that it only works over the internet, not locally. – celtschk Jan 18 '12 at 11:39
@celtschk: Sure, but I was assuming that one needed to be connected to the Internet anyway to access stuff from the OEIS... – J. M. Jan 18 '12 at 11:43
Ah, right, how could I miss that. Unless you are running on the OEIS hosting site, of course. :-) – celtschk Jan 18 '12 at 11:53

Here is the GitHub repo for my OEIS Mathematica package:

share|improve this answer
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Karsten 7. Mar 23 '15 at 20:12

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:

share|improve this answer

Have a look at the output of

a = Entity["IntegerSequence", "A004001"];
Grid[List@@@Normal@DeleteMissing@AssociationMap[a, EntityProperties@a]]

Some of them have EntityProperty["IntegerSequence", "WolframLanguageTable"]. This seems to be a mathematica expression in String form which I assume to have the parameter n. It can be used as follows in a few cases I tested:

OEISDataTableFunction[a_String] := 
  Module[{t = 
     Entity["IntegerSequence", a]@
      EntityProperty["IntegerSequence", "WolframLanguageTable"]},
   If[MissingQ@t, Return@t];
   ToExpression["Function[{n}," <> t <> "]"]

n = 5;
"works, but n is not consistently the 'amount of terms'"

"doesn't work:"

As you can see, the data is not particularly well curated.

share|improve this answer

Instead of relying on WolframAlpha data, you can query the database directly, preferable through the /internal data format, documented here:

One such page is:

For example, %t starts a line of mathematica code. We might do the following to query all of these lines from an entry:

OEISMathematica[a_String] := Module[{s},
   s = StringSplit[Import["" <> a <> "/internal"], 
    Select[s, StringMatchQ[#, RegularExpression@"^%t.*$"] &], 
    RegularExpression@"^%t" -> ""]

(*this might take a while*)
ds = Dataset[
   OEISMathematica, {"A000139", "A000142", "A004001", "A000142", 
    "A000165", "A000165", "A001044", "A001563", "A003422", "A009445", 
    "A010050", "A012245", "A033312", "A034886", "A038507", "A047920", 

(*try it!*)
StringJoin @@ ds["A048631"] // ToExpression

This is much better populated than the W|A data, but not curated to follow any standards either: Sometimes all of the lines might belong to the same example, sometimes they might be split across multiple lines (a sample often ends in a comment (**) though).

share|improve this answer
?q=id: &fmt=text probably has less overhead: OEISMathematica[a_String] := Module[{s}, s = StringSplit[ Import["" <> a <> "&fmt=text", "Text"], "\n"]; StringReplace[ Select[s, StringMatchQ[#, RegularExpression@"^%t.*$"] &], RegularExpression@"^%t A[0-9]* " -> ""] ]; – masterxilo Jun 26 at 0:40

The Sage system has an OEIS integration:

and a package that computes some infinite OEIS sequences

It could be leveraged as an external program, and the source code can be studied.

share|improve this answer
This isn't a Mathematica answer, no? – J. M. Jul 2 at 15:15
Other programs can be considered a data source for mathematica, just like the internet ;) – masterxilo Jul 2 at 19:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.