File Coverage

blib/lib/Math/NumSeq/SophieGermainPrimes.pm
Criterion Covered Total %
statement 47 48 97.9
branch 4 6 66.6
condition 3 3 100.0
subroutine 15 15 100.0
pod 3 3 100.0
total 72 75 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   7439 use 5.004;
  2         5  
20 2     2   6 use strict;
  2         3  
  2         49  
21 2     2   353 use Math::Prime::XS 0.23 'is_prime'; # version 0.23 fix for 1928099
  2         10583  
  2         108  
22              
23 2     2   10 use vars '$VERSION', '@ISA';
  2         6  
  2         116  
24             $VERSION = 72;
25              
26 2     2   355 use Math::NumSeq;
  2         2  
  2         40  
27 2     2   338 use Math::NumSeq::Primes;
  2         2  
  2         74  
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   8 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         17  
  2         6  
36 2     2   7 use constant characteristic_increasing => 1;
  2         3  
  2         65  
37 2     2   6 use constant characteristic_integer => 1;
  2         2  
  2         71  
38 2     2   6 use constant values_min => 2; # first 2*2+1=5
  2         2  
  2         71  
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   5 use constant oeis_anum => 'A005384';
  2         4  
  2         370  
57              
58             #------------------------------------------------------------------------------
59              
60             sub rewind {
61 4     4 1 629 my ($self) = @_;
62 4         17 $self->{'i'} = $self->i_start;
63 4         21 $self->{'prime_seq'} = Math::NumSeq::Primes->new;
64             }
65              
66             sub next {
67 109     109 1 705 my ($self) = @_;
68              
69 109         87 my $prime_seq = $self->{'prime_seq'};
70 109         64 for (;;) {
71 501 50       602 (undef, my $prime) = $prime_seq->next
72             or return;
73 501 50       578 if ($prime >= 0x7FFF_FFFF) {
74 0         0 return;
75             }
76 501 100       837 if (is_prime(2*$prime+1)) {
77 109         146 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 4781 my ($self, $value) = @_;
87 1955   100     2493 return ($self->Math::NumSeq::Primes::pred ($value)
88             && $self->Math::NumSeq::Primes::pred (2*$value + 1));
89             }
90              
91 2     2   1001 use Math::NumSeq::TwinPrimes;
  2         3  
  2         57  
92             *value_to_i_estimate = \&Math::NumSeq::TwinPrimes::value_to_i_estimate;
93              
94             1;
95             __END__