``````# Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2019, 2020 Kevin Ryde

# This file is part of Math-NumSeq.
#
# Math-NumSeq is free software; you can redistribute it and/or modify
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# Math-NumSeq is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Math-NumSeq.  If not, see <http://www.gnu.org/licenses/>.

package Math::NumSeq::Emirps;
use 5.004;
use strict;

use vars '\$VERSION', '@ISA';
\$VERSION = 75;
use Math::NumSeq;
use Math::NumSeq::Primes;
@ISA = ('Math::NumSeq');
*_is_infinite = \&Math::NumSeq::_is_infinite;

# uncomment this to run the ### lines

# use constant name => Math::NumSeq::__('Emirps');
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.');

use Math::NumSeq::Base::Digits

use constant characteristic_increasing => 1;
use constant characteristic_integer => 1;

# FIXME: find the first value in the sequence ... maybe save it
my @values_min;
\$values_min[2]  = 11; # binary 1011 reverse 1101 is decimal 13
\$values_min[10] = 13; # reverse to 31
sub values_min {
my (\$self) = @_;
}

#------------------------------------------------------------------------------
# A006567 - decimal reversal is a prime and different
# A007500 - decimal reversal is a prime, so palindromes which are primes too
#
my @oeis_anum;
\$oeis_anum[2] = 'A080790';
\$oeis_anum[10] = 'A006567';
sub oeis_anum {
my (\$self) = @_;
}

#------------------------------------------------------------------------------

sub rewind {
my (\$self) = @_;
\$self->{'i'} = \$self->i_start;
\$self->{'primes_seq'} = Math::NumSeq::Primes->new;
}

# ENHANCE-ME: The commented out code below took blocks of primes by radix
# powers and filtered.  More memory but faster.
#
# ENHANCE-ME: No need to examine blocks where the high digit is even, or
# where it has a common factor with the radix.
#
sub next {
my (\$self) = @_;

my \$primes_seq = \$self->{'primes_seq'};

for (;;) {
(undef, my \$prime) = \$primes_seq->next
or return;

### consider: \$prime
### \$rev

if (\$rev != \$prime && \$self->Math::NumSeq::Primes::pred(\$rev)) {
### yes ...
return (\$self->{'i'}++, \$prime);
}
}
}

# ENHANCE-ME: are_all_prime() to look for small divisors in both values
# simultaneously, in case the reversal is even etc and easily excluded.
sub pred {
my (\$self, \$value) = @_;
if (_is_infinite(\$value)) {
return 0;
}
return (\$rev != \$value
&& \$self->Math::NumSeq::Primes::pred(\$value)
}

# return \$n reversed in \$radix

return scalar(reverse("\$n"));
} else {
my \$ret = \$n*0;   # inherit bignum 0
# ### _reverse_in_radix(): sprintf '%#X %d', \$n, \$n
do {
# ### ret: sprintf '%#X %d', \$ret, \$ret
return \$ret;
}
}

1;
__END__

#   my (\$n, \$radix) = @_;
# }
#
# sub new {
#   my (\$class, %options) = @_;
#   ### Emirps new()
#
#   my \$lo = \$options{'lo'} || 0;
#   my \$hi = \$options{'hi'};
#   \$lo = max (10, \$lo);
#   \$hi = max (\$lo, \$hi);
#
#   #
#   ### using primes: "\$primes_lo to \$primes_hi"
#
#   # Math::NumSeq::Primes->new (lo => \$primes_lo,
#   #                                      hi => \$primes_hi);
#
#   require Math::NumSeq::Primes;
#   my @array = Math::NumSeq::Primes::_primes_list
#     (\$primes_lo, \$primes_hi);
#
#   my %primes;
#   @primes{@array} = ();
#   if (\$radix == 10) {
#     @array = grep {
#       \$_ >= \$lo && \$_ <= \$hi && do {
#         my \$r;
#         (((\$r = reverse \$_) != \$_) && exists \$primes{\$r})
#       }
#     } @array;
#   } else {
#     @array = grep {
#       \$_ >= \$lo && \$_ <= \$hi && do {
#         my \$r;
#       }
#     } @array;
#   }
#   ### @array
#
#   return \$class->SUPER::new (%options,
#                              array => \@array);
# }

=for stopwords Ryde Math-NumSeq emirp emirps

Math::NumSeq::Emirps -- primes backwards and forwards

use Math::NumSeq::Emirps;
my \$seq = Math::NumSeq::Emirps->new;
my (\$i, \$value) = \$seq->next;

The "emirps", being numbers which are primes backwards and forwards.  For
example 157 is an emirp because both 157 and its reverse 751 are primes.
Prime palindromes are excluded.

The default base is decimal, or the C<radix> parameter can select another
base.

See L<Math::NumSeq/FUNCTIONS> for behaviour common to all sequence classes.

=over 4

=item C<\$seq = Math::NumSeq::Emirps-E<gt>new ()>

=item C<\$seq = Math::NumSeq::Emirps-E<gt>new (radix =E<gt> 16)>

Create and return a new sequence object.

=item C<\$bool = \$seq-E<gt>pred(\$value)>

Return true if C<\$value> is an emirp, meaning it and its digit reversal (in

=back

L<Math::NumSeq>,
L<Math::NumSeq::Primes>

L<http://user42.tuxfamily.org/math-numseq/index.html>

Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2019, 2020 Kevin Ryde

Math-NumSeq is free software; you can redistribute it and/or modify it