| 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 | package Math::NumSeq::LemoineCount; | ||||||||||||||||||||||||||||
| 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 'max'; | ||||||||||||||||||||||||||
|
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 | 1 | @ISA = ('Math::NumSeq'); | |||||||||||||||||||||||||||
| 26 | 1 | *_is_infinite = \&Math::NumSeq::_is_infinite; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use Math::NumSeq::Primes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 31 | #use Smart::Comments; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # use constant name => Math::NumSeq::__('Lemoine Count'); | ||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use constant description => Math::NumSeq::__('The number of ways i can be represented as P+2*Q for primes P and Q.'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use constant default_i_start => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | use constant values_min => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | use constant characteristic_count => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | use constant characteristic_smaller => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use constant parameter_info_array => | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | [ | ||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||
| 44 | name => 'on_values', | ||||||||||||||||||||||||||||
| 45 | share_key => 'on_values_odd', | ||||||||||||||||||||||||||||
| 46 | type => 'enum', | ||||||||||||||||||||||||||||
| 47 | default => 'all', | ||||||||||||||||||||||||||||
| 48 | choices => ['all','odd'], | ||||||||||||||||||||||||||||
| 49 | choices_display => [Math::NumSeq::__('All'), | ||||||||||||||||||||||||||||
| 50 | Math::NumSeq::__('Odd')], | ||||||||||||||||||||||||||||
| 51 | description => Math::NumSeq::__('The values to act on, either all integers of just the odd integers.'), | ||||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||
| 53 | 1 | ]; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 56 | # "one_as_prime" secret undocumented parameter ... | ||||||||||||||||||||||||||||
| 57 | # some sort of odd i only option too maybe ... | ||||||||||||||||||||||||||||
| 58 | # | ||||||||||||||||||||||||||||
| 59 | # A046925 ways 2n+1, including 1 as a prime | ||||||||||||||||||||||||||||
| 60 | # A046927 ways 2n+1, not including 1 as a prime | ||||||||||||||||||||||||||||
| 61 | # A194831 records of A046927 | ||||||||||||||||||||||||||||
| 62 | # A194830 positions of records | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | my %oeis_anum = (all => [ 'A046924', # ways n, including 1 as a prime | |||||||||||||||||||||||||||
| 65 | 'A046926', # ways n, not including 1 as a prime | ||||||||||||||||||||||||||||
| 66 | ], | ||||||||||||||||||||||||||||
| 67 | odd => [ 'A046925', # ways n, including 1 as a prime | ||||||||||||||||||||||||||||
| 68 | 'A046927', # ways n, not including 1 as a prime | ||||||||||||||||||||||||||||
| 69 | ], | ||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||
| 71 | # OEIS-Catalogue: A046926 | ||||||||||||||||||||||||||||
| 72 | # OEIS-Catalogue: A046924 one_as_prime=1 | ||||||||||||||||||||||||||||
| 73 | # OEIS-Catalogue: A046927 on_values=odd # starting n=0 for odd=1 | ||||||||||||||||||||||||||||
| 74 | # OEIS-Catalogue: A046925 on_values=odd one_as_prime=1 | ||||||||||||||||||||||||||||
| 75 | 1 | ▶ | sub oeis_anum { | ||||||||||||||||||||||||||
|
Subroutine
oeis_anum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 77 | 1 | return $oeis_anum{$self->{'on_values'}}->[!$self->{'one_as_prime'}]; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 3 | ▶ | sub rewind { | ||||||||||||||||||||||||||
|
Subroutine
rewind: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 84 | ### LemoineCount rewind() ... | ||||||||||||||||||||||||||||
| 85 | 3 | ▶ | $self->{'i_start'} = ($self->{'on_values'} eq 'odd' ? 0 : 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 3 | $self->{'i'} = $self->i_start; | |||||||||||||||||||||||||||
| 87 | 3 | $self->{'a'} = 1; | |||||||||||||||||||||||||||
| 88 | 3 | $self->{'array'} = []; | |||||||||||||||||||||||||||
| 89 | 3 | $self->{'size'} = 500; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 36 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=14 SCAR=27.0
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 36 | my ($self) = @_; | |||||||||||||||||||||||||||
| 94 | ### next(): "i=$self->{'i'}" | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 36 | for (;;) { | |||||||||||||||||||||||||||
| 97 | 36 | ▶ | unless (@{$self->{'array'}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 2 | $self->{'size'} = int (1.08 * $self->{'size'}); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 2 | my $lo = $self->{'a'}; | |||||||||||||||||||||||||||
| 101 | 2 | my $hi = $lo + $self->{'size'}; | |||||||||||||||||||||||||||
| 102 | 2 | $self->{'next_lo'} = $hi+1; | |||||||||||||||||||||||||||
| 103 | ### range: "lo=$lo to hi=$hi" | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 2 | my @array; | |||||||||||||||||||||||||||
| 106 | 2 | $array[$hi-$lo] = 0; # array size | |||||||||||||||||||||||||||
| 107 | 2 | $self->{'array'} = \@array; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | ▶ | my @primes = (($self->{'one_as_prime'} ? (1) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | Math::NumSeq::Primes::_primes_list (0, $hi)); | ||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | 2 | my $qamax = $#primes; | |||||||||||||||||||||||||||
| 113 | 2 | foreach my $pa (0 .. $#primes-1) { | |||||||||||||||||||||||||||
| 114 | 198 | foreach my $qa (reverse $pa .. $qamax) { | |||||||||||||||||||||||||||
| 115 | 2574 | my $sum = $primes[$pa] + 2*$primes[$qa]; | |||||||||||||||||||||||||||
| 116 | ### at: "p=$primes[$pa] q=$primes[$qa] sum=$sum incr ".($sum-$lo) | ||||||||||||||||||||||||||||
| 117 | 2574 | ▶ | if ($sum > $hi) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 118 | 118 | $qamax = $qa-1; | |||||||||||||||||||||||||||
| 119 | } elsif ($sum < $lo) { | ||||||||||||||||||||||||||||
| 120 | 0 | last; | |||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||
| 122 | 2456 | $array[$sum-$lo]++; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||
| 128 | 2 | my $qamax = $#primes; | |||||||||||||||||||||||||||
| 129 | 2 | foreach my $pa (0 .. $#primes-1) { | |||||||||||||||||||||||||||
| 130 | 198 | foreach my $qa (reverse $pa+1 .. $qamax) { | |||||||||||||||||||||||||||
| 131 | 4556 | my $sum = 2*$primes[$pa] + $primes[$qa]; | |||||||||||||||||||||||||||
| 132 | ### at: "p=$primes[$pa] q=$primes[$qa] sum=$sum incr ".($sum-$lo) | ||||||||||||||||||||||||||||
| 133 | 4556 | ▶ | if ($sum > $hi) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 134 | 116 | $qamax = $qa-1; | |||||||||||||||||||||||||||
| 135 | } elsif ($sum < $lo) { | ||||||||||||||||||||||||||||
| 136 | 0 | last; | |||||||||||||||||||||||||||
| 137 | } else { | ||||||||||||||||||||||||||||
| 138 | 4440 | $array[$sum-$lo]++; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | ### @array | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 36 | my $a = $self->{'a'}++; | |||||||||||||||||||||||||||
| 147 | 36 | ▶ | my $value = shift @{$self->{'array'}} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift @{$$self{'array'};} || 0
MC/DC100%1/1
shift @{$$self{'array'};} || 0
shift @{$$self{'array'};}
Truth table100%2/2
A
A= shift @{$$self{'array'};}
| |||||||||||||||||||||||||||||
| 148 | 36 | ▶ | if ($self->{'on_values'} eq 'odd' && !($a&1)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"on_values"} eq "odd" and not $a & 1
MC/DC0%0/2
$self->{"on_values"} eq "odd" and not $a & 1
$self->{"on_values"} eq "odd"
not $a & 1
Truth table33%1/3
A and not(B)
A= $self->{"on_values"} eq "odd"B= not $a & 1
| |||||||||||||||||||||||||||||
| 149 | 0 | next; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 36 | return ($self->{'i'}++, $value); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 57 | ▶ | sub ith { | ||||||||||||||||||||||||||
|
Subroutine
ith: called CC=10 SCAR=23.3
Pod
documented
| |||||||||||||||||||||||||||||
| 156 | 57 | my ($self, $i) = @_; | |||||||||||||||||||||||||||
| 157 | ### ith(): $i | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 57 | ▶ | if ($self->{'on_values'} eq 'odd') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | $i = 2*$i+1; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 57 | ▶ | if ($i < 3) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 9 | return 0; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 48 | ▶ | unless ($i >= 0 && $i <= 0xFF_FFFF) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$i >= 0 and $i <= 16777215
MC/DC0%0/2
$i >= 0 and $i <= 16777215
$i >= 0
$i <= 16777215
Truth table33%1/3
A and B
A= $i >= 0B= $i <= 16777215
| |||||||||||||||||||||||||||||
| 166 | 0 | return undef; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 48 | $i = "$i"; # numize any Math::BigInt for speed | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 48 | my $count = 0; | |||||||||||||||||||||||||||
| 171 | 48 | ▶ | my @primes = (($self->{'one_as_prime'} ? (1) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | Math::NumSeq::Primes::_primes_list (0, $i-1)); | ||||||||||||||||||||||||||||
| 173 | ### @primes | ||||||||||||||||||||||||||||
| 174 | { | ||||||||||||||||||||||||||||
| 175 | 48 | my $pa = 0; | |||||||||||||||||||||||||||
| 176 | 48 | my $qa = $#primes; | |||||||||||||||||||||||||||
| 177 | 48 | ▶ | while ($pa <= $qa) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 222 | my $sum = $primes[$pa] + 2*$primes[$qa]; | |||||||||||||||||||||||||||
| 179 | 222 | ▶ | if ($sum <= $i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | ### at: "p=$primes[$pa] q=$primes[$qa] count ".($sum == $i) | ||||||||||||||||||||||||||||
| 181 | 89 | $count += ($sum == $i); | |||||||||||||||||||||||||||
| 182 | 89 | $pa++; | |||||||||||||||||||||||||||
| 183 | } else { | ||||||||||||||||||||||||||||
| 184 | 133 | $qa--; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | { | ||||||||||||||||||||||||||||
| 189 | 48 | my $pa = 0; | |||||||||||||||||||||||||||
| 190 | 48 | my $qa = $#primes; | |||||||||||||||||||||||||||
| 191 | 48 | ▶ | while ($pa < $qa) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 174 | my $sum = 2*$primes[$pa] + $primes[$qa]; | |||||||||||||||||||||||||||
| 193 | 174 | ▶ | if ($sum <= $i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | ### at: "p=$primes[$pa] q=$primes[$qa] count ".($sum == $i) | ||||||||||||||||||||||||||||
| 195 | 74 | $count += ($sum == $i); | |||||||||||||||||||||||||||
| 196 | 74 | $pa++; | |||||||||||||||||||||||||||
| 197 | } else { | ||||||||||||||||||||||||||||
| 198 | 100 | $qa--; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | 48 | return $count; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 1 | 1; | |||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||