| 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::Multiples; | ||||||||||
| 19 | 2 | ▶ | use 5.004; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use POSIX 'ceil'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 2 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 2 | $VERSION = 75; | |||||||||
| 25 | 2 | ▶ | use Math::NumSeq; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 2 | @ISA = ('Math::NumSeq'); | |||||||||
| 27 | |||||||||||
| 28 | |||||||||||
| 29 | # use constant name => Math::NumSeq::__('Multiples of a given K'); | ||||||||||
| 30 | 2 | ▶ | use constant description => Math::NumSeq::__('The multiples K, 2*K, 3*K, 4*K, etc of a given number.'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 2 | ▶ | use constant default_i_start => 0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | 2 | ▶ | sub values_min { | ||||||||
|
Subroutine
values_min: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 34 | 2 | my ($self) = @_; | |||||||||
| 35 | 2 | ▶ | if ((my $multiples = $self->{'multiples'}) >= 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 2 | return $multiples * $self->i_start; | |||||||||
| 37 | } else { | ||||||||||
| 38 | 0 | return undef; | |||||||||
| 39 | } | ||||||||||
| 40 | } | ||||||||||
| 41 | 0 | ▶ | sub values_max { | ||||||||
|
Subroutine
values_max: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 42 | 0 | my ($self) = @_; | |||||||||
| 43 | 0 | ▶ | if ((my $multiples = $self->{'multiples'}) <= 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | return $multiples * $self->i_start; | |||||||||
| 45 | } else { | ||||||||||
| 46 | 0 | return undef; | |||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | 2 | ▶ | sub characteristic_non_decreasing { | ||||||||
|
Subroutine
characteristic_non_decreasing: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 50 | 2 | my ($self) = @_; | |||||||||
| 51 | 2 | return ($self->{'multiples'} >= 0); | |||||||||
| 52 | } | ||||||||||
| 53 | 4 | ▶ | sub characteristic_increasing { | ||||||||
|
Subroutine
characteristic_increasing: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 54 | 4 | my ($self) = @_; | |||||||||
| 55 | 4 | return ($self->{'multiples'} > 0); | |||||||||
| 56 | } | ||||||||||
| 57 | 3 | ▶ | sub characteristic_integer { | ||||||||
|
Subroutine
characteristic_integer: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 58 | 3 | my ($self) = @_; | |||||||||
| 59 | 3 | return (int($self->{'multiples'}) == $self->{'multiples'}); | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 2 | ▶ | use constant parameter_info_array => | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 63 | [ { name => 'multiples', | ||||||||||
| 64 | type => 'float', | ||||||||||
| 65 | width => 10, | ||||||||||
| 66 | decimals => 4, | ||||||||||
| 67 | page_increment => 10, | ||||||||||
| 68 | step_increment => 1, | ||||||||||
| 69 | minimum => 0, | ||||||||||
| 70 | default => 29, | ||||||||||
| 71 | description => Math::NumSeq::__('Display multiples of this number. For example 6 means show 6,12,18,24,30,etc.'), | ||||||||||
| 72 | }, | ||||||||||
| 73 | 2 | ]; | |||||||||
| 74 | |||||||||||
| 75 | #------------------------------------------------------------------------------ | ||||||||||
| 76 | |||||||||||
| 77 | # cf A017173 9n+1 | ||||||||||
| 78 | 2 | my %oeis_anum = | |||||||||
| 79 | (0 => { 0 => 'A000004', # 0, all zeros | ||||||||||
| 80 | # OEIS-Catalogue: A000004 multiples=0 | ||||||||||
| 81 | |||||||||||
| 82 | 1 => 'A001477', # 1, integers 0,1,2,... | ||||||||||
| 83 | 2 => 'A005843', # 2 even 0,2,4,... | ||||||||||
| 84 | # OEIS-Other: A001477 multiples=1 | ||||||||||
| 85 | # OEIS-Other: A005843 multiples=2 | ||||||||||
| 86 | |||||||||||
| 87 | 3 => 'A008585', # 3 starting from i=0 | ||||||||||
| 88 | 4 => 'A008586', # 4 starting from i=0 | ||||||||||
| 89 | 5 => 'A008587', # 5 starting from i=0 | ||||||||||
| 90 | 6 => 'A008588', # 6 starting from i=0 | ||||||||||
| 91 | 7 => 'A008589', # 7 starting from i=0 | ||||||||||
| 92 | 8 => 'A008590', # 8 starting from i=0 | ||||||||||
| 93 | 9 => 'A008591', # 9 starting from i=0 | ||||||||||
| 94 | 10 => 'A008592', # 10 starting from i=0 | ||||||||||
| 95 | # OEIS-Catalogue: A008585 multiples=3 | ||||||||||
| 96 | # OEIS-Catalogue: A008586 multiples=4 | ||||||||||
| 97 | # OEIS-Catalogue: A008587 multiples=5 | ||||||||||
| 98 | # OEIS-Catalogue: A008588 multiples=6 | ||||||||||
| 99 | # OEIS-Catalogue: A008589 multiples=7 | ||||||||||
| 100 | # OEIS-Catalogue: A008590 multiples=8 | ||||||||||
| 101 | # OEIS-Catalogue: A008591 multiples=9 | ||||||||||
| 102 | # OEIS-Catalogue: A008592 multiples=10 | ||||||||||
| 103 | }, | ||||||||||
| 104 | 1 => { | ||||||||||
| 105 | 3018 => 'A086746', # multiples of 3018, start OFFSET=1 value=3018 | ||||||||||
| 106 | # OEIS-Catalogue: A086746 multiples=3018 i_start=1 | ||||||||||
| 107 | }, | ||||||||||
| 108 | ); | ||||||||||
| 109 | 2 | ▶ | sub oeis_anum { | ||||||||
|
Subroutine
oeis_anum: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||
| 110 | 2 | my ($self) = @_; | |||||||||
| 111 | 2 | my $i_start = $self->i_start; | |||||||||
| 112 | 0 | ▶ | if ($i_start < 0) { return undef; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 2 | return $oeis_anum{$i_start}->{$self->{'multiples'}}; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | #------------------------------------------------------------------------------ | ||||||||||
| 117 | |||||||||||
| 118 | 12 | ▶ | sub rewind { | ||||||||
|
Subroutine
rewind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 119 | 12 | my ($self) = @_; | |||||||||
| 120 | 12 | $self->{'i'} = $self->i_start; | |||||||||
| 121 | } | ||||||||||
| 122 | 14 | ▶ | sub seek_to_i { | ||||||||
|
Subroutine
seek_to_i: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 123 | 14 | my ($self, $i) = @_; | |||||||||
| 124 | # if ($i >= $self->{'uv_i_limit'}) { | ||||||||||
| 125 | # $i = Math::NumSeq::_to_bigint($i); | ||||||||||
| 126 | # } | ||||||||||
| 127 | 14 | $self->{'i'} = $i; | |||||||||
| 128 | } | ||||||||||
| 129 | 14 | ▶ | sub seek_to_value { | ||||||||
|
Subroutine
seek_to_value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 130 | 14 | my ($self, $value) = @_; | |||||||||
| 131 | 14 | $self->{'i'} = $self->value_to_i_ceil($value); | |||||||||
| 132 | } | ||||||||||
| 133 | 98 | ▶ | sub next { | ||||||||
|
Subroutine
next: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 134 | 98 | my ($self) = @_; | |||||||||
| 135 | 98 | my $i = $self->{'i'}++; | |||||||||
| 136 | 98 | return ($i, $i * $self->{'multiples'}); | |||||||||
| 137 | } | ||||||||||
| 138 | 218 | ▶ | sub ith { | ||||||||
|
Subroutine
ith: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 139 | 218 | my ($self, $i) = @_; | |||||||||
| 140 | 218 | return $i * $self->{'multiples'}; | |||||||||
| 141 | } | ||||||||||
| 142 | 484 | ▶ | sub pred { | ||||||||
|
Subroutine
pred: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 143 | 484 | my ($self, $value) = @_; | |||||||||
| 144 | 484 | my $multiples = $self->{'multiples'}; | |||||||||
| 145 | 484 | ▶ | if ($multiples == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | 0 | return ($value == 0); | |||||||||
| 147 | } | ||||||||||
| 148 | 484 | my $i = int($value / $multiples); | |||||||||
| 149 | 484 | return ($value == $i*$multiples); | |||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | 2 | ▶ | use constant::defer _INFINITY => sub { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 153 | 1 | require POSIX; | |||||||||
| 154 | 1 | return 2 * POSIX::DBL_MAX(); | |||||||||
| 155 | 2 | }; | |||||||||
| 156 | |||||||||||
| 157 | 34 | ▶ | sub value_to_i_estimate { | ||||||||
|
Subroutine
value_to_i_estimate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 158 | 34 | my ($self, $value) = @_; | |||||||||
| 159 | 34 | my $multiples = $self->{'multiples'}; | |||||||||
| 160 | 34 | ▶ | if ($multiples == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | 1 | return _INFINITY; | |||||||||
| 162 | } | ||||||||||
| 163 | 33 | return int($value / $multiples); | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | 2 | ▶ | use Math::NumSeq::All; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 167 | 2 | *_floor = \&Math::NumSeq::All::_floor; | |||||||||
| 168 | |||||||||||
| 169 | 76 | ▶ | sub value_to_i { | ||||||||
|
Subroutine
value_to_i: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 170 | 76 | my ($self, $value) = @_; | |||||||||
| 171 | 76 | my $i = $self->value_to_i_floor($value); | |||||||||
| 172 | 76 | ▶ | if ($value == $self->ith($i)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 29 | return $i; | |||||||||
| 174 | } | ||||||||||
| 175 | 47 | return undef; | |||||||||
| 176 | } | ||||||||||
| 177 | 277 | ▶ | sub value_to_i_floor { | ||||||||
|
Subroutine
value_to_i_floor: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 178 | 277 | my ($self, $value) = @_; | |||||||||
| 179 | 277 | return _floor($value/$self->{'multiples'}); | |||||||||
| 180 | } | ||||||||||
| 181 | 94 | ▶ | sub value_to_i_ceil { | ||||||||
|
Subroutine
value_to_i_ceil: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 182 | 94 | my ($self, $value) = @_; | |||||||||
| 183 | 0 | ▶ | if ($value < 0) { return 0; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 184 | 94 | my $i = $self->value_to_i_floor($value); | |||||||||
| 185 | 94 | ▶ | if ($value > $self->ith($i)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 186 | 24 | $i += 1; | |||||||||
| 187 | } | ||||||||||
| 188 | 94 | return $i; | |||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | |||||||||||
| 192 | 2 | 1; | |||||||||
| 193 | __END__ | ||||||||||