Recursive sequences
Nick Hobson
nickh at qbyte.org
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
0
>>> # 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)
12
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.
Nick
Recaman-related:
http://www.qbyte.org/oeis/a005132.py.txt
http://www.qbyte.org/oeis/a123483.py.txt
http://www.qbyte.org/oeis/a053461.py.txt
http://www.qbyte.org/oeis/a057165.py.txt
http://www.qbyte.org/oeis/a057166.py.txt
http://www.qbyte.org/oeis/a057167.py.txt
http://www.qbyte.org/oeis/a128204.py.txt
http://www.qbyte.org/oeis/a064389.py.txt
http://www.qbyte.org/oeis/a064289.py.txt
http://www.qbyte.org/oeis/a078758.py.txt
http://www.qbyte.org/oeis/a078759.py.txt
http://www.qbyte.org/oeis/a079051.py.txt
http://www.qbyte.org/oeis/a046901.py.txt
http://www.qbyte.org/oeis/a064387.py.txt
http://www.qbyte.org/oeis/a008336.py.txt
http://www.qbyte.org/oeis/a048991.py.txt
http://www.qbyte.org/oeis/a048992.py.txt
http://www.qbyte.org/oeis/a105391.py.txt
http://www.qbyte.org/oeis/a128333.py.txt
Hofstadter-type sequences:
http://www.qbyte.org/oeis/a005374.py.txt
http://www.qbyte.org/oeis/a005375.py.txt
http://www.qbyte.org/oeis/a005376.py.txt
http://www.qbyte.org/oeis/a005229.py.txt
http://www.qbyte.org/oeis/a005206.py.txt
http://www.qbyte.org/oeis/a005185.py.txt
http://www.qbyte.org/oeis/a004001.py.txt
http://www.qbyte.org/oeis/a070868.py.txt
http://www.qbyte.org/oeis/a070867.py.txt
http://www.qbyte.org/oeis/a070864.py.txt
Tribonacci-related:
http://www.qbyte.org/oeis/a000213.py.txt
http://www.qbyte.org/oeis/a046734.py.txt
Pascal's triangle-related:
http://www.qbyte.org/oeis/a007318.py.txt
http://www.qbyte.org/oeis/a126256.py.txt
http://www.qbyte.org/oeis/a126257.py.txt
Add k, then reverse digits, or similar:
http://www.qbyte.org/oeis/a007396.py.txt
http://www.qbyte.org/oeis/a007397.py.txt
http://www.qbyte.org/oeis/a007398.py.txt
http://www.qbyte.org/oeis/a007399.py.txt
http://www.qbyte.org/oeis/a003608.py.txt
http://www.qbyte.org/oeis/a072452.py.txt
http://www.qbyte.org/oeis/a052008.py.txt
http://www.qbyte.org/oeis/a036447.py.txt
Add k, multiply by k, add k+1, multiply by k+1, and so on:
http://www.qbyte.org/oeis/a082448.py.txt
http://www.qbyte.org/oeis/a019463.py.txt
http://www.qbyte.org/oeis/a019462.py.txt
http://www.qbyte.org/oeis/a019461.py.txt
http://www.qbyte.org/oeis/a019460.py.txt
Sequence-as-string:
http://www.qbyte.org/oeis/a048991.py.txt
http://www.qbyte.org/oeis/a048992.py.txt
http://www.qbyte.org/oeis/a105391.py.txt
http://www.qbyte.org/oeis/a118248.py.txt
http://www.qbyte.org/oeis/a128291.py.txt
Miscellaneous nice:
http://www.qbyte.org/oeis/a011971.py.txt
http://www.qbyte.org/oeis/a002491.py.txt
http://www.qbyte.org/oeis/a001333.py.txt
http://www.qbyte.org/oeis/a000179.py.txt
http://www.qbyte.org/oeis/a000178.py.txt
http://www.qbyte.org/oeis/a000172.py.txt
http://www.qbyte.org/oeis/a000166.py.txt
http://www.qbyte.org/oeis/a000129.py.txt
http://www.qbyte.org/oeis/a000120.py.txt
http://www.qbyte.org/oeis/a000102.py.txt
http://www.qbyte.org/oeis/a000100.py.txt
http://www.qbyte.org/oeis/a005362.py.txt
On Tue, 27 Feb 2007 20:17:47 -0000, Jaap Spies <j.spies at hccnet.nl> 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