| 1 | # Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2017, 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::SophieGermainPrimes; | ||||||||||||||||||||
| 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 | 2 | ▶ | use Math::Prime::XS 0.23 'is_prime'; # version 0.23 fix for 1928099 | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 2 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 2 | $VERSION = 75; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 2 | ▶ | use Math::NumSeq; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 2 | ▶ | use Math::NumSeq::Primes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 2 | @ISA = ('Math::NumSeq'); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | # use constant name => Math::NumSeq::__('Sophie Germain Primes'); | ||||||||||||||||||||
| 32 | 2 | ▶ | 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).'); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 2 | ▶ | use constant characteristic_increasing => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 2 | ▶ | use constant characteristic_integer => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 2 | ▶ | use constant values_min => 2; # first 2*2+1=5 | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 38 | # cf. A156874 count SG primes <= n | ||||||||||||||||||||
| 39 | # A092816 count SG primes <= 10^n | ||||||||||||||||||||
| 40 | # A007700 for n,2n+1,4n+3 are all primes - or something? | ||||||||||||||||||||
| 41 | # A005385 the safe primes | ||||||||||||||||||||
| 42 | # A156875 count safe primes <= n | ||||||||||||||||||||
| 43 | # A117360 n and 2*n+1 have same number of prime factors | ||||||||||||||||||||
| 44 | # | ||||||||||||||||||||
| 45 | # A156876 count SG or safe | ||||||||||||||||||||
| 46 | # A156877 count SG and safe | ||||||||||||||||||||
| 47 | # A156878 count neither SG nor safe | ||||||||||||||||||||
| 48 | # A156875 safe count | ||||||||||||||||||||
| 49 | # A156659 safe charact | ||||||||||||||||||||
| 50 | # A156658 p also 2*p+1 or (p-1)/2 prime | ||||||||||||||||||||
| 51 | # A156657 not safe primes | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 2 | ▶ | use constant oeis_anum => 'A005384'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 4 | ▶ | sub rewind { | ||||||||||||||||||
|
Subroutine
rewind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 58 | 4 | my ($self) = @_; | |||||||||||||||||||
| 59 | 4 | $self->{'i'} = $self->i_start; | |||||||||||||||||||
| 60 | 4 | $self->{'prime_seq'} = Math::NumSeq::Primes->new; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 109 | ▶ | sub next { | ||||||||||||||||||
|
Subroutine
next: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||
| 64 | 109 | my ($self) = @_; | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 109 | my $prime_seq = $self->{'prime_seq'}; | |||||||||||||||||||
| 67 | 109 | for (;;) { | |||||||||||||||||||
| 68 | 501 | ▶ | (undef, my $prime) = $prime_seq->next | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | or return; | ||||||||||||||||||||
| 70 | 501 | ▶ | if ($prime >= 0x7FFF_FFFF) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 0 | return; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | 501 | ▶ | if (is_prime(2*$prime+1)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 109 | return ($self->{'i'}++, $prime); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | # ENHANCE-ME: are_all_prime() to look for small divisors in both values | ||||||||||||||||||||
| 80 | # simultaneously, in case one or the other easily excluded. | ||||||||||||||||||||
| 81 | # | ||||||||||||||||||||
| 82 | 1955 | ▶ | sub pred { | ||||||||||||||||||
|
Subroutine
pred: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 83 | 1955 | my ($self, $value) = @_; | |||||||||||||||||||
| 84 | 1955 | ▶ | return ($self->Math::NumSeq::Primes::pred ($value) | ||||||||||||||||||
|
Condition100%3/3
$self->Math::NumSeq::Primes::pred($value) && $self->Math::NumSeq::Primes::pred(2 * $value + 1)
MC/DC100%2/2
$self->Math::NumSeq::Primes::pred($value) && $self->Math::NumSeq::Primes::pred(2 * $value + 1)
$self->Math::NumSeq::Primes::pred($value)
$self->Math::NumSeq::Primes::pred(2 * $value + 1)
Truth table100%3/3
A && B
A= $self->Math::NumSeq::Primes::pred($value)B= $self->Math::NumSeq::Primes::pred(2 * $value + 1)
| |||||||||||||||||||||
| 85 | && $self->Math::NumSeq::Primes::pred (2*$value + 1)); | ||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 2 | ▶ | use Math::NumSeq::TwinPrimes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 89 | 2 | *value_to_i_estimate = \&Math::NumSeq::TwinPrimes::value_to_i_estimate; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 2 | 1; | |||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||