Math-NumSeq

 view release on metacpan or  search on metacpan

lib/Math/NumSeq/SpiroFibonacci.pm  view on Meta::CPAN

#   $self->{'cache'}->[$orig_i] = $total;
#
#   ### $total
#   return $total;
# }

1;
__END__

=for stopwords Ryde Math-NumSeq BigInt spiro-Fibonacci

=head1 NAME

Math::NumSeq::SpiroFibonacci -- recurrence around a square spiral

=head1 SYNOPSIS

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

=head1 DESCRIPTION

This is the spiro-Fibonacci numbers by Neil Fernandez.  The sequence is a
recurrence

    SF[0] = 0
    SF[1] = 1
    SF[i] = SF[i-1] + SF[i-k]

where the offset k is the closest point on the on the preceding loop of a
square spiral.  The initial values are

    0, 1, 1, ..., 1, 2, 3, 4, ... 61, 69, 78, 88, 98, 108, ...
    starting i=0

On the square spiral this is

     98-88-78-69-61-54-48
      |                 |
    108 10--9--8--7--6 42
      |  |           |  |
        11  1--1--1  5 36
         |  |     |  |  |
        12  1  0--1  4 31
         |  |        |  |
        13  1--1--2--3 27
         |              |
        14-15-16-18-21-24

Value 36 on the right is 31+5, being the immediately preceding 31 and the
value on the next inward loop closest to that new 36 position.

At the corners the same inner value is used three times, so for example
42=36+6, then 48=42+6 and 54=48+6, all using the corner "6".  For the
innermost loop SF[2] through SF[7] the "0" at the origin is the inner value,
hence the run of seven 1s at the start.

=head2 Absolute Differences

Optional C<recurrence_type =E<gt> 'absdiff'> changes the recurrence formula
to an absolute difference

    SF[i] = abs (SF[i-1] - SF[i-k])

With the default initial values SF[0]=0 and SF[1]=1 this behaves as an XOR,
always giving 0 or 1.

    0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, ...

The result plotted around the square spiral is similar to some of the
cellular automaton patterns which work on xor feedback.

    *** *    *  **       *     **  **      * * * *  * * **
    * *  *****   *        *** * * * *      ******** *****
     **   * * ****         * ********      *       **    *
      *    **  * *          **      *      **      * *   *
    ***     *   **           *     **      * *     ****  *
     * ******    *            *** * *      ****    *   * *
      ** * * *****             * ***       *   *   **  ***
    *** **  ** * *              **o**      **  **  * * *
    * *  *   *  **               * * *     * * * * ******
     **   ****   *              *  * **    *********     *
    ** **** * ****              ***    *   *        *    *
    * ** * **  * *              * *******  **       **   *
     ** **  *   **              ** * * * * * *      * *  *
    ** ** ***    *              *  **  * ******     **** *
     *  *  * *****              *** ***        *    *   **

=head2 Initial Values

Optional C<initial_0> and C<initial_1> can give different initial i=0 and i=1
values.  For example C<initial_0=E<gt>1, initial_1=E<gt>0> gives

    1, 0, 1, 2, 3, 4, 5, 6, 6, 6, 6, 7, 8, 9, 11, 14, 17, 20, ...

=head1 FUNCTIONS

See L<Math::NumSeq/FUNCTIONS> for behaviour common to all sequence classes.

=over 4

=item C<$seq = Math::NumSeq::SpiroFibonacci-E<gt>new ()>

Create and return a new sequence object.

=item C<($i, $value) = $seq-E<gt>next()>

Return the next index and value in the sequence.

When C<$value> exceeds the range of a Perl unsigned integer the return is
promoted to a C<Math::BigInt> to keep full precision.

=back

=head1 SEE ALSO

L<Math::NumSeq>,
L<Math::NumSeq::Fibonacci>

L<Math::PlanePath::SquareSpiral>



( run in 3.146 seconds using v1.01-cache-2.11-cpan-5b529ec07f3 )