Math::Aronson -- generate values of Aronson's sequence
use Math::Aronson; my $aronson = Math::Aronson->new; print $aronson->next,"\n"; # 1 print $aronson->next,"\n"; # 4 print $aronson->next,"\n"; # 11
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". Those words have 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.
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'); # 1, 7 only
This is "F is the first, seventh" giving 1, 7 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.
Sloane's On-Line Encyclopedia of Integer Sequences has entries for Aronson's sequence and some variations
http://oeis.org/A005224 A005224 without_conjunctions=>1 A055508 letter=>'H', without_conjunctions=>1 A049525 letter=>'I', without_conjunctions=>1 A081023 lying=>1, without_conjunctions=>1 A072886 lying=>1, initial_string=>"S ain't the" A080520 lang=>'fr' A081024 complement of lying A081023 A072887 complement of lying "S ain't" A072886 A072421 Latin P A072422 Latin N A072423 Latin T
The English sequences are without conjunctions, hence for example
# sequence A005224 $it = Math::Aronson->new (without_conjunctions => 1);
The "lying" versions A081023 and A072886 are presumably the same, but the sample values don't go far enough to see a difference.
The sequence is an infinite recurrence (or may be) so is generated in iterator style from an object created with various options.
$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. "en" and "fr" have defaults for the options below. Other languages can be used if you have the Lingua::Any::Numbers
module.
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
.
lying => $bool
(default false)A "lying" version of the sequence, where the positions described and returned are those without the target letter. So for example
T is the second, third, fifth, ... ^^ ^^ ^^^^^^ ^ 2,3, 5,6 7,8,9,10,11,12, 14, ... <-- sequence
Starting from "T is the", the first position is a T so "first" is not appended, but the second position is not a T so lie by giving "second", and similarly the third position, but the fourth is a T so it doesn't appear.
$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) { ... }
Accents are stripped using Unicode::Normalize
if available (Perl 5.8.0 and up), or a built-in Latin-1 table as a fallback otherwise. The Latin-1 suits Lingua::FR::Numbers
and probably most of the European numbers modules.
The Lingua modules and string processing means next
probably isn't particularly fast. It'd be possible to go numbers-only with the usual rules for ordinals as words but generating just the positions of the "T"s or whatever desired letter, but that doesn't seem worth the effort.
Lingua::EN::Numbers, Lingua::FR::Numbers, Lingua::Any::Numbers
http://user42.tuxfamily.org/math-aronson/index.html
Math-Aronson is Copyright 2010, 2011, 2012 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/>.