Math::NumSeq::HofstadterFigure -- sequence excludes its own first differences


 use Math::NumSeq::HofstadterFigure;
 my $seq = Math::NumSeq::HofstadterFigure->new;
 my ($i, $value) = $seq->next;


This is Douglas Hofstadter's "figure" sequence which comprises all integers except those which are differences between its own successive values,

    1, 3, 7, 12, 18, 26, 35, 45, 56, 69, 83, 98, ...

So for example at value=1 the next cannot be 2 because the difference 2-1=1 is already in the sequence, so value=3 with difference 3-1=2 is next. Then the next cannot be 4 since 4-3=1 is already in the sequences, and likewise 5-3=2 and 6-3=3, so the next is value=7 with 7-3=4 not already in the sequence.

The effect is that the sequence increments by 1,2,3,4, etc but excluding values of the sequence itself. This makes it close to the Triangular numbers i*(i+1)/2, but incrementing by a little extra at the places it skips its own values.

Start Value

The optional start => $value can give the first value for the sequence, instead of the default 1. For example starting at 2

    2, 3, 7, 12, 18, 26, 35, 45, 56, ...

or starting at 5

    5, 6, 8, 11, 15, 22, 31, 41, 53, 66, ...

The differences are still the values not in the sequence, so for example starting at 5 means the differences are 1, 2, 3, 4 before skipping 5 and 6.

In general the effect is to push the first skip up a bit, but still settles down to grow roughly like the triangular numbers.

The start > must be 1 or more. If the start was 0 then the first value and first difference would always be the same, contradicting the conditions for the sequence.


See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence classes.

$seq = Math::NumSeq::HofstadterFigure->new ()
$seq = Math::NumSeq::HofstadterFigure->new (start => $value)

Create and return a new sequence object.


Math::NumSeq, Math::NumSeq::Kolakoski



Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2019, 2020 Kevin Ryde

Math-NumSeq 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-NumSeq 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-NumSeq. If not, see <>.