| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# Copyright 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
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
package Math::NumSeq::HafermanCarpet; |
|
20
|
2
|
|
|
2
|
|
280209
|
use 5.004; |
|
|
2
|
|
|
|
|
500
|
|
|
|
2
|
|
|
|
|
165
|
|
|
21
|
2
|
|
|
2
|
|
21
|
use strict; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
349
|
|
|
22
|
|
|
|
|
|
|
|
|
23
|
2
|
|
|
2
|
|
15
|
use vars '$VERSION', '@ISA'; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
314
|
|
|
24
|
|
|
|
|
|
|
$VERSION = 71; |
|
25
|
|
|
|
|
|
|
|
|
26
|
2
|
|
|
2
|
|
988
|
use Math::NumSeq; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
63
|
|
|
27
|
2
|
|
|
2
|
|
750
|
use Math::NumSeq::Base::IterateIth; |
|
|
2
|
|
|
|
|
7
|
|
|
|
2
|
|
|
|
|
383
|
|
|
28
|
|
|
|
|
|
|
@ISA = ('Math::NumSeq::Base::IterateIth', |
|
29
|
|
|
|
|
|
|
'Math::NumSeq'); |
|
30
|
|
|
|
|
|
|
*_is_infinite = \&Math::NumSeq::_is_infinite; |
|
31
|
|
|
|
|
|
|
|
|
32
|
2
|
|
|
2
|
|
1050
|
use Math::NumSeq::Repdigits; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
95
|
|
|
33
|
|
|
|
|
|
|
*_digit_split_lowtohigh = \&Math::NumSeq::Repdigits::_digit_split_lowtohigh; |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# uncomment this to run the ### lines |
|
36
|
|
|
|
|
|
|
# use Smart::Comments; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
|
39
|
2
|
|
|
2
|
|
11
|
use constant description => Math::NumSeq::__('Flattened Haferman carpet.'); |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
9
|
|
|
40
|
2
|
|
|
2
|
|
9
|
use constant default_i_start => 0; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
70
|
|
|
41
|
2
|
|
|
2
|
|
9
|
use constant values_min => 0; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
75
|
|
|
42
|
2
|
|
|
2
|
|
9
|
use constant values_max => 1; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
95
|
|
|
43
|
2
|
|
|
2
|
|
10
|
use constant characteristic_integer => 1; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
74
|
|
|
44
|
2
|
|
|
2
|
|
9
|
use constant characteristic_smaller => 1; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
179
|
|
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# 000 all zeros |
|
47
|
|
|
|
|
|
|
# 001 infs are box fractal |
|
48
|
|
|
|
|
|
|
# 010 evens is plain starting from 1 |
|
49
|
|
|
|
|
|
|
# 011 odd=1 even=0 inf=0 is inverse of plain |
|
50
|
|
|
|
|
|
|
# 100 odds odd=0 even=1 inf=1 is plain starting from 0 |
|
51
|
|
|
|
|
|
|
# 101 inverse of evens |
|
52
|
|
|
|
|
|
|
# 110 inverse of infs box fractal |
|
53
|
|
|
|
|
|
|
# 111 all ones |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# start0 carpet 0 |
|
56
|
|
|
|
|
|
|
# start0 inv carpet 0 inverse |
|
57
|
|
|
|
|
|
|
# start1 carpet 1 |
|
58
|
|
|
|
|
|
|
# start1 inv carpet 1 inverse |
|
59
|
|
|
|
|
|
|
# box |
|
60
|
|
|
|
|
|
|
# box inverse |
|
61
|
|
|
|
|
|
|
|
|
62
|
2
|
|
|
|
|
18
|
use constant parameter_info_array => |
|
63
|
|
|
|
|
|
|
[ |
|
64
|
|
|
|
|
|
|
# { name => 'haferman_type', |
|
65
|
|
|
|
|
|
|
# display => Math::NumSeq::__('Type'), |
|
66
|
|
|
|
|
|
|
# type => 'enum', |
|
67
|
|
|
|
|
|
|
# default => 'array', |
|
68
|
|
|
|
|
|
|
# choices => ['array','alt','side'], |
|
69
|
|
|
|
|
|
|
# }, |
|
70
|
|
|
|
|
|
|
{ name => 'initial_value', |
|
71
|
|
|
|
|
|
|
display => Math::NumSeq::__('Initial'), |
|
72
|
|
|
|
|
|
|
type => 'integer', |
|
73
|
|
|
|
|
|
|
default => 0, |
|
74
|
|
|
|
|
|
|
minimum => 0, |
|
75
|
|
|
|
|
|
|
maximum => 1, |
|
76
|
|
|
|
|
|
|
width => 1, |
|
77
|
|
|
|
|
|
|
}, |
|
78
|
|
|
|
|
|
|
{ name => 'inverse', |
|
79
|
|
|
|
|
|
|
display => Math::NumSeq::__('Inverse'), |
|
80
|
|
|
|
|
|
|
type => 'boolean', |
|
81
|
|
|
|
|
|
|
default => 0, |
|
82
|
|
|
|
|
|
|
}, |
|
83
|
|
|
|
|
|
|
# { name => 'radix', |
|
84
|
|
|
|
|
|
|
# share_key => 'radix_3', |
|
85
|
|
|
|
|
|
|
# type => 'integer', |
|
86
|
|
|
|
|
|
|
# display => Math::NumSeq::__('Radix'), |
|
87
|
|
|
|
|
|
|
# default => 3, |
|
88
|
|
|
|
|
|
|
# minimum => 2, |
|
89
|
|
|
|
|
|
|
# width => 3, |
|
90
|
|
|
|
|
|
|
# description => Math::NumSeq::__('Radix, ie. base, for the values calculation. Default is base 3.'), |
|
91
|
|
|
|
|
|
|
# }, |
|
92
|
2
|
|
|
2
|
|
10
|
]; |
|
|
2
|
|
|
|
|
4
|
|
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# 000 all zeros |
|
97
|
|
|
|
|
|
|
# 001 infs are box fractal |
|
98
|
|
|
|
|
|
|
# 010 evens inverse of starting from 1 |
|
99
|
|
|
|
|
|
|
# 011 odd=1 even=0 inf=0 is inverse of plain |
|
100
|
|
|
|
|
|
|
# 100 odds odd=0 even=1 inf=1 is plain starting from 0 |
|
101
|
|
|
|
|
|
|
# 101 is plain starting from 1 |
|
102
|
|
|
|
|
|
|
# 110 inverse of infs box fractal |
|
103
|
|
|
|
|
|
|
# 111 all ones |
|
104
|
|
|
|
|
|
|
# |
|
105
|
|
|
|
|
|
|
# my %odd_by_type = (start0 => 1, |
|
106
|
|
|
|
|
|
|
# start1 => 1, |
|
107
|
|
|
|
|
|
|
# box => 0); |
|
108
|
|
|
|
|
|
|
# my %inf_by_type = (start0 => 0, |
|
109
|
|
|
|
|
|
|
# start1 => 1, |
|
110
|
|
|
|
|
|
|
# box => 1); |
|
111
|
|
|
|
|
|
|
sub ith { |
|
112
|
1777
|
|
|
1777
|
1
|
2855
|
my ($self, $i) = @_; |
|
113
|
|
|
|
|
|
|
### ith(): $i |
|
114
|
|
|
|
|
|
|
|
|
115
|
1777
|
100
|
66
|
|
|
6433
|
if ($i < 0 || _is_infinite($i)) { # don't loop forever if $i is +infinity |
|
116
|
9
|
|
|
|
|
23
|
return undef; |
|
117
|
|
|
|
|
|
|
} |
|
118
|
|
|
|
|
|
|
|
|
119
|
1768
|
|
50
|
|
|
6871
|
my $haferman_type = $self->{'haferman_type'} || 'array'; |
|
120
|
1768
|
|
50
|
|
|
6269
|
my $radix = $self->{'radix'} || 3; |
|
121
|
1768
|
|
|
|
|
1915
|
my $two_digits; |
|
122
|
1768
|
50
|
|
|
|
3506
|
if ($haferman_type eq 'array') { |
|
123
|
1768
|
50
|
|
|
|
3008
|
if ($radix & 1) { |
|
124
|
1768
|
|
|
|
|
2464
|
$radix *= $radix; |
|
125
|
|
|
|
|
|
|
} else { |
|
126
|
0
|
|
|
|
|
0
|
$two_digits = 1; |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
} else { |
|
129
|
0
|
|
|
|
|
0
|
$two_digits = ($haferman_type eq 'alt'); |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
1768
|
|
|
|
|
2358
|
my $value = 0; # position even or odd |
|
133
|
1768
|
|
|
|
|
1889
|
for (;;) { |
|
134
|
3145
|
100
|
|
|
|
5294
|
if ($i) { |
|
135
|
2609
|
|
|
|
|
4804
|
my $digit = _divrem_mutate($i,$radix); |
|
136
|
2609
|
50
|
|
|
|
5259
|
if ($two_digits) { |
|
137
|
0
|
|
|
|
|
0
|
my $digit2 = _divrem_mutate($i,$radix); |
|
138
|
0
|
0
|
|
|
|
0
|
if ($haferman_type eq 'array') { |
|
139
|
0
|
|
|
|
|
0
|
$digit += $digit2; |
|
140
|
|
|
|
|
|
|
} |
|
141
|
|
|
|
|
|
|
} |
|
142
|
2609
|
100
|
|
|
|
4371
|
if ($digit & 1) { |
|
143
|
|
|
|
|
|
|
# stop at odd digit |
|
144
|
1232
|
100
|
|
|
|
1949
|
if ($value) { |
|
145
|
466
|
|
|
|
|
590
|
$value = 0; # even position value=0 always |
|
146
|
|
|
|
|
|
|
} else { |
|
147
|
766
|
50
|
|
|
|
1495
|
$value = ($haferman_type eq 'box' ? 0 : 1); |
|
148
|
|
|
|
|
|
|
} |
|
149
|
1232
|
|
|
|
|
1580
|
last; |
|
150
|
|
|
|
|
|
|
} else { |
|
151
|
|
|
|
|
|
|
# step position across even digit |
|
152
|
1377
|
|
|
|
|
2058
|
$value ^= 1; |
|
153
|
|
|
|
|
|
|
} |
|
154
|
|
|
|
|
|
|
} else { |
|
155
|
|
|
|
|
|
|
# no more digits, all even, no odd |
|
156
|
536
|
|
|
|
|
934
|
$value = $self->{'initial_value'}; |
|
157
|
536
|
|
|
|
|
706
|
last; |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
} |
|
160
|
1768
|
100
|
|
|
|
3938
|
if ($self->{'inverse'}) { |
|
161
|
622
|
|
|
|
|
794
|
$value ^= 1; |
|
162
|
|
|
|
|
|
|
} |
|
163
|
1768
|
|
|
|
|
6531
|
return $value; |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub pred { |
|
167
|
92
|
|
|
92
|
1
|
467
|
my ($self, $value) = @_; |
|
168
|
92
|
|
66
|
|
|
515
|
return ($value == 0 || $value == 1); |
|
169
|
|
|
|
|
|
|
} |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# return $remainder, modify $n |
|
174
|
|
|
|
|
|
|
# the scalar $_[0] is modified, but if it's a BigInt then a new BigInt is made |
|
175
|
|
|
|
|
|
|
# and stored there, the bigint value is not changed |
|
176
|
|
|
|
|
|
|
sub _divrem_mutate { |
|
177
|
2609
|
|
|
2609
|
|
3015
|
my $d = $_[1]; |
|
178
|
2609
|
|
|
|
|
2935
|
my $rem; |
|
179
|
2609
|
50
|
33
|
|
|
6213
|
if (ref $_[0] && $_[0]->isa('Math::BigInt')) { |
|
180
|
0
|
|
|
|
|
0
|
($_[0], $rem) = $_[0]->copy->bdiv($d); # quot,rem in array context |
|
181
|
0
|
0
|
0
|
|
|
0
|
if (! ref $d || $d < 1_000_000) { |
|
182
|
0
|
|
|
|
|
0
|
return $rem->numify; # plain remainder if fits |
|
183
|
|
|
|
|
|
|
} |
|
184
|
|
|
|
|
|
|
} else { |
|
185
|
2609
|
|
|
|
|
3244
|
$rem = $_[0] % $d; |
|
186
|
2609
|
|
|
|
|
4889
|
$_[0] = int(($_[0]-$rem)/$d); # exact division stays in UV |
|
187
|
|
|
|
|
|
|
} |
|
188
|
2609
|
|
|
|
|
4722
|
return $rem; |
|
189
|
|
|
|
|
|
|
} |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
1; |
|
192
|
|
|
|
|
|
|
__END__ |