| 1 | # Copyright 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::HappySteps; | ||||||||||||||||||||
| 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 | |||||||||||||||||||||
| 22 | 1 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 1 | $VERSION = 75; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 1 | ▶ | use Math::NumSeq; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | 1 | ▶ | use Math::NumSeq::Base::IterateIth; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 1 | @ISA = ('Math::NumSeq::Base::IterateIth', | |||||||||||||||||||
| 28 | 'Math::NumSeq'); | ||||||||||||||||||||
| 29 | 1 | *_is_infinite = \&Math::NumSeq::_is_infinite; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | # uncomment this to run the ### lines | ||||||||||||||||||||
| 32 | #use Devel::Comments; | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # use constant name => Math::NumSeq::__('Happy Steps'); | ||||||||||||||||||||
| 36 | 1 | ▶ | use constant description => Math::NumSeq::__('How many sum of squares of digits steps to get to a repeating iteration.'); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 1 | ▶ | use constant i_start => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 1 | ▶ | use constant values_min => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | 1 | ▶ | use constant characteristic_count => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | 1 | ▶ | use constant characteristic_smaller => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | 1 | ▶ | use constant characteristic_increasing => 0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | use Math::NumSeq::Base::Digits | ||||||||||||||||||||
| 44 | 1 | ▶ | 'parameter_info_array'; # radix parameter | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 47 | # cf A001273 smallest happy which takes N steps | ||||||||||||||||||||
| 48 | # | ||||||||||||||||||||
| 49 | 1 | my @oeis_anum; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 1 | $oeis_anum[2] = 'A078627'; # starting i=1 ... | |||||||||||||||||||
| 52 | # OEIS-Catalogue: A078627 radix=2 | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 1 | $oeis_anum[10] = 'A193995'; # but starting i=1 ... | |||||||||||||||||||
| 55 | # OEIS-Catalogue: A193995 | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 2 | ▶ | sub oeis_anum { | ||||||||||||||||||
|
Subroutine
oeis_anum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 58 | 2 | my ($self) = @_; | |||||||||||||||||||
| 59 | 2 | return $oeis_anum[$self->{'radix'}]; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 187 | ▶ | sub ith { | ||||||||||||||||||
|
Subroutine
ith: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 65 | 187 | my ($self, $i) = @_; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 187 | ▶ | if ($i <= 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 6 | return 0; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | 181 | ▶ | if (_is_infinite($i)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 0 | return $i; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 181 | my $radix = $self->{'radix'}; | |||||||||||||||||||
| 75 | 181 | my $steps = 0; | |||||||||||||||||||
| 76 | 181 | my %seen; | |||||||||||||||||||
| 77 | 181 | for (;;) { | |||||||||||||||||||
| 78 | ### $i | ||||||||||||||||||||
| 79 | 1375 | my $sum = 0; | |||||||||||||||||||
| 80 | 1375 | ▶ | if ($seen{$i}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 181 | return $steps; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | 1194 | $seen{$i} = 1; | |||||||||||||||||||
| 84 | 1194 | ▶ | while ($i) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 2276 | my $digit = ($i % $radix); | |||||||||||||||||||
| 86 | 2276 | $sum += $digit * $digit; | |||||||||||||||||||
| 87 | 2276 | $i = int($i/$radix); | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | 1194 | $i = $sum; | |||||||||||||||||||
| 90 | 1194 | $steps++; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 18 | ▶ | sub pred { | ||||||||||||||||||
|
Subroutine
pred: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||
| 95 | 18 | my ($self, $value) = @_; | |||||||||||||||||||
| 96 | ### HappySteps pred(): $value | ||||||||||||||||||||
| 97 | 18 | ▶ | return ($value >= 0 && $value == int($value)); | ||||||||||||||||||
|
Condition33%1/3
$value >= 0 && $value == int $value
MC/DC0%0/2
$value >= 0 && $value == int $value
$value >= 0
$value == int $value
Truth table33%1/3
A && B
A= $value >= 0B= $value == int $value
| |||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 1 | 1; | |||||||||||||||||||
| 101 | __END__ | ||||||||||||||||||||