The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

# NAME

Math::Recaman - Calculate numbers in Recamán's sequence

# SYNOPSIS

``````    use Math::Recaman qw(recaman);

recaman(100);                             # print the first 100 numbers in Recamán's sequence
recaman(1000, sub { push @nums, shift }); # collect the first 1000 numbers into an array
my @array = recaman(10);                  # Returns the first 10 Recaman numbers as an array``````

# DESCRIPTION

Recamán's sequence is a well known sequence defined by a recurrence relation.

It is named after its inventor, Colombian mathematician Bernado Recamán Santos by Neil Sloane, creator of the On-Line Encyclopedia of Integer Sequences (OEIS). The OEIS entry for this sequence is A005132.

The sequence is defined as

``````  aₙ = 0         if n = 0
aₙ = aₙ₋₁ - n  if aₙ₋₁ - n > 0 and is not already in the sequence
aₙ = aₙ₋₁ + n  otherwise``````

It is known to produce quite aethetically pleasing outputs if plotted as an image or as music.

See more:

OEIS

https://oeis.org/A005132

Wikipedia

https://en.wikipedia.org/wiki/Recam%C3%A1n%27s_sequence

# METHODS

## recaman <target> [callback]

Takes a target number to calculate to. If nothing is given the method returns immediately.

By default it prints each number out on a new line.

You can optionally pass in a anonymous subroutine which will be called for each new number in the sequence with the arguments `number`, `count` and `max seen so far`.

If you do not pass an anonymous subroutine and if you call this subroutine expecting an array in return then nothing will be printed.

## recaman_a008336 <target> [callback]

There is another sequence invented by Recamán, less known, defined as:

``````  a₁   = 1
aₙ₊₁ = aₙ/n  if n divides aₙ
aₙ₊₁ = naₙ   otherwise``````

Takes a target number to calculate to. If nothing is given the method returns immediately.

By default it prints each number out on a new line.

You can optionally pass in a anonymous subroutine which will be called for each new number in the sequence with the arguments `number`, `count` and `max seen so far`.

If you do not pass an anonymous subroutine and if you call this subroutine expecting an array in return then nothing will be printed.

# Using Set::IntSpan

If the module Set::IntSpan is installed then that will be used for keeping track of the sequence.

This should make it more efficient for very long sequences.

You can check to see if the module is using Set::IntSpan by checking the variable `\$Math::Recaman::USING_INTSPAN`.

You can disable using the module even if it's installed by setting the variable `\$Math::Recaman::USING_INTSPAN` to `0` before calling the `recaman` function.

# AUTHOR

Simon Wistow, `<simon at thegestalt.org>`

# BUGS

Please report any bugs or feature requests to `bug-math-recaman at rt.cpan.org`, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-Recaman. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

You can also open issues on GitHub at https://github.com/simonwistow/Math-Recaman.

Version 0.01

# SUPPORT

You can find documentation for this module with the perldoc command.

``    perldoc Math::Recaman``

You can also look for information at:

``  The Artistic License 2.0 (GPL Compatible)``