# Copyright 2012 Kevin Ryde

# This file is part of Math-NumSeq.
#
# Math-NumSeq is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# 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::DuffinianNumbers;
use 5.004;
use strict;
use List::Util 'sum';
use Math::Prime::XS 0.23 'is_prime'; # version 0.23 fix for 1928099
use Math::Factor::XS 'factors';


use vars '$VERSION', '@ISA';
$VERSION = 46;
use Math::NumSeq;
use List::Util 'min';
use Math::NumSeq::Base::IteratePred;
@ISA = ('Math::NumSeq::Base::IteratePred',
        'Math::NumSeq');
*_is_infinite = \&Math::NumSeq::_is_infinite;
*_to_bigint = \&Math::NumSeq::_to_bigint;

# uncomment this to run the ### lines
#use Smart::Comments;


# use constant name => Math::NumSeq::__('Duffinian Numbers');
use constant description => Math::NumSeq::__('Duffinian numbers.');
use constant i_start => 1;
use constant characteristic_integer => 1;
use constant characteristic_increasing => 1;

use constant values_min => 4;

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

use constant oeis_anum => 'A003624';


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

sub pred {
  my ($self, $value) = @_;
  ### DuffinianNumbers pred(): $value

  if (_is_infinite($value)) {
    return undef;
  }
  if ($value < 2 || $value != int($value)) {
    return 0;
  }
  if ($value > 0xFFFF_FFFF) {
    return undef;
  }
  return ! is_prime($value)
    && _coprime($value, sum(1,factors($value)));
}

sub _coprime {
  my ($x, $y) = @_;
  #### _coprime(): "$x,$y"
  if ($y > $x) {
    return 0;
  }
  for (;;) {
    if ($y <= 1) {
      return ($y == 1);
    }
    ($x,$y) = ($y, $x % $y);
  }
}

1;
__END__

=for stopwords Ryde Math-NumSeq Duffinian

=head1 NAME

Math::NumSeq::DuffinianNumbers -- numbers having no common factor with sum-of-divisors

=head1 SYNOPSIS

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

=head1 DESCRIPTION

I<In progress ...>

This is the Duffinian numbers ...

    1 ...

=head1 FUNCTIONS

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

=over 4

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

Create and return a new sequence object.

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

Return true if C<$value> is a Duffinian number.

=back

=head1 SEE ALSO

L<Math::NumSeq>

=head1 HOME PAGE

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

=head1 LICENSE

Copyright 2012 Kevin Ryde

Math-NumSeq is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the 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/>.

=cut
