| 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 | |||||||||||||||||||||||||||||||||||||||||
| 19 | # http://infolab.stanford.edu/TR/CS-TR-72-269.html | ||||||||||||||||||||||||||||||||||||||||
| 20 | # http://infolab.stanford.edu/TR/CS-TR-72-275.html | ||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||
| 22 | package Math::NumSeq::KlarnerRado; | ||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use 5.004; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use List::Util; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | $VERSION = 75; | |||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Math::NumSeq; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use Math::NumSeq::Base::IteratePred; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | @ISA = ('Math::NumSeq::Base::IteratePred', 'Math::NumSeq'); | |||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||
| 34 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||||||||||||||
| 35 | #use Devel::Comments; | ||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||
| 37 | # use constant name => Math::NumSeq::__('Klarner-Rado'); | ||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | use constant description => Math::NumSeq::__('Klarner-Rado sequence 1,2,4,5,8,9,etc being 1 and then if n in the sequence so are 2n, 3n+2 and 6n+3.'); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | use constant i_start => 1; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||
| 41 | 4 | ▶ | sub values_min { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
values_min: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||
| 43 | 4 | return $self->{'start'}; | |||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | use constant parameter_info_array => | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 47 | [ { name => 'start', | ||||||||||||||||||||||||||||||||||||||||
| 48 | type => 'integer', | ||||||||||||||||||||||||||||||||||||||||
| 49 | default => 1, | ||||||||||||||||||||||||||||||||||||||||
| 50 | minimum => 1, | ||||||||||||||||||||||||||||||||||||||||
| 51 | width => 3, | ||||||||||||||||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ]; | |||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||
| 55 | # cf A005659 2n-2, 3n-3, starting 4 | ||||||||||||||||||||||||||||||||||||||||
| 56 | # A005660 2n+2, 3n+3, starting 3 | ||||||||||||||||||||||||||||||||||||||||
| 57 | # A005661 2n-2, 3n-3, starting 5 | ||||||||||||||||||||||||||||||||||||||||
| 58 | # A005662 2n+2, 3n+3 | ||||||||||||||||||||||||||||||||||||||||
| 59 | # A185661 2n+1, 3n+1, starting 1 | ||||||||||||||||||||||||||||||||||||||||
| 60 | # A005658 2n+1, 3n+1, 6n+1, starting 1 | ||||||||||||||||||||||||||||||||||||||||
| 61 | # | ||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | my @oeis_anum; | |||||||||||||||||||||||||||||||||||||||
| 63 | 1 | $oeis_anum[1] = 'A005658'; | |||||||||||||||||||||||||||||||||||||||
| 64 | 1 | ▶ | sub oeis_anum { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
oeis_anum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||
| 66 | 1 | return $oeis_anum[$self->{'start'}]; | |||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||
| 69 | # sub rewind { | ||||||||||||||||||||||||||||||||||||||||
| 70 | # my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
| 71 | # $self->{'i'} = $self->i_start; | ||||||||||||||||||||||||||||||||||||||||
| 72 | # | ||||||||||||||||||||||||||||||||||||||||
| 73 | # # $self->{'pending'} = { 1 => undef }; | ||||||||||||||||||||||||||||||||||||||||
| 74 | # # $self->{'pref_ref'} = \''; | ||||||||||||||||||||||||||||||||||||||||
| 75 | # # $self->{'pstart'} = 0; | ||||||||||||||||||||||||||||||||||||||||
| 76 | # } | ||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||
| 78 | # sub next { | ||||||||||||||||||||||||||||||||||||||||
| 79 | # my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
| 80 | # ### KlarnerRado next(): "$self->{'i'}" | ||||||||||||||||||||||||||||||||||||||||
| 81 | # | ||||||||||||||||||||||||||||||||||||||||
| 82 | # # my $pstart = $self->{'pstart'}; | ||||||||||||||||||||||||||||||||||||||||
| 83 | # # vec($$pref, | ||||||||||||||||||||||||||||||||||||||||
| 84 | # # # my $pending = $self->{'pending'}; | ||||||||||||||||||||||||||||||||||||||||
| 85 | # # return ($self->{'i'}++, $min); | ||||||||||||||||||||||||||||||||||||||||
| 86 | # | ||||||||||||||||||||||||||||||||||||||||
| 87 | # my $pending = $self->{'pending'}; | ||||||||||||||||||||||||||||||||||||||||
| 88 | # my $min = List::Util::min (keys %$pending); | ||||||||||||||||||||||||||||||||||||||||
| 89 | # delete $pending->{$min}; | ||||||||||||||||||||||||||||||||||||||||
| 90 | # @{$pending}{2*$min,3*$min+2,6*$min+3} = (); | ||||||||||||||||||||||||||||||||||||||||
| 91 | # return ($self->{'i'}++, $min); | ||||||||||||||||||||||||||||||||||||||||
| 92 | # } | ||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||
| 94 | 41 | ▶ | sub pred { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
pred: called CC=15 SCAR=27.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 95 | 41 | my ($self, $value) = @_; | |||||||||||||||||||||||||||||||||||||||
| 96 | ### KlarnerRado pred(): $value | ||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||
| 98 | 41 | my $start = $self->{'start'}; | |||||||||||||||||||||||||||||||||||||||
| 99 | 41 | my @pending = ($value); | |||||||||||||||||||||||||||||||||||||||
| 100 | 41 | for (;;) { | |||||||||||||||||||||||||||||||||||||||
| 101 | ### @pending | ||||||||||||||||||||||||||||||||||||||||
| 102 | ### $value | ||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||
| 104 | 102 | ▶ | if ($value <= $start) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||
| 105 | 24 | ▶ | if ($value == $start) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 106 | 24 | return 1; | |||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | $value = pop @pending || return 0; | ||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
pop @pending || (return 0)
MC/DC0%0/1
pop @pending || (return 0)
pop @pending
Truth table0%0/2
A
A= pop @pending
| |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||
| 110 | ▶ | } elsif ($value == 2 || $value == 4) { | |||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$value == 2 or $value == 4
MC/DC100%2/2
$value == 2 or $value == 4
$value == 2
$value == 4
Truth table100%3/3
A or B
A= $value == 2B= $value == 4
| |||||||||||||||||||||||||||||||||||||||||
| 111 | 20 | $value /= 2; # from 2k | |||||||||||||||||||||||||||||||||||||||
| 112 | } elsif ($value == 5) { | ||||||||||||||||||||||||||||||||||||||||
| 113 | 4 | $value = 1; # from 3k+2 | |||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||
| 115 | } elsif ($value >= 6) { | ||||||||||||||||||||||||||||||||||||||||
| 116 | # mod6 | ||||||||||||||||||||||||||||||||||||||||
| 117 | # 2k = 0,2, 4 | ||||||||||||||||||||||||||||||||||||||||
| 118 | # 3k+2 = 2, 5 | ||||||||||||||||||||||||||||||||||||||||
| 119 | # 6k+3 = 3 | ||||||||||||||||||||||||||||||||||||||||
| 120 | # | ||||||||||||||||||||||||||||||||||||||||
| 121 | 26 | my $mod = ($value % 6); | |||||||||||||||||||||||||||||||||||||||
| 122 | 26 | ▶ | if ($mod == 0 || $mod == 4) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition66%2/3
$mod == 0 or $mod == 4
MC/DC50%1/2
$mod == 0 or $mod == 4
$mod == 0
$mod == 4
Truth table66%2/3
A or B
A= $mod == 0B= $mod == 4
| |||||||||||||||||||||||||||||||||||||||||
| 123 | # 6k under 2n -> 3k is 0,3 mod 6 | ||||||||||||||||||||||||||||||||||||||||
| 124 | # 6k+4 under 2n -> 3k+2 is 2,5 mod 6 | ||||||||||||||||||||||||||||||||||||||||
| 125 | 8 | $value /= 2; | |||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||
| 127 | } elsif ($mod == 2) { | ||||||||||||||||||||||||||||||||||||||||
| 128 | ### 6k+2 ... | ||||||||||||||||||||||||||||||||||||||||
| 129 | # under 2n -> 3k+2 is 2,5 mod 6 | ||||||||||||||||||||||||||||||||||||||||
| 130 | # under 3n+2 -> 2k is 0,2,4 mod 6 | ||||||||||||||||||||||||||||||||||||||||
| 131 | 6 | push @pending, ($value-2)/3; # n <- 3n+2 | |||||||||||||||||||||||||||||||||||||||
| 132 | 6 | $value /= 2; # n <- 2n | |||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||
| 134 | } elsif ($mod == 3) { | ||||||||||||||||||||||||||||||||||||||||
| 135 | # 6k+3 -> k | ||||||||||||||||||||||||||||||||||||||||
| 136 | 4 | $value -= 3; | |||||||||||||||||||||||||||||||||||||||
| 137 | 4 | $value /= 6; | |||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||
| 139 | } elsif ($mod == 5) { | ||||||||||||||||||||||||||||||||||||||||
| 140 | # 6k+5 under 3n+2 -> 2*k+1 | ||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | $value -= 2; | |||||||||||||||||||||||||||||||||||||||
| 142 | 0 | $value /= 3; | |||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||
| 144 | } else { | ||||||||||||||||||||||||||||||||||||||||
| 145 | # mod == 1 | ||||||||||||||||||||||||||||||||||||||||
| 146 | 8 | ▶ | $value = pop @pending || return 0; | ||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
pop @pending || (return 0)
MC/DC100%1/1
pop @pending || (return 0)
pop @pending
Truth table100%2/2
A
A= pop @pending
| |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||
| 148 | } else { | ||||||||||||||||||||||||||||||||||||||||
| 149 | 28 | ▶ | $value = pop @pending || return 0; | ||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
pop @pending || (return 0)
MC/DC100%1/1
pop @pending || (return 0)
pop @pending
Truth table100%2/2
A
A= pop @pending
| |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||
| 154 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||
| 155 | __END__ | ||||||||||||||||||||||||||||||||||||||||