| 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::TotientPerfect; | ||
| 19 | use 5.004; | ||
| 20 | use strict; | ||
| 21 | |||
| 22 | use vars '$VERSION', '@ISA'; | ||
| 23 | $VERSION = 75; | ||
| 24 | use Math::NumSeq; | ||
| 25 | use Math::NumSeq::Base::IteratePred; | ||
| 26 | @ISA = ('Math::NumSeq::Base::IteratePred', | ||
| 27 | 'Math::NumSeq'); | ||
| 28 | *_is_infinite = \&Math::NumSeq::_is_infinite; | ||
| 29 | |||
| 30 | use Math::NumSeq::Totient; | ||
| 31 | *_totient = \&Math::NumSeq::Totient::_totient; | ||
| 32 | |||
| 33 | use Math::NumSeq::PrimeFactorCount;; | ||
| 34 | *_prime_factors = \&Math::NumSeq::PrimeFactorCount::_prime_factors; | ||
| 35 | |||
| 36 | # uncomment this to run the ### lines | ||
| 37 | #use Smart::Comments; | ||
| 38 | |||
| 39 | |||
| 40 | # use constant name => Math::NumSeq::__('Totient Perfect Numbers'); | ||
| 41 | use constant description => Math::NumSeq::__('Numbers for which the sum of repeated applications of the totient function equals N. Eg. 9 is perfect because phi(9)=6, phi(6)=2, phi(2)=1 and the sum 6+2+1 = 9.'); | ||
| 42 | |||
| 43 | use constant values_min => 3; | ||
| 44 | use constant i_start => 1; | ||
| 45 | use constant oeis_anum => 'A082897'; | ||
| 46 | |||
| 47 | |||
| 48 | sub rewind { | ||
| 49 | my ($self) = @_; | ||
| 50 | $self->{'i'} = $self->i_start; | ||
| 51 | $self->{'upto'} = 1; | ||
| 52 | } | ||
| 53 | sub next { | ||
| 54 | my ($self) = @_; | ||
| 55 | |||
| 56 | OUTER: for (;;) { | ||
| 57 | my $value = ($self->{'upto'} += 2); | ||
| 58 | |||
| 59 | my $sum = my $p = _totient($value); | ||
| 60 | while ($p > 1) { | ||
| 61 | $sum += ($p = _totient($p)); | ||
| 62 | if ($sum > $value) { | ||
| 63 | next OUTER; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | if ($sum == $value) { | ||
| 67 | return ($self->{'i'}++, $value); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | sub pred { | ||
| 74 | my ($self, $value) = @_; | ||
| 75 | |||
| 76 | if ($value < $self->values_min | ||
| 77 | || _is_infinite($value) | ||
| 78 | || ($value % 2) == 0) { # even numbers not perfect | ||
| 79 | return 0; | ||
| 80 | } | ||
| 81 | if ($value < 0) { | ||
| 82 | return undef; | ||
| 83 | } | ||
| 84 | |||
| 85 | my ($good, @primes) = _prime_factors($value); | ||
| 86 | return undef unless $good; | ||
| 87 | |||
| 88 | my %primes; | ||
| 89 | foreach my $p (@primes) { | ||
| 90 | $primes{$p}++; | ||
| 91 | } | ||
| 92 | |||
| 93 | my %factors; | ||
| 94 | my $sum = 0; | ||
| 95 | while (%primes) { | ||
| 96 | ### %primes | ||
| 97 | |||
| 98 | my %next; | ||
| 99 | while (my ($p, $e) = each %primes) { | ||
| 100 | if (--$e) { | ||
| 101 | $next{$p} += $e; | ||
| 102 | } | ||
| 103 | my $factors_aref = ($factors{$p} ||= do { | ||
| 104 | my ($good, @primes) = _prime_factors($p-1); | ||
| 105 | return undef unless $good; | ||
| 106 | \@primes | ||
| 107 | }); | ||
| 108 | foreach my $f (@$factors_aref) { | ||
| 109 | $next{$f}++; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | my $next_value = 1; | ||
| 114 | while (my ($p, $e) = each %next) { | ||
| 115 | $next_value *= $p ** $e; | ||
| 116 | } | ||
| 117 | $sum += $next_value; | ||
| 118 | last unless $sum < $value; | ||
| 119 | |||
| 120 | %primes = %next; | ||
| 121 | } | ||
| 122 | ### final sum: $sum | ||
| 123 | |||
| 124 | return ($sum == $value); | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | 1; | ||
| 130 | __END__ |