| 1 | # Copyright 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 | |||
| 19 | # period(period(...period(m))) | ||
| 20 | # p^(w+1)[m]=p^w[m] at w=Fibonacci frequency, for w>=1 | ||
| 21 | # A001178 Fibonacci frequency of n. | ||
| 22 | # | ||
| 23 | |||
| 24 | |||
| 25 | package Math::NumSeq::PisanoPeriodSteps; | ||
| 26 | use 5.004; | ||
| 27 | use strict; | ||
| 28 | |||
| 29 | use vars '$VERSION', '@ISA'; | ||
| 30 | $VERSION = 75; | ||
| 31 | |||
| 32 | use Math::NumSeq; | ||
| 33 | use Math::NumSeq::Base::IterateIth; | ||
| 34 | @ISA = ('Math::NumSeq::Base::IterateIth', | ||
| 35 | 'Math::NumSeq'); | ||
| 36 | *_is_infinite = \&Math::NumSeq::_is_infinite; | ||
| 37 | |||
| 38 | use Math::NumSeq::Base::Cache | ||
| 39 | 'cache_hash'; | ||
| 40 | |||
| 41 | use Math::NumSeq::NumAronson 8; # new in v.8 | ||
| 42 | *_round_down_pow = \&Math::NumSeq::NumAronson::_round_down_pow; | ||
| 43 | |||
| 44 | use Math::NumSeq::PisanoPeriod; | ||
| 45 | |||
| 46 | # uncomment this to run the ### lines | ||
| 47 | #use Smart::Comments; | ||
| 48 | |||
| 49 | |||
| 50 | # use constant name => Math::NumSeq::__('...'); | ||
| 51 | use constant i_start => 1; | ||
| 52 | use constant characteristic_smaller => 1; | ||
| 53 | use constant characteristic_integer => 1; | ||
| 54 | use constant values_min => 0; | ||
| 55 | |||
| 56 | use constant parameter_info_array => | ||
| 57 | [ { name => 'values_type', | ||
| 58 | display => Math::NumSeq::__('Values Type'), | ||
| 59 | type => 'enum', | ||
| 60 | default => 'freq', | ||
| 61 | choices => ['freq', | ||
| 62 | 'log'], | ||
| 63 | choices_display => [Math::NumSeq::__('Freq'), | ||
| 64 | Math::NumSeq::__('Log')], | ||
| 65 | description => Math::NumSeq::__('The "frequency" count of steps, or the "logarithm" power in the final repeating period.'), | ||
| 66 | }, | ||
| 67 | ]; | ||
| 68 | |||
| 69 | sub description { | ||
| 70 | my ($self) = @_; | ||
| 71 | if (ref $self && $self->{'values_type'} eq 'log') { | ||
| 72 | return Math::NumSeq::__('Leonardo logarithm, the "l" exponent in the final period 24*5^(l-1) on reaching an unchanging PisanoPeriod.'); | ||
| 73 | } else { | ||
| 74 | return Math::NumSeq::__('Fibonacci frequency, how many applications of the PisanoPeriod to reach an unchanging value.') | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | sub characteristic_count { | ||
| 79 | my ($self) = @_; | ||
| 80 | return ($self->{'values_type'} eq 'freq'); | ||
| 81 | } | ||
| 82 | |||
| 83 | #------------------------------------------------------------------------------ | ||
| 84 | |||
| 85 | my %oeis_anum = (freq => 'A001178', | ||
| 86 | log => 'A001179', | ||
| 87 | # OEIS-Catalogue: A001178 | ||
| 88 | # OEIS-Catalogue: A001179 values_type=log | ||
| 89 | ); | ||
| 90 | sub oeis_anum { | ||
| 91 | my ($self) = @_; | ||
| 92 | ### oeis_anum(): $self | ||
| 93 | return $oeis_anum{$self->{'values_type'}}; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | #------------------------------------------------------------------------------ | ||
| 98 | |||
| 99 | sub ith { | ||
| 100 | my ($self, $i) = @_; | ||
| 101 | ### PisanoPeriodSteps ith(): $i | ||
| 102 | |||
| 103 | if (_is_infinite($i)) { | ||
| 104 | return $i; | ||
| 105 | } | ||
| 106 | if ($i <= 1) { | ||
| 107 | if ($i < 1) { | ||
| 108 | return undef; | ||
| 109 | } | ||
| 110 | return 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | my $key = "PisanoPeriodSteps:" . $self->{'values_type'}; | ||
| 114 | |||
| 115 | my $value = cache_hash()->{$key.$i}; | ||
| 116 | if (! defined $value) { | ||
| 117 | ### calculate ... | ||
| 118 | |||
| 119 | my $count = -1; # default undef for outside range | ||
| 120 | my $log = -1; | ||
| 121 | |||
| 122 | my @pending = ($i); | ||
| 123 | if (defined ($i = $self->Math::NumSeq::PisanoPeriod::ith($i))) { | ||
| 124 | for (;;) { | ||
| 125 | ### at: "i=$i" | ||
| 126 | |||
| 127 | my $p = $self->Math::NumSeq::PisanoPeriod::ith($i); | ||
| 128 | if (! defined $p) { | ||
| 129 | ### outside range of PisanoPeriod ... | ||
| 130 | last; | ||
| 131 | } | ||
| 132 | if ($p == $i) { | ||
| 133 | ### same: "i=$i p=$p" | ||
| 134 | $count = 0; | ||
| 135 | |||
| 136 | # $i is the final period, turn it into the logarithm | ||
| 137 | $i /= 24; | ||
| 138 | ($i, $log) = _round_down_pow ($i, 5); | ||
| 139 | $log++; | ||
| 140 | |||
| 141 | last; | ||
| 142 | } | ||
| 143 | ### not same: "i=$i p=$p" | ||
| 144 | |||
| 145 | if (defined ($count = cache_hash()->{"PisanoPeriodSteps:freq:".$i})) { | ||
| 146 | $log = cache_hash()->{"PisanoPeriodSteps:log:".$i}; | ||
| 147 | ### found cache: "i=$i count=$count log=$log" | ||
| 148 | last; | ||
| 149 | } | ||
| 150 | push @pending, $i; | ||
| 151 | $i = $p; | ||
| 152 | } | ||
| 153 | |||
| 154 | ### @pending | ||
| 155 | foreach (reverse @pending) { | ||
| 156 | if ($count >= 0) { $count++; } | ||
| 157 | ### store: "$_ count $count" | ||
| 158 | cache_hash()->{"PisanoPeriodSteps:freq:".$_} = $count; | ||
| 159 | cache_hash()->{"PisanoPeriodSteps:log:".$_} = $log; | ||
| 160 | } | ||
| 161 | $value = ($self->{'values_type'} eq 'freq' ? $count : $log); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | ### return: $value | ||
| 165 | return ($value >= 0 ? $value : undef); | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; | ||
| 169 | __END__ |