line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Prime::Util::PrimeIterator; |
2
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
BEGIN { |
6
|
1
|
|
|
1
|
|
3
|
$Math::Prime::Util::PrimeIterator::AUTHORITY = 'cpan:DANAJ'; |
7
|
1
|
|
|
|
|
15
|
$Math::Prime::Util::PrimeIterator::VERSION = '0.68'; |
8
|
|
|
|
|
|
|
} |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
5
|
use base qw( Exporter ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
127
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw( ); |
12
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => [ @EXPORT_OK ]); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
5
|
use Math::Prime::Util qw/next_prime prev_prime is_prime prime_count nth_prime/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# We're going to use a scalar rather than a hash because there is currently |
18
|
|
|
|
|
|
|
# only one data object (the current value) and this makes it little faster. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub new { |
21
|
11
|
|
|
11
|
1
|
20
|
my ($class, $start) = @_; |
22
|
11
|
|
|
|
|
15
|
my $p = 2; |
23
|
11
|
|
|
|
|
21
|
my $self = bless \$p, $class; |
24
|
11
|
100
|
|
|
|
43
|
$self->rewind($start) if defined $start; |
25
|
8
|
|
|
|
|
17
|
return $self; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# To make Iterator::Simple happy. |
29
|
|
|
|
|
|
|
sub __iter__ { |
30
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
31
|
0
|
|
|
|
|
0
|
require Iterator::Simple; |
32
|
0
|
|
|
0
|
|
0
|
return Iterator::Simple::iterator(sub { $self->iterate }); |
|
0
|
|
|
|
|
0
|
|
33
|
0
|
|
|
|
|
0
|
$self; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
9
|
|
|
9
|
1
|
24
|
sub value { ${$_[0]}; } |
|
9
|
|
|
|
|
39
|
|
37
|
|
|
|
|
|
|
sub next { |
38
|
|
|
|
|
|
|
#my $self = shift; $$self = next_prime($$self); return $self; |
39
|
214
|
|
|
214
|
1
|
450
|
${$_[0]} = next_prime(${$_[0]}); |
|
214
|
|
|
|
|
320
|
|
|
214
|
|
|
|
|
379
|
|
40
|
214
|
|
|
|
|
277
|
return $_[0]; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
sub prev { |
43
|
3
|
|
|
3
|
1
|
498
|
my $self = shift; |
44
|
3
|
|
|
|
|
7
|
my $p = $$self; |
45
|
3
|
50
|
|
|
|
11
|
$$self = ($p <= 2) ? 2 : prev_prime($p); |
46
|
3
|
|
|
|
|
11
|
return $self; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
sub iterate { |
49
|
|
|
|
|
|
|
#my $self = shift; my $p = $$self; $$self = next_prime($p); return $p; |
50
|
32
|
|
|
32
|
1
|
84
|
my $p = ${$_[0]}; |
|
32
|
|
|
|
|
40
|
|
51
|
32
|
|
|
|
|
38
|
${$_[0]} = next_prime(${$_[0]}); |
|
32
|
|
|
|
|
40
|
|
|
32
|
|
|
|
|
73
|
|
52
|
32
|
|
|
|
|
80
|
return $p; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub rewind { |
56
|
15
|
|
|
15
|
1
|
1830
|
my ($self, $start) = @_; |
57
|
15
|
|
|
|
|
29
|
$$self = 2; |
58
|
15
|
100
|
66
|
|
|
63
|
if (defined $start && $start ne '2') { |
59
|
13
|
50
|
|
|
|
77
|
Math::Prime::Util::_validate_num($start) |
60
|
|
|
|
|
|
|
|| Math::Prime::Util::_validate_positive_integer($start); |
61
|
10
|
50
|
|
|
|
96
|
$$self = next_prime($start-1) if $start > 2; |
62
|
|
|
|
|
|
|
} |
63
|
12
|
|
|
|
|
74
|
return $self; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub peek { |
67
|
0
|
|
|
0
|
1
|
0
|
return next_prime(${$_[0]}); |
|
0
|
|
|
|
|
0
|
|
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Some methods to match Math::NumSeq |
71
|
|
|
|
|
|
|
sub tell_i { |
72
|
1
|
|
|
1
|
1
|
6
|
return prime_count(${$_[0]}); |
|
1
|
|
|
|
|
10
|
|
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
sub pred { |
75
|
1
|
|
|
1
|
1
|
3
|
my($self, $n) = @_; |
76
|
1
|
|
|
|
|
18
|
return is_prime($n); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
sub ith { |
79
|
1
|
|
|
1
|
1
|
4
|
my($self, $n) = @_; |
80
|
1
|
|
|
|
|
14
|
return nth_prime($n); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
sub seek_to_i { |
83
|
1
|
|
|
1
|
1
|
3
|
my($self, $n) = @_; |
84
|
1
|
|
|
|
|
16
|
$self->rewind( nth_prime($n) ); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
sub seek_to_value { |
87
|
1
|
|
|
1
|
1
|
4
|
my($self, $n) = @_; |
88
|
1
|
|
|
|
|
3
|
$self->rewind($n); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
sub value_to_i { |
91
|
2
|
|
|
2
|
1
|
5
|
my($self, $n) = @_; |
92
|
2
|
100
|
|
|
|
11
|
return unless is_prime($n); |
93
|
1
|
|
|
|
|
7
|
return prime_count($n); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
sub value_to_i_ceil { |
96
|
1
|
|
|
1
|
1
|
4
|
my($self, $n) = @_; |
97
|
1
|
|
|
|
|
9
|
return prime_count(next_prime($n-1)); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
sub value_to_i_floor { |
100
|
1
|
|
|
1
|
1
|
3
|
my($self, $n) = @_; |
101
|
1
|
|
|
|
|
7
|
return prime_count($n); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
sub value_to_i_estimate { |
104
|
1
|
|
|
1
|
1
|
3
|
my($self, $n) = @_; |
105
|
1
|
|
|
|
|
38
|
return Math::Prime::Util::prime_count_approx($n); |
106
|
|
|
|
|
|
|
} |
107
|
1
|
|
|
1
|
1
|
5
|
sub i_start { 1 } |
108
|
1
|
|
|
1
|
1
|
10
|
sub description { "The prime numbers 2, 3, 5, 7, 11, 13, 17, etc." } |
109
|
1
|
|
|
1
|
1
|
4
|
sub values_min { 2 } |
110
|
1
|
|
|
1
|
1
|
4
|
sub values_max { undef } |
111
|
1
|
|
|
1
|
1
|
4
|
sub oeis_anum { "A000040" } |
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
__END__ |