| 1 | # Copyright 2010, 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::Emirps; | ||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use 5.004; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 23 | 2 | $VERSION = 75; | |||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use Math::NumSeq; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use Math::NumSeq::Primes; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 26 | 2 | @ISA = ('Math::NumSeq'); | |||||||||||||||||||||||||||||||||
| 27 | 2 | *_is_infinite = \&Math::NumSeq::_is_infinite; | |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||||||||
| 31 | #use Smart::Comments; | ||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | # use constant name => Math::NumSeq::__('Emirps'); | ||||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | use constant description => Math::NumSeq::__('Numbers which are primes forwards and backwards, eg. 157 because both 157 and 751 are primes. Palindromes like 131 are excluded. Default is decimal, or select a radix.'); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | use Math::NumSeq::Base::Digits | ||||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | 'parameter_info_array'; # radix parameter | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | use constant characteristic_increasing => 1; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | use constant characteristic_integer => 1; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | # FIXME: find the first value in the sequence ... maybe save it | ||||||||||||||||||||||||||||||||||
| 43 | 2 | my @values_min; | |||||||||||||||||||||||||||||||||
| 44 | 2 | $values_min[2] = 11; # binary 1011 reverse 1101 is decimal 13 | |||||||||||||||||||||||||||||||||
| 45 | 2 | $values_min[10] = 13; # reverse to 31 | |||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | sub values_min { | ||||||||||||||||||||||||||||||||
|
Subroutine
values_min: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 47 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 48 | 1 | return $values_min[$self->{'radix'}]; | |||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||
| 52 | # A006567 - decimal reversal is a prime and different | ||||||||||||||||||||||||||||||||||
| 53 | # A007500 - decimal reversal is a prime, so palindromes which are primes too | ||||||||||||||||||||||||||||||||||
| 54 | # | ||||||||||||||||||||||||||||||||||
| 55 | 2 | my @oeis_anum; | |||||||||||||||||||||||||||||||||
| 56 | 2 | $oeis_anum[2] = 'A080790'; | |||||||||||||||||||||||||||||||||
| 57 | 2 | $oeis_anum[10] = 'A006567'; | |||||||||||||||||||||||||||||||||
| 58 | # OEIS-Catalogue: A080790 radix=2 | ||||||||||||||||||||||||||||||||||
| 59 | # OEIS-Catalogue: A006567 radix=10 | ||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | sub oeis_anum { | ||||||||||||||||||||||||||||||||
|
Subroutine
oeis_anum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 61 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 62 | 1 | return $oeis_anum[$self->{'radix'}]; | |||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | 4 | ▶ | sub rewind { | ||||||||||||||||||||||||||||||||
|
Subroutine
rewind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 68 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 69 | 4 | $self->{'i'} = $self->i_start; | |||||||||||||||||||||||||||||||||
| 70 | 4 | $self->{'primes_seq'} = Math::NumSeq::Primes->new; | |||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | # ENHANCE-ME: The commented out code below took blocks of primes by radix | ||||||||||||||||||||||||||||||||||
| 74 | # powers and filtered. More memory but faster. | ||||||||||||||||||||||||||||||||||
| 75 | # | ||||||||||||||||||||||||||||||||||
| 76 | # ENHANCE-ME: No need to examine blocks where the high digit is even, or | ||||||||||||||||||||||||||||||||||
| 77 | # where it has a common factor with the radix. | ||||||||||||||||||||||||||||||||||
| 78 | # | ||||||||||||||||||||||||||||||||||
| 79 | 100 | ▶ | sub next { | ||||||||||||||||||||||||||||||||
|
Subroutine
next: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 80 | 100 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | 100 | my $primes_seq = $self->{'primes_seq'}; | |||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | 100 | for (;;) { | |||||||||||||||||||||||||||||||||
| 85 | 392 | ▶ | (undef, my $prime) = $primes_seq->next | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 86 | or return; | ||||||||||||||||||||||||||||||||||
| 87 | 392 | my $rev = _reverse_in_radix($prime,$self->{'radix'}); | |||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | ### consider: $prime | ||||||||||||||||||||||||||||||||||
| 90 | ### $rev | ||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | 392 | ▶ | if ($rev != $prime && $self->Math::NumSeq::Primes::pred($rev)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rev != $prime and $self->Math::NumSeq::Primes::pred($rev)
MC/DC100%2/2
$rev != $prime and $self->Math::NumSeq::Primes::pred($rev)
$rev != $prime
$self->Math::NumSeq::Primes::pred($rev)
Truth table100%3/3
A and B
A= $rev != $primeB= $self->Math::NumSeq::Primes::pred($rev)
| |||||||||||||||||||||||||||||||||||
| 93 | ### yes ... | ||||||||||||||||||||||||||||||||||
| 94 | 100 | return ($self->{'i'}++, $prime); | |||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||
| 99 | # ENHANCE-ME: are_all_prime() to look for small divisors in both values | ||||||||||||||||||||||||||||||||||
| 100 | # simultaneously, in case the reversal is even etc and easily excluded. | ||||||||||||||||||||||||||||||||||
| 101 | 2007 | ▶ | sub pred { | ||||||||||||||||||||||||||||||||
|
Subroutine
pred: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 102 | 2007 | my ($self, $value) = @_; | |||||||||||||||||||||||||||||||||
| 103 | 2007 | ▶ | if (_is_infinite($value)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 104 | 0 | return 0; | |||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||
| 106 | 2007 | my $rev = _reverse_in_radix($value,$self->{'radix'}); | |||||||||||||||||||||||||||||||||
| 107 | return ($rev != $value | ||||||||||||||||||||||||||||||||||
| 108 | && $self->Math::NumSeq::Primes::pred($value) | ||||||||||||||||||||||||||||||||||
| 109 | 2007 | ▶ | && $self->Math::NumSeq::Primes::pred(_reverse_in_radix($value,$self->{'radix'}))); | ||||||||||||||||||||||||||||||||
|
Condition100%6/6
$rev != $value && $self->Math::NumSeq::Primes::pred($value) && $self->Math::NumSeq::Primes::pred(_reverse_in_radix($value, $self->{'radix'}))
$rev != $value and $self->Math::NumSeq::Primes::pred($value)
MC/DC100%3/3
$rev != $value && $self->Math::NumSeq::Primes::pred($value) && $self->Math::NumSeq::Primes::pred(_reverse_in_radix($value, $self->{'radix'}))
$rev != $value
$self->Math::NumSeq::Primes::pred($value)
$self->Math::NumSeq::Primes::pred(_reverse_in_radix($value, $self->{'radix'}))
Truth table100%4/4
A and B && C
A= $rev != $valueB= $self->Math::NumSeq::Primes::pred($value)C= $self->Math::NumSeq::Primes::pred(_reverse_in_radix($value, $self->{'radix'}))
| |||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | # return $n reversed in $radix | ||||||||||||||||||||||||||||||||||
| 113 | 2599 | ▶ | sub _reverse_in_radix { | ||||||||||||||||||||||||||||||||
|
Subroutine
_reverse_in_radix: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 114 | 2599 | my ($n, $radix) = @_; | |||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | 2599 | ▶ | if ($radix == 10) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 117 | 2597 | return scalar(reverse("$n")); | |||||||||||||||||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||||||||||||||||
| 119 | 2 | my $ret = $n*0; # inherit bignum 0 | |||||||||||||||||||||||||||||||||
| 120 | # ### _reverse_in_radix(): sprintf '%#X %d', $n, $n | ||||||||||||||||||||||||||||||||||
| 121 | 2 | ▶ | do { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 122 | 5 | $ret = $ret * $radix + ($n % $radix); | |||||||||||||||||||||||||||||||||
| 123 | } while ($n = int($n/$radix)); | ||||||||||||||||||||||||||||||||||
| 124 | # ### ret: sprintf '%#X %d', $ret, $ret | ||||||||||||||||||||||||||||||||||
| 125 | 2 | return $ret; | |||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | 2 | 1; | |||||||||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||||||||