# 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?

-
– 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

1
– Simon Jan 24 '12 at 1:37

## 3 Answers

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}
````
-

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 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.

-
A disadvantage of this is that it only works over the internet, not locally. – celtschk Jan 18 '12 at 11:39

2
@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

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]]
````

Edit: Come to think of it, if you just want the numbers, it could be even easier to just import from http://oeis.org/A004001/list

-

1
Nice piece of work – niklasfi Jan 17 '12 at 23:22
lang-mma
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.872684121131897, "perplexity_flag": "middle"}