| 1 | # Copyright 2010, 2011, 2012, 2013, 2014, 2016, 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::RepdigitAny; | ||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | $VERSION = 75; | |||||||||||||||||||||||||||
| 24 | 2 | ▶ | use Math::NumSeq 7; # v.7 for _is_infinite() | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | @ISA = ('Math::NumSeq'); | |||||||||||||||||||||||||||
| 26 | 2 | *_is_infinite = \&Math::NumSeq::_is_infinite; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 29 | #use Smart::Comments; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # use constant name => Math::NumSeq::__('Repdigit Any Radix'); | ||||||||||||||||||||||||||||
| 33 | 2 | ▶ | use constant description => Math::NumSeq::__('Numbers which are a "repdigit" like 1111, 222, 999 etc of 3 or more digits in some number base.'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | use constant i_start => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | use constant values_min => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | use constant characteristic_increasing => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | use constant characteristic_integer => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # cf A167783 - length >=2, 2 or more bases | ||||||||||||||||||||||||||||
| 40 | # A053696 - length >=3, 1 or more bases repunit | ||||||||||||||||||||||||||||
| 41 | # | ||||||||||||||||||||||||||||
| 42 | # A158235 - square is a repdigit in some base < i | ||||||||||||||||||||||||||||
| 43 | # A158236 - the radix for those squares | ||||||||||||||||||||||||||||
| 44 | # A158237 - those squares, ie. squares which are repdigits some base | ||||||||||||||||||||||||||||
| 45 | # A158245 - "primitives" in squares seq, meaning square-free | ||||||||||||||||||||||||||||
| 46 | # | ||||||||||||||||||||||||||||
| 47 | 2 | ▶ | use constant oeis_anum => 'A167782'; # length >=3, 1 or more bases | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 4 | ▶ | sub rewind { | ||||||||||||||||||||||||||
|
Subroutine
rewind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 51 | 4 | $self->{'i'} = $self->i_start; | |||||||||||||||||||||||||||
| 52 | 4 | $self->{'done'} = 0; | |||||||||||||||||||||||||||
| 53 | 4 | $self->{'ones'} = [ undef, undef, 7 ]; | |||||||||||||||||||||||||||
| 54 | 4 | $self->{'digits'} = [ undef, undef, 1 ]; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 25 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 25 | my ($self) = @_; | |||||||||||||||||||||||||||
| 59 | ### RepdigitAny next(): $self->{'i'} | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 25 | my $done; | |||||||||||||||||||||||||||
| 62 | 25 | ▶ | if ($done = $self->{'done'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 22 | my $min = $done*$done + 7; | |||||||||||||||||||||||||||
| 64 | 22 | my $ones = $self->{'ones'}; | |||||||||||||||||||||||||||
| 65 | 22 | my $digits = $self->{'digits'}; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 22 | for (my $radix = 2; ; $radix++) { | |||||||||||||||||||||||||||
| 68 | ### $radix | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 86 | my $one; | |||||||||||||||||||||||||||
| 71 | 86 | ▶ | if ($radix > $#$ones) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | ### maybe extend array: $radix | ||||||||||||||||||||||||||||
| 73 | 33 | $one = ($radix + 1) * $radix + 1; | |||||||||||||||||||||||||||
| 74 | 33 | ▶ | if ($one > $min) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | ### stop at big one: $one | ||||||||||||||||||||||||||||
| 76 | 22 | last; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 11 | $ones->[$radix] = $one; | |||||||||||||||||||||||||||
| 79 | 11 | $digits->[$radix] = 1; | |||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||
| 81 | 53 | $one = $ones->[$radix]; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 64 | my $repdigit; | |||||||||||||||||||||||||||
| 85 | 64 | ▶ | while (($repdigit = $one * $digits->[$radix]) <= $done) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | ### increase past done: $repdigit | ||||||||||||||||||||||||||||
| 87 | 20 | ▶ | if (++$digits->[$radix] >= $radix) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 10 | $digits->[$radix] = 1; | |||||||||||||||||||||||||||
| 89 | 10 | $one = $ones->[$radix] = $ones->[$radix] * $radix + 1; | |||||||||||||||||||||||||||
| 90 | ### digit wrap new ones: $ones->[$radix] | ||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||
| 92 | ### digit step: $digits->[$radix] | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | ### consider repdigit: $repdigit | ||||||||||||||||||||||||||||
| 97 | 64 | ▶ | if ($repdigit < $min) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | ### min now: "$repdigit at $radix" | ||||||||||||||||||||||||||||
| 99 | 40 | $min = $repdigit; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | ### result: $min | ||||||||||||||||||||||||||||
| 103 | 22 | $self->{'done'} = $min; | |||||||||||||||||||||||||||
| 104 | 22 | return ($self->{'i'}++, $min); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | } else { | ||||||||||||||||||||||||||||
| 107 | # special case value 0 | ||||||||||||||||||||||||||||
| 108 | 3 | $self->{'done'} = 1; | |||||||||||||||||||||||||||
| 109 | 3 | return ($self->{'i'}++, 0); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 70 | ▶ | sub pred { | ||||||||||||||||||||||||||
|
Subroutine
pred: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 70 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 115 | ### RepdigitAny pred(): $value | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 70 | ▶ | if ($value == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 2 | return 1; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 68 | ▶ | if (_is_infinite($value) || $value != int($value)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
_is_infinite($value) or $value != int $value
MC/DC50%1/2
_is_infinite($value) or $value != int $value
_is_infinite($value)
$value != int $value
Truth table66%2/3
A or B
A= _is_infinite($value)B= $value != int $value
| |||||||||||||||||||||||||||||
| 121 | 31 | return 0; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 37 | RADIX: for (my $radix = 2; ; $radix++) { | |||||||||||||||||||||||||||
| 125 | 86 | my $ones = ($radix + 1) * $radix + 1; | |||||||||||||||||||||||||||
| 126 | 86 | ▶ | if ($ones > $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 25 | return 0; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 61 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 84 | ▶ | if ($ones == $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 10 | return 1; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 74 | foreach my $digit (2 .. $radix-1) { | |||||||||||||||||||||||||||
| 135 | 26 | my $repdigit = $digit * $ones; | |||||||||||||||||||||||||||
| 136 | 26 | ▶ | if ($repdigit == $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 2 | return 1; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 24 | ▶ | if ($repdigit > $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 20 | next RADIX; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 52 | $ones = $ones * $radix + 1; | |||||||||||||||||||||||||||
| 144 | } while ($ones <= $value); | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | 1; | |||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||