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::PowerFlip; |
19
|
2
|
|
|
2
|
|
8929
|
use 5.004; |
|
2
|
|
|
|
|
6
|
|
20
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
15
|
|
|
2
|
|
|
|
|
67
|
|
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
2
|
|
8
|
use vars '$VERSION', '@ISA'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
129
|
|
23
|
|
|
|
|
|
|
$VERSION = 72; |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
2
|
|
477
|
use Math::NumSeq; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
37
|
|
26
|
2
|
|
|
2
|
|
332
|
use Math::NumSeq::Base::IterateIth; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
115
|
|
27
|
|
|
|
|
|
|
@ISA = ('Math::NumSeq::Base::IterateIth', |
28
|
|
|
|
|
|
|
'Math::NumSeq'); |
29
|
|
|
|
|
|
|
*_is_infinite = \&Math::NumSeq::_is_infinite; |
30
|
|
|
|
|
|
|
*_to_bigint = \&Math::NumSeq::_to_bigint; |
31
|
|
|
|
|
|
|
|
32
|
2
|
|
|
2
|
|
393
|
use Math::NumSeq::PrimeFactorCount;; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
78
|
|
33
|
|
|
|
|
|
|
*_prime_factors = \&Math::NumSeq::PrimeFactorCount::_prime_factors; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# uncomment this to run the ### lines |
36
|
|
|
|
|
|
|
#use Smart::Comments; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
2
|
|
10
|
use constant name => Math::NumSeq::__('Prime Exponent Flip'); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
6
|
|
40
|
2
|
|
|
2
|
|
9
|
use constant description => Math::NumSeq::__('Flip each prime and its exponent, so for example 3^8 -> 8^3'); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
10
|
|
41
|
2
|
|
|
2
|
|
7
|
use constant default_i_start => 1; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
72
|
|
42
|
2
|
|
|
2
|
|
6
|
use constant characteristic_increasing => 0; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
61
|
|
43
|
2
|
|
|
2
|
|
6
|
use constant characteristic_non_decreasing => 0; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
90
|
|
44
|
2
|
|
|
2
|
|
14
|
use constant characteristic_integer => 1; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
91
|
|
45
|
2
|
|
|
2
|
|
8
|
use constant characteristic_smaller => 1; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
78
|
|
46
|
2
|
|
|
2
|
|
6
|
use constant values_min => 1; # at i=1 |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
68
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
50
|
|
|
|
|
|
|
# cf A005117 squarefrees have all exponents 1 so value=1 |
51
|
|
|
|
|
|
|
# A013929 non-squarefrees have some exponent>1 so value>1 |
52
|
|
|
|
|
|
|
# A005361 product of exponents of prime factorization |
53
|
|
|
|
|
|
|
|
54
|
2
|
|
|
2
|
|
6
|
use constant oeis_anum => 'A008477'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
87
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
57
|
|
|
|
|
|
|
|
58
|
2
|
|
|
2
|
|
8
|
use constant 1.02 _UV_LIMIT => 31**2; # is value=2**31 |
|
2
|
|
|
|
|
27
|
|
|
2
|
|
|
|
|
636
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub ith { |
61
|
1079
|
|
|
1079
|
1
|
916
|
my ($self, $i) = @_; |
62
|
|
|
|
|
|
|
### PowerFlip ith(): $i |
63
|
|
|
|
|
|
|
|
64
|
1079
|
50
|
|
|
|
1446
|
if (_is_infinite($i)) { |
65
|
0
|
|
|
|
|
0
|
return $i; |
66
|
|
|
|
|
|
|
} |
67
|
1079
|
|
|
|
|
907
|
$i = abs($i); |
68
|
|
|
|
|
|
|
|
69
|
1079
|
|
|
|
|
1590
|
my ($good, @primes) = _prime_factors($i); |
70
|
1079
|
50
|
|
|
|
1570
|
return undef unless $good; |
71
|
|
|
|
|
|
|
|
72
|
1079
|
100
|
|
|
|
1350
|
if (! @primes) { |
73
|
9
|
|
|
|
|
32
|
return $i; # 0,1 unchanged |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
1070
|
|
|
|
|
816
|
my $value = 1; |
77
|
1070
|
|
|
|
|
635
|
my $log = 0; |
78
|
|
|
|
|
|
|
|
79
|
1070
|
|
|
|
|
695
|
for (;;) { |
80
|
3280
|
|
100
|
|
|
4097
|
my $p = shift @primes || last; |
81
|
2210
|
|
|
|
|
1414
|
my $count = 1; |
82
|
2210
|
|
100
|
|
|
4669
|
while (@primes && $primes[0] == $p) { |
83
|
788
|
|
|
|
|
546
|
shift @primes; |
84
|
788
|
|
|
|
|
1847
|
$count++; |
85
|
|
|
|
|
|
|
} |
86
|
2210
|
|
|
|
|
2260
|
$log += $p*log($count); |
87
|
2210
|
50
|
|
|
|
2480
|
if ($log > 31) { |
88
|
0
|
|
|
|
|
0
|
$count = _to_bigint($count); |
89
|
|
|
|
|
|
|
} |
90
|
2210
|
|
|
|
|
1812
|
$value *= $count ** $p; |
91
|
|
|
|
|
|
|
} |
92
|
1070
|
|
|
|
|
2144
|
return $value; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub pred { |
96
|
8
|
|
|
8
|
1
|
46
|
my ($self, $value) = @_; |
97
|
|
|
|
|
|
|
### PowerFlip pred(): $value |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# ! is_square_free() |
100
|
|
|
|
|
|
|
|
101
|
8
|
50
|
33
|
|
|
37
|
unless ($value >= 0 && $value <= 0xFFFF_FFFF) { |
102
|
0
|
|
|
|
|
0
|
return undef; |
103
|
|
|
|
|
|
|
} |
104
|
8
|
50
|
|
|
|
18
|
if ($value != int($value)) { |
105
|
0
|
|
|
|
|
0
|
return 0; |
106
|
|
|
|
|
|
|
} |
107
|
8
|
|
|
|
|
8
|
$value = "$value"; # numize Math::BigInt for speed |
108
|
|
|
|
|
|
|
|
109
|
8
|
100
|
|
|
|
20
|
if ($value < 2) { |
110
|
6
|
|
|
|
|
13
|
return 1; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
2
|
|
|
|
|
4
|
my $limit = sqrt($value) + 1; |
114
|
|
|
|
|
|
|
|
115
|
2
|
|
|
|
|
7
|
for (my $p = 2; $p <= $limit; $p += 2-($p==2)) { |
116
|
3
|
100
|
|
|
|
13
|
next if ($value % $p); |
117
|
|
|
|
|
|
|
### prime factor: $p |
118
|
2
|
|
|
|
|
4
|
$value /= $p; |
119
|
|
|
|
|
|
|
|
120
|
2
|
50
|
|
|
|
6
|
if (($value % $p) == 0) { |
121
|
|
|
|
|
|
|
# found a square factor |
122
|
2
|
|
|
|
|
6
|
return 1; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
$limit = sqrt($value) + 1; |
126
|
|
|
|
|
|
|
### divided out: "$p, new limit $limit" |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
### final: $value |
130
|
|
|
|
|
|
|
# $value now either 1 or a prime, no square factor found |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
return 0; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
1; |
136
|
|
|
|
|
|
|
__END__ |