| 1 | package Math::Recaman; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Exporter qw/import/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | our $VERSION = '1.6'; | |||||||||||||||||||||||||||
| 10 | 5 | our $USING_INTSPAN; | |||||||||||||||||||||||||||
| 11 | 5 | our @EXPORT_OK=qw(recaman recaman_a008336); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.9
| |||||||||||||||||||||||||||||
| 14 | 5 | eval { require Set::IntSpan }; | |||||||||||||||||||||||||||
| 15 | 5 | $USING_INTSPAN=0; | |||||||||||||||||||||||||||
| 16 | 5 | ▶ | unless ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 0 | Set::IntSpan->import(); | |||||||||||||||||||||||||||
| 18 | 0 | $USING_INTSPAN = 1; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 5 | our @checks = (0, 1, 3, 6, 2, 7, 13, 20, 12, 21, | |||||||||||||||||||||||||||
| 23 | 11, 22, 10, 23, 9, 24, 8, 25, 43, | ||||||||||||||||||||||||||||
| 24 | 62, 42, 63, 41, 18, 42, 17, 43, 16, | ||||||||||||||||||||||||||||
| 25 | 44, 15, 45, 14, 46, 79, 113, 78, 114, | ||||||||||||||||||||||||||||
| 26 | 77, 39, 78, 38, 79, 37, 80, 36, 81, | ||||||||||||||||||||||||||||
| 27 | 35, 82, 34, 83, 33, 84, 32, 85, 31, | ||||||||||||||||||||||||||||
| 28 | 86, 30, 87, 29, 88, 28, 89, 27, 90, | ||||||||||||||||||||||||||||
| 29 | 26, 91, 157, 224, 156, 225, 155); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 5 | our @a008336_checks = (1, 1, 2, 6, 24, 120, 20, 140, 1120, 10080, 1008, 11088, 924, | |||||||||||||||||||||||||||
| 32 | 12012, 858, 12870, 205920, 3500640, 194480, 3695120, | ||||||||||||||||||||||||||||
| 33 | 184756, 3879876, 176358, 4056234, 97349616, 2433740400, | ||||||||||||||||||||||||||||
| 34 | 93605400, 2527345800, 90262350, 2617608150, 87253605, | ||||||||||||||||||||||||||||
| 35 | 2704861755, 86555576160, 2856334013280); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =encoding utf8 | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 NAME | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | Math::Recaman - Calculate numbers in Recamán's sequence | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | use Math::Recaman qw(recaman); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | recaman(100); # print the first 100 numbers in Recamán's sequence | ||||||||||||||||||||||||||||
| 48 | recaman(1000, sub { push @nums, shift }); # collect the first 1000 numbers into an array | ||||||||||||||||||||||||||||
| 49 | my @array = recaman(10); # Returns the first 10 Recaman numbers as an array | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | Recamán's sequence is a well known sequence defined by a recurrence relation. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | It is named after its inventor, Colombian mathematician Bernado Recamán Santos by Neil Sloane, | ||||||||||||||||||||||||||||
| 56 | creator of the On-Line Encyclopedia of Integer Sequences (OEIS). The OEIS entry for this sequence is A005132. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | The sequence is defined as | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | aₙ = 0 if n = 0 | ||||||||||||||||||||||||||||
| 61 | aₙ = aₙ₋₁ - n if aₙ₋₁ - n > 0 and is not already in the sequence | ||||||||||||||||||||||||||||
| 62 | aₙ = aₙ₋₁ + n otherwise | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | It is known to produce quite aethetically pleasing outputs if plotted as an image or as music. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | See more: | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =over 4 | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =item OEIS | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | L<https://oeis.org/A005132> | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =item Wikipedia | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | L<https://en.wikipedia.org/wiki/Recam%C3%A1n%27s_sequence> | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =item Numberphile on YouTube | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | L<https://www.youtube.com/watch?v=FGC5TdIiT9U> | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =back | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 METHODS | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 recaman <target> [callback] | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Takes a target number to calculate to. If nothing is given the method returns immediately. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | By default it prints each number out on a new line. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | You can optionally pass in a anonymous subroutine which will be called for each new number in the sequence | ||||||||||||||||||||||||||||
| 93 | with the arguments C<number>, C<count> and C<max seen so far>. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | If you do not pass an anonymous subroutine and if you call this subroutine expecting an array in return then nothing will be printed. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | ▶ | sub recaman { | ||||||||||||||||||||||||||
|
Subroutine
recaman: called CC=10 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 2 | my @seen; | |||||||||||||||||||||||||||
| 101 | my @values; | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | ▶ | my $target = shift || return; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | my $callback = shift || (wantarray ? sub { push @values, shift } : sub { print $_[0]."\n" }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.5
Subroutine
__ANON__: not called CC=1 SCAR=0.5
Branch: 1 branch
Condition66%2/3
shift() || (wantarray ? sub {
push @values, shift();
}
: sub {
print $_[0] . "\n";
}
)
MC/DC0%0/2
shift() || (wantarray ? sub {
push @values, shift();
}
: sub {
print $_[0] . "\n";
}
)
shift()
(wantarray ? sub {
push @values, shift();
}
: sub {
print $_[0] . "\n";
}
)
Truth table66%2/3
A || B
A= shift()B= (wantarray ? sub {
push @values, shift();
}
: sub {
print $_[0] . "\n";
}
)
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | ▶ | my $increment = sub { $seen[$_[0]]++ }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 107 | 2 | ▶ | my $present = sub { $seen[$_[0]] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | 2 | my $set; | |||||||||||||||||||||||||||
| 109 | 2 | ▶ | if ($USING_INTSPAN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | $set = Set::IntSpan->new; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | $increment = sub { $set->insert($_[0]) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | $present = sub { $set->member($_[0]) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 2 | my $num = 1; | |||||||||||||||||||||||||||
| 116 | 2 | my $pointer = 0; | |||||||||||||||||||||||||||
| 117 | 2 | my $max = 0; | |||||||||||||||||||||||||||
| 118 | 2 | ▶ | while ($num<=$target) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 81 | $increment->($pointer); | |||||||||||||||||||||||||||
| 120 | 81 | ▶ | $callback->($pointer, $num, $max) if defined $callback; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 81 | my $next = $pointer - $num; | |||||||||||||||||||||||||||
| 122 | 81 | ▶ | $next = $pointer+$num if $next<0 || $present->($next); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$next < 0 or &$present($next)
MC/DC100%2/2
$next < 0 or &$present($next)
$next < 0
&$present($next)
Truth table100%3/3
A or B
A= $next < 0B= &$present($next)
| |||||||||||||||||||||||||||||
| 123 | 81 | $pointer = $next; | |||||||||||||||||||||||||||
| 124 | 81 | $num++; | |||||||||||||||||||||||||||
| 125 | 81 | ▶ | $max = $pointer if $pointer>$max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 2 | return @values; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head2 recaman_a008336 <target> [callback] | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | There is another sequence invented by Recamán, less known, defined as: | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | a₁ = 1 | ||||||||||||||||||||||||||||
| 135 | aₙ₊₁ = aₙ/n if n divides aₙ | ||||||||||||||||||||||||||||
| 136 | aₙ₊₁ = naₙ otherwise | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Takes a target number to calculate to. If nothing is given the method returns immediately. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | By default it prints each number out on a new line. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | You can optionally pass in a anonymous subroutine which will be called for each new number in the sequence | ||||||||||||||||||||||||||||
| 143 | with the arguments C<number>, C<count> and C<max seen so far>. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | If you do not pass an anonymous subroutine and if you call this subroutine expecting an array in return then nothing will be printed. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =cut | ||||||||||||||||||||||||||||
| 148 | 2 | ▶ | sub recaman_a008336 { | ||||||||||||||||||||||||||
|
Subroutine
recaman_a008336: called CC=9 SCAR=22.7
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 2 | my @values; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 2 | ▶ | my $target = shift || return 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || (return 0)
MC/DC100%1/1
shift() || (return 0)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my $callback = shift || (wantarray ? sub { push @values, shift } : sub { print $_[0]."\n" }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=3.5
Subroutine
__ANON__: not called CC=1 SCAR=3.5
Branch: 1 branch
Condition33%1/3
shift() || (wantarray ? sub {
push @values, shift();
}
: sub {
print $_[0] . "\n";
}
)
MC/DC0%0/2
shift() || (wantarray ? sub {
push @values, shift();
}
: sub {
print $_[0] . "\n";
}
)
shift()
(wantarray ? sub {
push @values, shift();
}
: sub {
print $_[0] . "\n";
}
)
Truth table33%1/3
A || B
A= shift()B= (wantarray ? sub {
push @values, shift();
}
: sub {
print $_[0] . "\n";
}
)
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 1 | ▶ | return 0 if $target<1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 1 | my $num = 0; | |||||||||||||||||||||||||||
| 157 | 1 | my $pointer = 0; | |||||||||||||||||||||||||||
| 158 | 1 | my $max = 0; | |||||||||||||||||||||||||||
| 159 | 1 | ▶ | while ($num<$target) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 34 | ▶ | if ($num < 2) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 161 | 2 | $pointer = 1; | |||||||||||||||||||||||||||
| 162 | } elsif (($pointer%$num)==0) { | ||||||||||||||||||||||||||||
| 163 | 10 | $pointer = $pointer/$num; | |||||||||||||||||||||||||||
| 164 | } else { | ||||||||||||||||||||||||||||
| 165 | 22 | $pointer = $pointer*$num; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | # print "$num) $pointer\n"; | ||||||||||||||||||||||||||||
| 168 | 34 | $callback->($pointer, $num, $max); | |||||||||||||||||||||||||||
| 169 | 34 | $num++; | |||||||||||||||||||||||||||
| 170 | 34 | ▶ | $max = $pointer if $pointer>$max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 1 | return @values; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 Using Set::IntSpan | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | If the module L<Set::IntSpan> is installed then that will be used for keeping track of the sequence. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | This should make it more efficient for very long sequences. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | You can check to see if the module is using L<Set::IntSpan> by checking the variable C<$Math::Recaman::USING_INTSPAN>. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | You can disable using the module even if it's installed by setting the variable C<$Math::Recaman::USING_INTSPAN> to C<0> before calling the C<recaman> function. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Simon Wistow, C<< <simon at thegestalt.org> >> | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 BUGS | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Please report any bugs or feature requests to C<bug-math-recaman at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 194 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-Recaman>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 195 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | You can also open issues on GitHub at L<https://github.com/simonwistow/Math-Recaman>. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head1 VERSION | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Version 0.01 | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =cut | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | perldoc Math::Recaman | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | You can also look for information at: | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =over 4 | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Math-Recaman> | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | L<https://cpanratings.perl.org/d/Math-Recaman> | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | L<https://metacpan.org/release/Math-Recaman> | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =item * GitHub | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | L<https://github.com/simonwistow/Math-Recaman> | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =back | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | This software is Copyright (c) 2024 by Simon Wistow. | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =cut | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 5 | 1; # End of Math::Recaman | |||||||||||||||||||||||||||