line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Prime::Util::PrimeIterator; |
2
|
1
|
|
|
1
|
|
10
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
47
|
|
3
|
1
|
|
|
1
|
|
9
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
81
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
BEGIN { |
6
|
1
|
|
|
1
|
|
6
|
$Math::Prime::Util::PrimeIterator::AUTHORITY = 'cpan:DANAJ'; |
7
|
1
|
|
|
|
|
35
|
$Math::Prime::Util::PrimeIterator::VERSION = '0.69'; |
8
|
|
|
|
|
|
|
} |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
10
|
use base qw( Exporter ); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
232
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw( ); |
12
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => [ @EXPORT_OK ]); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
10
|
use Math::Prime::Util qw/next_prime prev_prime is_prime prime_count nth_prime/; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
9
|
|
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
|
37
|
my ($class, $start) = @_; |
22
|
11
|
|
|
|
|
23
|
my $p = 2; |
23
|
11
|
|
|
|
|
34
|
my $self = bless \$p, $class; |
24
|
11
|
100
|
|
|
|
74
|
$self->rewind($start) if defined $start; |
25
|
8
|
|
|
|
|
43
|
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
|
33
|
sub value { ${$_[0]}; } |
|
9
|
|
|
|
|
53
|
|
37
|
|
|
|
|
|
|
sub next { |
38
|
|
|
|
|
|
|
#my $self = shift; $$self = next_prime($$self); return $self; |
39
|
214
|
|
|
214
|
1
|
477
|
${$_[0]} = next_prime(${$_[0]}); |
|
214
|
|
|
|
|
317
|
|
|
214
|
|
|
|
|
414
|
|
40
|
214
|
|
|
|
|
295
|
return $_[0]; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
sub prev { |
43
|
3
|
|
|
3
|
1
|
973
|
my $self = shift; |
44
|
3
|
|
|
|
|
11
|
my $p = $$self; |
45
|
3
|
50
|
|
|
|
21
|
$$self = ($p <= 2) ? 2 : prev_prime($p); |
46
|
3
|
|
|
|
|
15
|
return $self; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
sub iterate { |
49
|
|
|
|
|
|
|
#my $self = shift; my $p = $$self; $$self = next_prime($p); return $p; |
50
|
32
|
|
|
32
|
1
|
157
|
my $p = ${$_[0]}; |
|
32
|
|
|
|
|
73
|
|
51
|
32
|
|
|
|
|
68
|
${$_[0]} = next_prime(${$_[0]}); |
|
32
|
|
|
|
|
77
|
|
|
32
|
|
|
|
|
138
|
|
52
|
32
|
|
|
|
|
158
|
return $p; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub rewind { |
56
|
15
|
|
|
15
|
1
|
2269
|
my ($self, $start) = @_; |
57
|
15
|
|
|
|
|
42
|
$$self = 2; |
58
|
15
|
100
|
66
|
|
|
96
|
if (defined $start && $start ne '2') { |
59
|
13
|
50
|
|
|
|
118
|
Math::Prime::Util::_validate_num($start) |
60
|
|
|
|
|
|
|
|| Math::Prime::Util::_validate_positive_integer($start); |
61
|
10
|
50
|
|
|
|
144
|
$$self = next_prime($start-1) if $start > 2; |
62
|
|
|
|
|
|
|
} |
63
|
12
|
|
|
|
|
130
|
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
|
|
|
|
|
11
|
|
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
sub pred { |
75
|
1
|
|
|
1
|
1
|
3
|
my($self, $n) = @_; |
76
|
1
|
|
|
|
|
8
|
return is_prime($n); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
sub ith { |
79
|
1
|
|
|
1
|
1
|
3
|
my($self, $n) = @_; |
80
|
1
|
|
|
|
|
16
|
return nth_prime($n); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
sub seek_to_i { |
83
|
1
|
|
|
1
|
1
|
3
|
my($self, $n) = @_; |
84
|
1
|
|
|
|
|
17
|
$self->rewind( nth_prime($n) ); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
sub seek_to_value { |
87
|
1
|
|
|
1
|
1
|
4
|
my($self, $n) = @_; |
88
|
1
|
|
|
|
|
4
|
$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
|
3
|
my($self, $n) = @_; |
97
|
1
|
|
|
|
|
12
|
return prime_count(next_prime($n-1)); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
sub value_to_i_floor { |
100
|
1
|
|
|
1
|
1
|
4
|
my($self, $n) = @_; |
101
|
1
|
|
|
|
|
8
|
return prime_count($n); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
sub value_to_i_estimate { |
104
|
1
|
|
|
1
|
1
|
3
|
my($self, $n) = @_; |
105
|
1
|
|
|
|
|
34
|
return Math::Prime::Util::prime_count_approx($n); |
106
|
|
|
|
|
|
|
} |
107
|
1
|
|
|
1
|
1
|
5
|
sub i_start { 1 } |
108
|
1
|
|
|
1
|
1
|
11
|
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
|
5
|
sub oeis_anum { "A000040" } |
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
__END__ |