| 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::HappyNumbers; | ||||||||||||||||||||||||||||||||||||||||||
| 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 List::Util 'sum'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | $VERSION = 75; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Math::NumSeq; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use Math::NumSeq::Base::IteratePred; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | @ISA = ('Math::NumSeq::Base::IteratePred', | |||||||||||||||||||||||||||||||||||||||||
| 29 | 'Math::NumSeq'); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | *_is_infinite = \&Math::NumSeq::_is_infinite; | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use Math::NumSeq::Repdigits; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | *_digit_split_lowtohigh = \&Math::NumSeq::Repdigits::_digit_split_lowtohigh; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # use Smart::Comments; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | # use constant name => Math::NumSeq::__('Happy Numbers'); | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | use constant description => Math::NumSeq::__('Happy numbers 1,7,10,13,19,23,etc, reaching 1 under iterating sum of squares of digits.'); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use constant default_i_start => 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | use constant values_min => 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | use Math::NumSeq::Base::Digits | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | 'parameter_info_array'; # radix parameter | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # cf A035497 happy primes, happy numbers which are prime | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # A003621 how many steps to reach 1 or 4 | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # A090425 how many steps for just the happy numbers | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # A031176 period of final cycle, being 1 or 8 in decimal | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||||||||||||||||
| 53 | # A000216 start 2 the cycle 4,16,37,58,89,145,42,20 | ||||||||||||||||||||||||||||||||||||||||||
| 54 | # A000218 start 3 | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # A080709 start 4 | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # A000221 start 5 | ||||||||||||||||||||||||||||||||||||||||||
| 57 | # A008460 start 6 | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # A008462 start 8 | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # A008463 start 9 | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # A139566 start 15 | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # A122065 start 74169 | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | my @oeis_anum; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | $oeis_anum[2] = 'A000027'; # 1,2,3,4, everything happy in base 2 | |||||||||||||||||||||||||||||||||||||||||
| 65 | # OEIS-Other: A000027 radix=2 | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | $oeis_anum[4] = 'A000027'; # 1,2,3,4, everything happy in base 4 | |||||||||||||||||||||||||||||||||||||||||
| 68 | # OEIS-Other: A000027 radix=4 | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | $oeis_anum[10] = 'A007770'; | |||||||||||||||||||||||||||||||||||||||||
| 71 | # OEIS-Catalogue: A007770 radix=10 | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | sub oeis_anum { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
oeis_anum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | return $oeis_anum[$self->{'radix'}]; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 97 | ▶ | sub pred { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pred: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 97 | my ($self, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 82 | ### HappyNumbers pred(): $value | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 97 | ▶ | if ($value <= 0 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$value <= 0 or $value != int $value or _is_infinite($value)
$value <= 0 or $value != int $value
MC/DC33%1/3
$value <= 0 or $value != int $value or _is_infinite($value)
$value <= 0
$value != int $value
_is_infinite($value)
Truth table50%2/4
A or B or C
A= $value <= 0B= $value != int $valueC= _is_infinite($value)
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | || $value != int($value) | ||||||||||||||||||||||||||||||||||||||||||
| 85 | || _is_infinite($value)) { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 22 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 75 | my $radix = $self->{'radix'}; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 75 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 75 | for (;;) { | |||||||||||||||||||||||||||||||||||||||||
| 91 | ### $value | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 732 | ▶ | if ($value == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 24 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 708 | ▶ | if ($seen{$value}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 51 | return 0; # inf loop | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 657 | $seen{$value} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 657 | $value = _sum_of_squares_of_digits($value,$radix); | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 657 | ▶ | sub _sum_of_squares_of_digits { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sum_of_squares_of_digits: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 657 | my ($n, $radix) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 657 | return sum (map {$_ * $_} _digit_split_lowtohigh($n,$radix)); | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||||||||||||||||||||||||