Recursive sequences

Nick Hobson nickh at
Fri Mar 2 01:47:09 CET 2007

I've started a section on my website where I give Python scripts for a  
number of OEIS sequences.  So far I've concentrated on sequences for which  
a generator function is the best means of production, which includes most  
recursive sequences.  I only have about 60 scripts so far, but I'll be  
adding more over the next few weeks.  The scripts don't use any  
SAGE-related functions; they're pure Python.  The aim is to showcase  
Python and comment on some of the sequences.

Each sequence is packaged as a Python module, designed to be imported  
using "from x import *".  To prevent "namespace pollution" (where names  
are imported which clash with those of the client), a short list of names  
to be exported is specified.  I've not gone with a class for each sequence  
because I can see little use for multiple instances of a sequence object.

Each module provides the following functions and data, illustrated for  
A000178.  Some modules provide additional functions.

>>> from a000178 import *
>>> # List the first seven terms in the sequence:
>>> print a000178_list(7)
[1, 1, 2, 12, 288, 34560, 24883200]
>>> # Print the offset for this sequence:
>>> print a000178_offset
>>> # List the first six terms, each as a tuple of the form (n, a(n)), #  
>>> respecting offset:
>>> for x in a000178_list_pairs(6):
...     print x
(0, 1)
(1, 1)
(2, 2)
(3, 12)
(4, 288)
(5, 34560)
>>> # Defined for increasing sequences only, list terms up to and #  
>>> including the specified limit:
>>> a000178_list_upto(10**6)
[1, 1, 2, 12, 288, 34560]
>>> # Print the term with index 3, respecting offset:
>>> print a000178(3)

For the recursive sequences I've started with, most of the program logic  
is in the generator function, which is written to yield a potentially  
infinite number of terms.  Exported functions such as a000178_list and  
a000178 return a finite subset of these terms.

I plan to write some background on the OEIS and commentary on the  
scripts.  I've not yet had chance to do that, but links to the scripts  
themselves are given below.  Any comments would be welcome.



Hofstadter-type sequences:


Pascal's triangle-related:

Add k, then reverse digits, or similar:

Add k, multiply by k, add k+1, multiply by k+1, and so on:


Miscellaneous nice:

On Tue, 27 Feb 2007 20:17:47 -0000, Jaap Spies <j.spies at> wrote:

> Tanya Khovanova wrote:
>>  Thank you! I would like to expand my webpage. Unfortunately for me and  
>> fortunately for humanity OEIS has too many sequences. :-) For the page  
>> I did, I had to check and copy manually more than 1,000 sequences.  It  
>> was somewhat discouraging (or maybe encouraging to become a better  
>> programmer) :-).
> I know what you mean. We can only scratch the surface. From the "core"  
> sequences I
> implemented only a percentage in SAGE. So I repeat: feel free to  
> contribute and
> become a better programmer :-)! All of you.
> Jaap

More information about the SeqFan mailing list