59
$\begingroup$

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?

$\endgroup$
7
  • 1
    $\begingroup$ You'll probably have to write a webscraper. At least according to stackoverflow.com/q/5991756/421225 $\endgroup$
    – Simon
    Commented Jan 17, 2012 at 23:06
  • $\begingroup$ ...I wasn't specifically asking for a Wolfram Alpha solution... $\endgroup$ Commented Jan 21, 2012 at 6:03
  • 2
    $\begingroup$ There was a recent W|A blog post on identifying sequences, which makes a brief mention of the OEIS. $\endgroup$
    – Simon
    Commented Jan 24, 2012 at 1:37
  • 1
    $\begingroup$ Charles R Greathouse has written some lines about programs in oeis oeis.org/wiki/User:Charles_R_Greathouse_IV/Programs . In a sense, PrimeQ, Prime and NextPrime could all be programs associated with A40. We would need some more differentiation. $\endgroup$
    – masterxilo
    Commented Jul 2, 2016 at 12:18
  • 2
    $\begingroup$ related ResourceFunctions: OEISSequence and OEISSequenceData $\endgroup$
    – kglr
    Commented Sep 23, 2021 at 18:15

9 Answers 9

47
$\begingroup$

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}
$\endgroup$
6
  • $\begingroup$ I was aware of the second one by Eric Weisstein, but not aware of the first. Thanks! $\endgroup$ Commented Jan 18, 2012 at 1:00
  • 1
    $\begingroup$ I've added another example as the first extraction method did not work for me. I hope you don't mind! $\endgroup$ Commented Sep 13, 2013 at 22:26
  • $\begingroup$ I believe you can use Part[A,n] to get the nth sequence, though I can't test this at the moment. $\endgroup$ Commented Feb 10, 2017 at 11:14
  • $\begingroup$ @AlbertRenshaw Yes, that is how to extract the $n$th element of the result list. But the result only has the first 25 elements by default. How you do request the 100th element? $\endgroup$
    – Szabolcs
    Commented Feb 10, 2017 at 11:17
  • $\begingroup$ @Szabolcs Good point, not sure! $\endgroup$ Commented Feb 10, 2017 at 11:22
25
$\begingroup$

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}
$\endgroup$
1
  • 2
    $\begingroup$ Nice piece of work $\endgroup$
    – niklasfi
    Commented Jan 17, 2012 at 23:22
15
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ A disadvantage of this is that it only works over the internet, not locally. $\endgroup$
    – celtschk
    Commented Jan 18, 2012 at 11:39
  • 6
    $\begingroup$ @celtschk: Sure, but I was assuming that one needed to be connected to the Internet anyway to access stuff from the OEIS... $\endgroup$ Commented Jan 18, 2012 at 11:43
  • $\begingroup$ Ah, right, how could I miss that. Unless you are running on the OEIS hosting site, of course. :-) $\endgroup$
    – celtschk
    Commented Jan 18, 2012 at 11:53
  • $\begingroup$ Well it should be possible to cache the results indefinitely for local execution. The sequences are not going to change afterall (immutability FTW). $\endgroup$
    – masterxilo
    Commented Oct 30, 2017 at 16:22
  • $\begingroup$ This might also be a license issue. CC-NC allows online access but may prohibit storing data in a commercial product. $\endgroup$
    – yshk
    Commented Jul 20, 2019 at 17:56
9
$\begingroup$

Here is the GitHub repo for my OEIS Mathematica package:

https://github.com/Psychedelic-Geometry/OEIS-Mathematica.git

$\endgroup$
0
6
$\begingroup$

I now have a ServiceConnection for this, to make things a little bit more standardized.

Installation

You can install it like so:

PacletInstall[
 "ServiceConnection_OEIS",
 "Site" ->
  "http://www.wolframcloud.com/objects/b3m2a1.paclets/PacletServer"
 ]

enter image description here

Then connect like:

$so = ServiceConnect["OEIS"]

enter image description here

Search

fibData = $so["Search", "Query" -> Fibonacci /@ Range[25]];
fibData[All, "Description"] // Normal

<|
     "000045" -> "Fibonacci numbers: F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.", 
     "212804" -> "Expansion of (1-x)/(1-x-x^2).", 
     "147316" -> "Fibonacci numbers (A000045) starting at offset -20.",
     "039834" -> "a(n+2) = -a(n+1) + a(n) (signed Fibonacci numbers) with a(-2) = a(-1) = 1; or Fibonacci numbers (A000045) extended to negative indices.", 
     "152163" -> "a(n)=a(n-1)+a(n-2), n>1 ; a(0)=1, a(1)=-1 .", 
     "236191" -> "(-1)^floor( (n-1) / 3 ) * F(n), where F = Fibonacci." 
    |>

$so["Search", "Query" -> {4, 12, 17, 25, 33, 38, 46}][All, "Description"] // Normal

<|
     "134860" -> "Wythoff AAB numbers; also, Fib101 numbers: those n for which the Zeckendorf expansion A014417(n) ends with 1,0,1.", 
     "095099" -> "Duplicate of A134860." 
    |>

SequenceData

fib2 = $so["SequenceData", "ID" -> "000045"];
fib2["Algorithms", "Mathematica"] // Normal

{
     "Table[Fibonacci[k], {k, 0, 50}] (* Mohammad K. Azarian, Jul 11 2015 *)", 
     "Table[2^n Sqrt @ Product[(Cos[Pi k/(n + 1)]^2 + 1/4), {k, n}] // FullSimplify, {n, 15}]; (* Kasteleyn's formula specialized, Sarah-Marie Belcastro (smbelcas(AT)toroidalsnark.net), Jul 04 2009 *)", 
     "LinearRecurrence[{1, 1}, {0, 1}, 40] (* Harvey P. Dale, Aug 03 2014 *)", 
     "Fibonacci[Range[0, 20]] (* Eric W. Weisstein, Sep 22 2017 *)", 
     "CoefficientList[Series[-(x/(-1 + x + x^2)), {x, 0, 20}], x] (* Eric W. Weisstein, Sep 22 2017 *)" 
    }

Sequence

$so["Sequence", "ID" -> "000045"] // Take[#, 100] & // ListPlot

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Very nice. Is there a way to get all results? For example oeis.org/search?q=8.886 has 23 pages, but $so["Search", "Query" -> {8, 8, 8, 6}] only gives the first page. $\endgroup$
    – Greg Hurst
    Commented Mar 19, 2019 at 14:07
3
$\begingroup$

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

$\endgroup$
1
$\begingroup$

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

https://oeis.org/eishelp1.html

One such page is:

https://oeis.org/A000139/internal

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["https://oeis.org/" <> a <> "/internal"], 
     "\n"];
   StringReplace[
    Select[s, StringMatchQ[#, RegularExpression@"^%t.*$"] &], 
    RegularExpression@"^%t" -> ""]
   ];

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

(*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).

$\endgroup$
1
  • $\begingroup$ ?q=id: &fmt=text probably has less overhead: OEISMathematica[a_String] := Module[{s}, s = StringSplit[ Import["https://oeis.org/search?q=id:" <> a <> "&fmt=text", "Text"], "\n"]; StringReplace[ Select[s, StringMatchQ[#, RegularExpression@"^%t.*$"] &], RegularExpression@"^%t A[0-9]* " -> ""] ]; $\endgroup$
    – masterxilo
    Commented Jun 26, 2016 at 0:40
1
$\begingroup$

Here's my GitHub repo for an updated, standalone Mathematica notebook to query the OEIS. It allows the user to specify as many sequence terms as they'd like and return a table of matches with respective formulas and links to the corresponding OEIS pages:

https://github.com/HarlanBrothers/mathematica/find/main

$\endgroup$
0
$\begingroup$

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'"
OEISDataTableFunction["A004001"][n]
OEISDataTableFunction["A000142"][n]
OEISDataTableFunction["A000165"][n]

"doesn't work:"
OEISDataTableFunction["A000140"][n]
OEISDataTableFunction["A000139"][n]

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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