line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright 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::AllPrimeFactors; |
19
|
1
|
|
|
1
|
|
4869
|
use 5.004; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
57
|
|
20
|
1
|
|
|
1
|
|
9
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
56
|
|
21
|
1
|
|
|
1
|
|
8
|
use Math::Prime::XS 0.23 'is_prime'; # version 0.23 fix for 1928099 |
|
1
|
|
|
|
|
30
|
|
|
1
|
|
|
|
|
73
|
|
22
|
1
|
|
|
1
|
|
5
|
use Math::Factor::XS 0.39 'prime_factors'; # version 0.39 for prime_factors() |
|
1
|
|
|
|
|
30
|
|
|
1
|
|
|
|
|
88
|
|
23
|
|
|
|
|
|
|
|
24
|
1
|
|
|
1
|
|
9
|
use vars '$VERSION', '@ISA'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
83
|
|
25
|
|
|
|
|
|
|
$VERSION = 71; |
26
|
1
|
|
|
1
|
|
6
|
use Math::NumSeq; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
37
|
|
27
|
|
|
|
|
|
|
@ISA = ('Math::NumSeq'); |
28
|
|
|
|
|
|
|
|
29
|
1
|
|
|
1
|
|
5
|
use Math::NumSeq 7; # v.7 for _is_infinite() |
|
1
|
|
|
|
|
18
|
|
|
1
|
|
|
|
|
48
|
|
30
|
|
|
|
|
|
|
*_is_infinite = \&Math::NumSeq::_is_infinite; |
31
|
|
|
|
|
|
|
|
32
|
1
|
|
|
1
|
|
5
|
use Math::NumSeq::Primes; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
41
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# uncomment this to run the ### lines |
35
|
|
|
|
|
|
|
#use Smart::Comments; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# use constant name => Math::NumSeq::__('All Prime Factors'); |
39
|
1
|
|
|
1
|
|
6
|
use constant description => Math::NumSeq::__('Prime factors of the integers.'); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
40
|
1
|
|
|
1
|
|
7
|
use constant default_i_start => 1; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
41
|
1
|
|
|
1
|
|
5
|
use constant characteristic_smaller => 1; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
194
|
|
42
|
|
|
|
|
|
|
|
43
|
1
|
|
|
|
|
4
|
use constant parameter_info_array => |
44
|
|
|
|
|
|
|
[ |
45
|
|
|
|
|
|
|
{ |
46
|
|
|
|
|
|
|
name => 'order', |
47
|
|
|
|
|
|
|
display => Math::NumSeq::__('Order'), |
48
|
|
|
|
|
|
|
share_key => 'order_as', |
49
|
|
|
|
|
|
|
type => 'enum', |
50
|
|
|
|
|
|
|
default => 'ascending', |
51
|
|
|
|
|
|
|
choices => ['ascending','descending'], |
52
|
|
|
|
|
|
|
choices_display => [Math::NumSeq::__('Ascending'), |
53
|
|
|
|
|
|
|
Math::NumSeq::__('Descending'), |
54
|
|
|
|
|
|
|
], |
55
|
|
|
|
|
|
|
description => Math::NumSeq::__('Order for the digits within each integer.'), |
56
|
|
|
|
|
|
|
}, |
57
|
|
|
|
|
|
|
{ name => 'multiplicity', |
58
|
|
|
|
|
|
|
display => Math::NumSeq::__('Multiplicity'), |
59
|
|
|
|
|
|
|
type => 'enum', |
60
|
|
|
|
|
|
|
choices => ['repeated','distinct'], |
61
|
|
|
|
|
|
|
choices_display => [Math::NumSeq::__('Repeated'), |
62
|
|
|
|
|
|
|
Math::NumSeq::__('Distinct'), |
63
|
|
|
|
|
|
|
], |
64
|
|
|
|
|
|
|
default => 'repeated', |
65
|
|
|
|
|
|
|
description => Math::NumSeq::__('Whether to include repeated prime factors, or only distinct prime factors.'), |
66
|
|
|
|
|
|
|
}, |
67
|
|
|
|
|
|
|
{ |
68
|
|
|
|
|
|
|
name => 'on_values', |
69
|
|
|
|
|
|
|
display => Math::NumSeq::__('On Values'), |
70
|
|
|
|
|
|
|
type => 'enum', |
71
|
|
|
|
|
|
|
default => 'all', |
72
|
|
|
|
|
|
|
choices => ['all','composites','odd','even'], |
73
|
|
|
|
|
|
|
choices_display => [Math::NumSeq::__('All'), |
74
|
|
|
|
|
|
|
Math::NumSeq::__('Composites'), |
75
|
|
|
|
|
|
|
Math::NumSeq::__('Odd'), |
76
|
|
|
|
|
|
|
Math::NumSeq::__('Even')], |
77
|
|
|
|
|
|
|
description => Math::NumSeq::__('The values to take prime factors from, either all integers or just composites or odds or evens.'), |
78
|
|
|
|
|
|
|
}, |
79
|
1
|
|
|
1
|
|
4
|
]; |
|
1
|
|
|
|
|
2
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my %values_min = (all => 2, |
82
|
|
|
|
|
|
|
composites => 2, |
83
|
|
|
|
|
|
|
odd => 3, |
84
|
|
|
|
|
|
|
even => 2); |
85
|
|
|
|
|
|
|
sub values_min { |
86
|
6
|
|
|
6
|
1
|
61
|
my ($self) = @_; |
87
|
6
|
|
|
|
|
29
|
return $values_min{$self->{'on_values'}}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
91
|
|
|
|
|
|
|
# cf A033308 - concatenate primes digits |
92
|
|
|
|
|
|
|
# A056538 - list of all divisors |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my %oeis_anum; |
95
|
|
|
|
|
|
|
$oeis_anum{'all'} ->{'repeated'}->{'ascending'} = 'A027746'; |
96
|
|
|
|
|
|
|
$oeis_anum{'composites'}->{'repeated'}->{'ascending'} = 'A109709'; |
97
|
|
|
|
|
|
|
$oeis_anum{'all'} ->{'distinct'}->{'ascending'} = 'A027748'; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub oeis_anum { |
100
|
6
|
|
|
6
|
1
|
37
|
my ($self) = @_; |
101
|
6
|
|
|
|
|
36
|
return $oeis_anum{$self->{'on_values'}} |
102
|
|
|
|
|
|
|
->{$self->{'multiplicity'}} |
103
|
|
|
|
|
|
|
->{$self->{'order'}}; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
107
|
|
|
|
|
|
|
# ENHANCE-ME: find prime factors by sieve |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
my %rewind = (all => [ n => 2-1, n_step => 1 ], |
110
|
|
|
|
|
|
|
composites => [ n => 2-1, n_step => 1 ], |
111
|
|
|
|
|
|
|
odd => [ n => 2-1, n_step => 2 ], |
112
|
|
|
|
|
|
|
even => [ n => 2-2, n_step => 2 ]); |
113
|
|
|
|
|
|
|
sub rewind { |
114
|
18
|
|
|
18
|
1
|
6937
|
my ($self) = @_; |
115
|
18
|
|
|
|
|
140
|
%$self = (%$self, |
116
|
18
|
|
|
|
|
58
|
@{$rewind{$self->{'on_values'}}}); |
117
|
18
|
|
|
|
|
66
|
$self->{'pending'} = [ ]; |
118
|
18
|
|
|
|
|
70
|
$self->{'i'} = $self->i_start; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub next { |
122
|
198
|
|
|
198
|
1
|
3605
|
my ($self) = @_; |
123
|
|
|
|
|
|
|
### AllDigits next(): $self->{'i'} |
124
|
|
|
|
|
|
|
|
125
|
198
|
|
|
|
|
222
|
my $value; |
126
|
198
|
|
|
|
|
279
|
my $pending = $self->{'pending'}; |
127
|
198
|
100
|
|
|
|
877
|
unless (defined ($value = shift @$pending)) { |
128
|
120
|
|
|
|
|
350
|
my $n = ($self->{'n'} += $self->{'n_step'}); |
129
|
|
|
|
|
|
|
|
130
|
120
|
50
|
|
|
|
247
|
if ($self->{'on_values'} eq 'composites') { |
131
|
0
|
|
|
|
|
0
|
while (is_prime($n)) { |
132
|
0
|
|
|
|
|
0
|
$n = ++$self->{'n'}; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
120
|
|
|
|
|
485
|
@$pending = prime_factors($n); |
137
|
120
|
100
|
|
|
|
280
|
if ($self->{'multiplicity'} eq 'distinct') { |
138
|
42
|
|
|
|
|
46
|
my $prev = 0; |
139
|
42
|
100
|
|
|
|
60
|
@$pending = grep { $_ == $prev ? () : ($prev=$_) } @$pending; |
|
86
|
|
|
|
|
196
|
|
140
|
|
|
|
|
|
|
} |
141
|
120
|
|
|
|
|
317
|
my $order = $self->{'order'}; |
142
|
120
|
100
|
|
|
|
220
|
if ($order eq 'descending') { |
143
|
20
|
|
|
|
|
44
|
@$pending = reverse @$pending; |
144
|
|
|
|
|
|
|
} |
145
|
120
|
|
|
|
|
194
|
$value = shift @$pending; |
146
|
|
|
|
|
|
|
} |
147
|
198
|
|
|
|
|
563
|
return ($self->{'i'}++, $value); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub pred { |
151
|
99
|
|
|
99
|
1
|
576
|
my ($self, $value) = @_; |
152
|
99
|
50
|
66
|
|
|
346
|
if ($self->{'on_values'} eq 'odd' && $value < 3) { |
153
|
0
|
|
|
|
|
0
|
return 0; |
154
|
|
|
|
|
|
|
} |
155
|
99
|
|
|
|
|
278
|
return Math::NumSeq::Primes->pred($value); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
1; |
159
|
|
|
|
|
|
|
__END__ |