NAME

Math::Aronson -- generate values of Aronson's sequence

SYNOPSIS

use Math::Aronson;
my $aronson = Math::Aronson->new;
print $aronson->next,"\n";  # 1
print $aronson->next,"\n";  # 4
print $aronson->next,"\n";  # 11

DESCRIPTION

This is a bit of fun generating Aronson's sequence of numbers formed by self-referential occurrences of the letter T in numbers written out in words.

T is the first, fourth, eleventh, sixteenth, ...
^    ^       ^      ^         ^      ^   ^
1    4      11     16        24     29  33  <-- sequence

In the initial string "T is the", the letter T is the first and fourth letters, so those words are appended to make "T is the first, fourth". In those words there are further Ts at 11 and 16, so those numbers are appended, and so on.

Spaces and punctuation are ignored. Accents like acutes are stripped for letter matching. The without_conjunctions option can ignore "and" or "et" too.

Sloane's OEIS

Sloane's On-Line Encyclopedia of Integer Sequences has Aronson's original sequence in English, plus a French form.

http://www.research.att.com/~njas/sequences/A005224
http://www.research.att.com/~njas/sequences/A080520

The English A005224 is without conjunctions, so is generated with

$it = Math::Aronson->new (without_conjunctions => 1);

But the French A080520 is with them, so just

$it = Math::Aronson->new (lang => 'fr');

Termination

It's possible for the English sequence to end since there's no T in some numbers, but there doesn't seem enough of those, or the sequence doesn't fall on enough of them. (Is that proven?)

But for example using letter "F" instead gives a finite sequence,

$it = Math::Aronson->new (letter => 'F');

This is 1, 7, 12 per "F is the first, seventh" but ends there as there's no more "F"s in "seventh". See examples/terminate.pl in the sources to run thorough which letters seem to terminate or not.

FUNCTIONS

The sequence is an infinite recurrence (or may be) so is generated in iterator style from an object created with various options.

Constructor

$it = Math::Aronson->new (key => value, ...)

Create and return a new Aronson sequence object. The following optional key/value parameters affect the sequence.

lang => $string (default "en")

The language to use for the sequence. This can be anything recognised by Lingua::Any::Numbers. "en" and "fr" have defaults for the options below.

initial_string => $str

The initial string for the sequence. The default is

English    "T is the"
French     "E est la"    

For other languages there's no default yet and an initial_string must be given.

letter => $str

The letter to look for in the words. The default is the first letter of initial_string.

When a letter is given the default initial_string follows that, so "X is the" or "X est la".

$it = Math::Aronson->new (letter => 'H');
# is 1, 5, 16, 25, ...
# per "H is the first, fifth, ..."

letter and initial_string can be given together to use a letter not at the start of the initial_string. For example,

$it = Math::Aronson->new (letter => 'T',
                          initial_string => "I think T is");
# is 2, 7, 21, 23, ...
# per "I think T is second, seventh, twenty-first, ..."
without_conjunctions => $boolean (default false)

Strip conjunctions, meaning "and"s, in the wording so for instance "one hundred and four" becomes "one hundred four". The default is leave unchanged whatever conjunctions Lingua::Any::Numbers (or ordinal_func below) gives.

conjunctions_word => $string (default "and" or "et")

The conjunction word to exclude if without_conjunctions is true. The default is "and" for English or "et" for French. For other languages there's no default.

ordinal_func => $coderef (default Lingua modules)

A function to call to turn a number into words. Each call is

$str = &$ordinal_func ($n);

The default is a call to_ordinal($n,$lang) of Lingua::Any::Numbers, or for English and French a direct call to Lingua::EN::Numbers or Lingua::FR::Numbers. The string returned can be wide chars.

An explicit ordinal_func can be used if Lingua::Any::Numbers doesn't support a desired language, or perhaps for a bit of rewording.

$it = Math::Aronson->new
         (ordinal_func => sub {
            my ($n) = @_;
            return something_made_from($n);
          });

There's nothing to select a gender from Lingua::Any::Numbers, as of version 0.30, so an ordinal_func might be used for instance to get feminine forms from Lingua::ES::Numbers.

Operations

$n = $it->next

Return the next number in the sequence, being the next position of T (or whatever letter) in the text. The first position is 1.

If the end of the sequence has been reached then the return is an empty list (which means undef in scalar context). Because positions begin at 1 a loop can be simply

while (my $n = $it->next) {
  ...
}

OTHER NOTES

Accents are stripped for letter matches using Unicode::Normalize if available, which means Perl 5.8.0 and higher, or a built-in Latin-1 table as a fallback otherwise. That Latin-1 suits Lingua::FR::Numbers and should suit most of the European numbers modules.

The use of the Lingua modules and string crunching means next probably isn't blindingly fast. It'd be possible to go numbers-only with the rules for ordinal words but generating just the positions of the "T"s or whatever desired letter, but that doesn't seem worth the effort.

SEE ALSO

Lingua::Any::Numbers, Lingua::EN::Numbers, Lingua::FR::Numbers

HOME PAGE

http://user42.tuxfamily.org/math-aronson/index.html

LICENSE

Math-Aronson is Copyright 2010 Kevin Ryde

Math-Aronson is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Math-Aronson is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Math-Aronson. If not, see <http://www.gnu.org/licenses/>.