| 1 | # Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2017, 2019, 2020 Kevin Ryde | ||||||||||
| 2 | |||||||||||
| 3 | # This file is part of Math-NumSeq. | ||||||||||
| 4 | # | ||||||||||
| 5 | # Math-NumSeq is free software; you can redistribute it and/or modify | ||||||||||
| 6 | # it under the terms of the GNU General Public License as published by the | ||||||||||
| 7 | # Free Software Foundation; either version 3, or (at your option) any later | ||||||||||
| 8 | # version. | ||||||||||
| 9 | # | ||||||||||
| 10 | # Math-NumSeq is distributed in the hope that it will be useful, but | ||||||||||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||||||||||
| 13 | # for more details. | ||||||||||
| 14 | # | ||||||||||
| 15 | # You should have received a copy of the GNU General Public License along | ||||||||||
| 16 | # with Math-NumSeq. If not, see <http://www.gnu.org/licenses/>. | ||||||||||
| 17 | |||||||||||
| 18 | package Math::NumSeq::StarNumbers; | ||||||||||
| 19 | 1 | ▶ | use 5.004; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 1 | ▶ | use POSIX 'ceil'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1 | ▶ | use List::Util 'max'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 1 | ▶ | use vars '$VERSION','@ISA'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 1 | $VERSION = 75; | |||||||||
| 26 | |||||||||||
| 27 | 1 | ▶ | use Math::NumSeq; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 1 | ▶ | use Math::NumSeq::Base::IterateIth; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 1 | @ISA = ('Math::NumSeq::Base::IterateIth', | |||||||||
| 30 | 'Math::NumSeq'); | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | # use constant name => Math::NumSeq::__('Star Numbers'); | ||||||||||
| 34 | 1 | ▶ | use constant description => Math::NumSeq::__('The star numbers 1, 13, 37, 73, 121, etc, 6*n*(n-1)+1, also called the centred 12-gonals.'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 1 | ▶ | use constant default_i_start => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 1 | ▶ | use constant values_min => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 1 | ▶ | use constant characteristic_increasing => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 1 | ▶ | use constant characteristic_integer => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | |||||||||||
| 40 | # cf | ||||||||||
| 41 | # A006060 - which are also triangular numbers | ||||||||||
| 42 | # A068774 - indices of the triangulars | ||||||||||
| 43 | # A068775 - indices of the stars | ||||||||||
| 44 | # A006061 - which are also perfect squares | ||||||||||
| 45 | # A054320 - indices of the squares | ||||||||||
| 46 | # A068778 - indices of the stars | ||||||||||
| 47 | # | ||||||||||
| 48 | |||||||||||
| 49 | # centered polygonal numbers (k*n^2-k*n+2)/2, for k = 3 through 14 sides: | ||||||||||
| 50 | # A005448 , A001844 , A005891 , A003215 , A069099 , A016754 , A060544 , | ||||||||||
| 51 | # A062786 , A069125 , A003154 , A069126 , A069127 | ||||||||||
| 52 | # | ||||||||||
| 53 | # centered polygonal numbers (k*n^2-k*n+2)/2, for k = 15 through 20 sides: | ||||||||||
| 54 | # A069128 , A069129 , A069130 , A069131 , A069132 , A069133 | ||||||||||
| 55 | # | ||||||||||
| 56 | 1 | ▶ | use constant oeis_anum => 'A003154'; # star numbers | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub _UNTESTED__seek_to_value { | ||||||||
|
Subroutine
_UNTESTED__seek_to_value: not called CC=1 SCAR=6.9
| |||||||||||
| 59 | 0 | my ($self, $value) = @_; | |||||||||
| 60 | 0 | $self->{'i'} = $self->value_to_i_ceil($value); | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 183 | ▶ | sub ith { | ||||||||
|
Subroutine
ith: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 64 | 183 | my ($self, $i) = @_; | |||||||||
| 65 | 183 | return 6*$i*($i-1)+1; | |||||||||
| 66 | } | ||||||||||
| 67 | 246 | ▶ | sub pred { | ||||||||
|
Subroutine
pred: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 68 | 246 | my ($self, $value) = @_; | |||||||||
| 69 | 246 | ▶ | if ($value >= 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 246 | my $int = int($value); | |||||||||
| 71 | 246 | ▶ | if ($value == $int) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 126 | my $i = _inverse($int); | |||||||||
| 73 | 126 | return ($int == $self->ith($i)); | |||||||||
| 74 | } | ||||||||||
| 75 | } | ||||||||||
| 76 | 120 | return 0; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 28 | ▶ | sub value_to_i { | ||||||||
|
Subroutine
value_to_i: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 80 | 28 | my ($self, $value) = @_; | |||||||||
| 81 | 28 | ▶ | if ($value >= 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 25 | my $int = int($value); | |||||||||
| 83 | 25 | ▶ | if ($value == $int) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 14 | my $i = int(_inverse($int)); | |||||||||
| 85 | 14 | ▶ | if ($int == $self->ith($i)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 10 | return $i; | |||||||||
| 87 | } | ||||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | 18 | return undef; | |||||||||
| 91 | } | ||||||||||
| 92 | 47 | ▶ | sub value_to_i_floor { | ||||||||
|
Subroutine
value_to_i_floor: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 93 | 47 | my ($self, $value) = @_; | |||||||||
| 94 | 47 | ▶ | if ($value < 0) { return 0; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 38 | return int(_inverse(int($value))); | |||||||||
| 96 | } | ||||||||||
| 97 | 1 | *value_to_i_estimate = \&value_to_i_floor; | |||||||||
| 98 | |||||||||||
| 99 | # i = 1/2 + sqrt(1/6 * $n + 1/12) | ||||||||||
| 100 | # = (3 + sqrt(6 * $n + 3)) / 6 | ||||||||||
| 101 | # | ||||||||||
| 102 | 178 | ▶ | sub _inverse { | ||||||||
|
Subroutine
_inverse: called CC=1 SCAR=0.0
| |||||||||||
| 103 | 178 | my ($int) = @_; | |||||||||
| 104 | 178 | return int ((sqrt(6*$int + 3) + 3) / 6); | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 1 | 1; | |||||||||
| 108 | __END__ | ||||||||||