| 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::HarshadNumbers; | ||
| 19 | use 5.004; | ||
| 20 | use strict; | ||
| 21 | |||
| 22 | use vars '$VERSION', '@ISA'; | ||
| 23 | $VERSION = 75; | ||
| 24 | |||
| 25 | use Math::NumSeq; | ||
| 26 | use Math::NumSeq::Base::IteratePred; | ||
| 27 | @ISA = ('Math::NumSeq::Base::IteratePred', | ||
| 28 | 'Math::NumSeq'); | ||
| 29 | |||
| 30 | # uncomment this to run the ### lines | ||
| 31 | #use Devel::Comments; | ||
| 32 | |||
| 33 | # use constant name => Math::NumSeq::__('Harshad Numbers'); | ||
| 34 | use constant description => Math::NumSeq::__('Harshad numbers, divisible by the sum of their digits.'); | ||
| 35 | use constant default_i_start => 1; | ||
| 36 | use constant values_min => 1; | ||
| 37 | |||
| 38 | use Math::NumSeq::Base::Digits | ||
| 39 | 'parameter_info_array'; # radix parameter | ||
| 40 | |||
| 41 | #------------------------------------------------------------------------------ | ||
| 42 | |||
| 43 | my @oeis_anum; | ||
| 44 | $oeis_anum[2] = 'A049445'; # binary 1s divide N OFFSET=1 | ||
| 45 | $oeis_anum[3] = 'A064150'; # base 3 OFFSET=0 | ||
| 46 | $oeis_anum[4] = 'A064438'; # base 4 OFFSET=1 | ||
| 47 | $oeis_anum[5] = 'A064481'; # base 5 OFFSET=1 | ||
| 48 | $oeis_anum[10] = 'A005349'; # decimal sum digits divide N OFFSET=1 | ||
| 49 | # OEIS-Catalogue: A049445 radix=2 | ||
| 50 | # OEIS-Catalogue: A064150 radix=3 i_start=0 | ||
| 51 | # OEIS-Catalogue: A064438 radix=4 | ||
| 52 | # OEIS-Catalogue: A064481 radix=5 | ||
| 53 | # OEIS-Catalogue: A005349 | ||
| 54 | |||
| 55 | sub oeis_anum { | ||
| 56 | my ($self) = @_; | ||
| 57 | return $oeis_anum[$self->{'radix'}]; | ||
| 58 | } | ||
| 59 | |||
| 60 | #------------------------------------------------------------------------------ | ||
| 61 | |||
| 62 | sub pred { | ||
| 63 | my ($self, $value) = @_; | ||
| 64 | ### HarshadNumbers pred(): $value | ||
| 65 | if ($value <= 0) { | ||
| 66 | return 0; | ||
| 67 | } | ||
| 68 | my $radix = $self->{'radix'}; | ||
| 69 | my $sum = 0; | ||
| 70 | my $v = $value; | ||
| 71 | while ($v) { | ||
| 72 | $sum += ($v % $radix); | ||
| 73 | $v = int($v/$radix); | ||
| 74 | } | ||
| 75 | return ($sum > 0 && ($value % $sum) == 0); | ||
| 76 | } | ||
| 77 | # sub ith { | ||
| 78 | # my ($self, $i) = @_; | ||
| 79 | # return ... | ||
| 80 | # } | ||
| 81 | |||
| 82 | 1; | ||
| 83 | __END__ |