File Coverage

blib/lib/Math/NumSeq/SophieGermainPrimes.pm
Criterion Covered Total %
statement 48 49 97.9
branch 4 6 66.6
condition 3 3 100.0
subroutine 15 15 100.0
pod 3 3 100.0
total 73 76 96.0


line stmt bran cond sub pod time code
1             # Copyright 2010, 2011, 2012, 2013, 2014 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 .
17              
18             package Math::NumSeq::SophieGermainPrimes;
19 2     2   29421 use 5.004;
  2         8  
  2         132  
20 2     2   16 use strict;
  2         5  
  2         107  
21 2     2   9333 use Math::Prime::XS 0.23 'is_prime'; # version 0.23 fix for 1928099
  2         19030  
  2         140  
22              
23 2     2   16 use vars '$VERSION', '@ISA';
  2         3  
  2         160  
24             $VERSION = 71;
25              
26 2     2   772 use Math::NumSeq;
  2         5  
  2         50  
27 2     2   622 use Math::NumSeq::Primes;
  2         6  
  2         117  
28             @ISA = ('Math::NumSeq');
29              
30             # uncomment this to run the ### lines
31             #use Smart::Comments;
32              
33              
34             # use constant name => Math::NumSeq::__('Sophie Germain Primes');
35 2     2   12 use constant description => Math::NumSeq::__('Sophie Germain primes 3,5,7,11,23,29, being primes P where 2*P+1 is also prime (those latter being the "safe" primes).');
  2         29  
  2         11  
36 2     2   16 use constant characteristic_increasing => 1;
  2         4  
  2         185  
37 2     2   12 use constant characteristic_integer => 1;
  2         4  
  2         102  
38 2     2   11 use constant values_min => 2; # first 2*2+1=5
  2         3  
  2         131  
39              
40             #------------------------------------------------------------------------------
41             # cf. A156874 count SG primes <= n
42             # A092816 count SG primes <= 10^n
43             # A007700 for n,2n+1,4n+3 are all primes - or something?
44             # A005385 the safe primes
45             # A156875 count safe primes <= n
46             # A117360 n and 2*n+1 have same number of prime factors
47             #
48             # A156876 count SG or safe
49             # A156877 count SG and safe
50             # A156878 count neither SG nor safe
51             # A156875 safe count
52             # A156659 safe charact
53             # A156658 p also 2*p+1 or (p-1)/2 prime
54             # A156657 not safe primes
55              
56 2     2   11 use constant oeis_anum => 'A005384';
  2         5  
  2         543  
57              
58             #------------------------------------------------------------------------------
59              
60             sub rewind {
61 4     4 1 2001 my ($self) = @_;
62 4         32 $self->{'i'} = $self->i_start;
63 4         182 $self->{'prime_seq'} = Math::NumSeq::Primes->new;
64             }
65              
66             sub next {
67 109     109 1 1394 my ($self) = @_;
68              
69 109         383 my $prime_seq = $self->{'prime_seq'};
70 109         167 for (;;) {
71 501 50       2546 (undef, my $prime) = $prime_seq->next
72             or return;
73 501 50       1216 if ($prime >= 0x7FFF_FFFF) {
74 0         0 return;
75             }
76 501 100       8229 if (is_prime(2*$prime+1)) {
77 109         648 return ($self->{'i'}++, $prime);
78             }
79             }
80             }
81              
82             # ENHANCE-ME: are_all_prime() to look for small divisors in both values
83             # simultaneously, in case one or the other easily excluded.
84             #
85             sub pred {
86 1955     1955 1 12047 my ($self, $value) = @_;
87 1955   100     16315 return ($self->Math::NumSeq::Primes::pred ($value)
88             && $self->Math::NumSeq::Primes::pred (2*$value + 1));
89             }
90              
91 2     2   6527 use Math::NumSeq::TwinPrimes;
  2         6  
  2         100  
92             *value_to_i_estimate = \&Math::NumSeq::TwinPrimes::value_to_i_estimate;
93              
94             1;
95             __END__