File: sierpinski-arrowhead.pl

package info (click to toggle)
libmath-planepath-perl 129-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 8,100 kB
  • sloc: perl: 115,748; ansic: 299; sh: 272; lisp: 73; makefile: 13
file content (329 lines) | stat: -rw-r--r-- 9,391 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/perl -w

# Copyright 2011, 2012, 2018 Kevin Ryde

# This file is part of Math-PlanePath.
#
# Math-PlanePath is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# Math-PlanePath is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Math-PlanePath.  If not, see <http://www.gnu.org/licenses/>.


use 5.004;
use strict;
use Math::PlanePath::SierpinskiArrowhead;

# uncomment this to run the ### lines
use Smart::Comments;



{
  # turn
  # A189706 = ternary lowest non-1 and its position
  #   A189707 positions of 0s, A189708 positions of 1s
  # A156595 = ternary lowest non-2 and its position

  # GP-DEFINE  select_first_n(f,n) = {
  # GP-DEFINE    my(l=List([]), i=0);
  # GP-DEFINE    while(#l<n, if(f(i),listput(l,i)); i++);
  # GP-DEFINE    Vec(l);
  # GP-DEFINE  }

  # GP-DEFINE  A189706(n) = {  \\ but here offset 0 so n=0 first term
  # GP-DEFINE    my(ret=0);
  # GP-DEFINE    while(n%3==1, n\=3;ret=!ret);
  # GP-DEFINE    if(n%3==0,ret,!ret);
  # GP-DEFINE  }
  # vector(24,n,n--; A189706(n))  == \
  # [0,1,1, 0,0,1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1]

  use constant::defer A189707_ternary_flat => sub {
    require MyFLAT;
    require FLAT::Regex;
    return FLAT::Regex->new ('((0|1|2)* 0 | []) 1(11)* | (0|1|2)* 2(11)*')->as_dfa
      ->MyFLAT::minimize
      ->MyFLAT::set_name("A189707_ternary0");
  };
  use constant::defer A189708_ternary_flat => sub {
    require MyFLAT;
    require FLAT::Regex;
    return FLAT::Regex->new ('((0|1|2)* 0 | []) (11)* | (0|1|2)* 2 1(11)*')->as_dfa
      ->MyFLAT::minimize
      ->MyFLAT::set_name("A189708_ternary0");
  };
  use constant::defer ternary_any_flat => sub {
    require MyFLAT;
    require FLAT::Regex;
    return FLAT::Regex->new ('(0|1|2)*')->as_dfa
      ->MyFLAT::minimize
      ->MyFLAT::set_name("ternary any");
  };
  A189707_ternary_flat()->union(A189708_ternary_flat())->as_dfa
    ->equals(ternary_any_flat()) or die;

  # MyFLAT::FLAT_show_breadth(A189707_ternary_flat(),3);
  # MyFLAT::FLAT_show_breadth(A189708_ternary_flat(),3);
  # A189708_ternary_flat()->MyFLAT::reverse->MyFLAT::minimize->MyFLAT::view;

  # left = even+even or odd+odd
  my $f = FLAT::Regex->new ('(0|2)* (1 (0|2)* 1 (0|2)*)* (1|2) (00)*
                  | (0|2)* 1 (0|2)* (1 (0|2)* 1 (0|2)*)* (1|2) 0(00)*
                            ')->as_dfa
    ->MyFLAT::minimize;
  $f->MyFLAT::view;
  $f->MyFLAT::reverse->MyFLAT::minimize->MyFLAT::view;

  require Math::NumSeq::PlanePathTurn;
  require Math::BaseCnv;
  my $seq = Math::NumSeq::PlanePathTurn->new
    (planepath => 'SierpinskiArrowhead',
     turn_type => 'Left');
  foreach (1 .. 400) {
    my ($i, $value) = $seq->next;
    my $i3 = Math::BaseCnv::cnv($i,10,3);
    my $calc = $f->contains($i3) ? 1 : 0;
    my $diff = ($value == $calc ? "" : " ***");
    print "$i $i3 $value $calc$diff\n";
  }

  exit 0;
}

{
  # turn sequence

  require Math::NumSeq::PlanePathTurn;
  require Math::BaseCnv;
  my $seq = Math::NumSeq::PlanePathTurn->new
    (planepath => 'SierpinskiArrowhead',
     turn_type => 'Left');
  foreach (1 .. 400) {
    my ($i, $value) = $seq->next;
    my $i3 = Math::BaseCnv::cnv($i,10,3);
    # my $calc = calc_turnleft($i);
    my $calc = WORKING__calc_turnleft($i);
    my $diff = ($value == $calc ? "" : " ***");
    print "$i $i3 $value $calc$diff\n";
  }

  sub calc_turnleft {   # not working
    my ($n) = @_;
    my $ret = 1;
    my $flip = 0;
    while ($n && ($n % 9) == 0) {
      $n = int($n/9);
    }
    if ($n) {
      my $digit = $n % 9;
      my $flip = ($digit == 0
                  || $digit == 1     # 01
                  # || $digit == 3  # 10
                  || $digit == 5  # 12
                  || $digit == 6  # 20
                  || $digit == 7  # 21
                 );
      $ret ^= $flip;
      $n = int($n/9);
    }
    while ($n) {
      my $digit = $n % 9;
      my $flip = ($digit == 1     # 01
                  || $digit == 3  # 10
                  || $digit == 5  # 12
                  || $digit == 7  # 21
                 );
      $ret ^= $flip;
      $n = int($n/9);
    }
    return $ret;
  }

  # GP-DEFINE  CountLowZeros(n) = valuation(n,3);
  # vector(20,n, CountLowZeros(n))
  # GP-DEFINE  CountLowTwos(n) = my(ret=0); while(n%3==2, n\=3;ret++); ret;
  # GP-DEFINE  CountLowOnes(n) = my(ret=0); while(n%3==1, n\=3;ret++); ret;
  # GP-DEFINE  CountOnes(n) = vecsum(apply(d->d==1,digits(n,3)));
  # vector(20,n,n--; CountOnes(n))
  # GP-DEFINE  LowestNonZero(n) = {
  # GP-DEFINE    if(n<1,error("LowestNonZero() is for n>=1")); \
  # GP-DEFINE    (n / 3^valuation(n,3)) % 3;
  # GP-DEFINE  }
  # GP-DEFINE  LowestNonOne(n) = while((n%3)==1,n=n\3); n%3;
  # GP-DEFINE  LowestNonTwo(n) = while((n%3)==2,n=n\3); n%3;
  # GP-DEFINE  CountOnesExceptLowestNonZero(n) = {
  # GP-DEFINE    while(n && n%3==0, n/=3);
  # GP-DEFINE    CountOnes(n\3);
  # GP-DEFINE  }
  # vector(20,n,n--; CountOnes(n))

  # GP-DEFINE  turn_left(n) = ! turn_right(n);
  # GP-DEFINE  turn_right(n) = (CountOnes(n) + LowestNonZero(n) + CountLowZeros(n)) % 2;
  # GP-DEFINE  turn_right(n) = (CountOnesExceptLowestNonZero(n) + CountLowZeros(n)) % 2;
  # vector(20,n, turn_left(n))
  # vector(22,n, turn_right(n))
  # vector(15,n, turn_left(n)-turn_right(n))
  # not in OEIS: 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1
  # not in OEIS: 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0,1,1
  # not in OEIS: 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1

  # at odd and even positions
  # vector(15,n, turn_left(2*n)-turn_right(2*n))
  # vector(18,n, turn_left(2*n-1)-turn_right(2*n-1))
  # not in OEIS: 1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1
  # not in OEIS: 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, 1, -1

  # GP-Test  vector(1000,m,m--; ((LowestNonOne(m)==0)+CountLowOnes(m))%2) == \
  # GP-Test  vector(1000,m, turn_left(2*m-1))
  # GP-Test  vector(1000,m,m--; ((LowestNonOne(m)==2)+CountLowOnes(m))%2) == \
  # GP-Test  vector(1000,m,m--; turn_right(2*m+1))

  # GP-Test  vector(1000,m,m--; ((LowestNonTwo(m)==0)+CountLowTwos(m))%2) == \
  # GP-Test  vector(1000,m, turn_left(2*m))
  # GP-Test  vector(1000,m,m--; (LowestNonTwo(m)+CountLowTwos(m))%2) == \
  # GP-Test  vector(1000,m, turn_right(2*m))

  # GP-Test  vector(1000,m, (LowestNonZero(m)+CountLowZeros(m))%2) == \
  # GP-Test  vector(1000,m, turn_left(2*m))

  # GP-Test  vector(1000,n, (n + LowestNonZero(n) + CountLowZeros(n))%2) == \
  # GP-Test  vector(1000,n, turn_right(n))

  # vector(25,n, (1+LowestNonZero(n) + CountLowZeros(n))%2) 
  # is A189706 with index change low-2s -> low-0s

  # ternary
  # [ count 1 digits ] [1 or 2] [ count low 0 digits ]

  # vector(10,k, (3^k)%2)
  # vector(10,k, (2*3^k)%2)

  sub WORKING__calc_turnleft { # works
    my ($n) = @_;
    my $ret = 1;
    while ($n && ($n % 3) == 0) {
      $ret ^= 1;             # flip for trailing 0s
      $n = int($n/3);
    }
    $n = int($n/3);          # skip lowest non-0
    while ($n) {
      if (($n % 3) == 1) {   # flip for all 1s
        $ret ^= 1;
      }
      $n = int($n/3);
    }
    return $ret;
  }

  sub count_digits {
    my ($n) = @_;
    my $count = 0;
    while ($n) {
      $count++;
      $n = int($n/3);
    }
    return $count;
  }
  sub count_1_digits {
    my ($n) = @_;
    my $count = 0;
    while ($n) {
      $count += (($n % 3) == 1);
      $n = int($n/3);
    }
    return $count;
  }
  exit 0;
}


{
  # direction sequence

  # 9-17  = mirror image horizontally 3-dir
  # 18-26 = dir+2

  require Math::NumSeq::PlanePathDelta;
  require Math::BaseCnv;
  my $seq = Math::NumSeq::PlanePathDelta->new
    (planepath => 'SierpinskiArrowhead',
     delta_type => 'TDir6');
  foreach (1 .. 3**4+1) {
    my ($i, $value) = $seq->next;
    # $value %= 6;
    my $i3 = Math::BaseCnv::cnv($i,10,3);
    my $calc = calc_dir6($i);
    print "$i $i3 $value $calc\n";
  }

  sub calc_dir6 {   # works
    my ($n) = @_;
    my $dir = 1;

    while ($n) {
      if (($n % 9) == 0) {
      } elsif (($n % 9) == 1) {
        $dir = 3 - $dir;
      } elsif (($n % 9) == 2) {
        $dir = $dir + 2;

      } elsif (($n % 9) == 3) {
        $dir = 3 - $dir;
      } elsif (($n % 9) == 4) {
      } elsif (($n % 9) == 5) {
        $dir = 1 - $dir;

      } elsif (($n % 9) == 6) {
        $dir = $dir - 2;
      } elsif (($n % 9) == 7) {
        $dir = 1 - $dir;
      } elsif (($n % 9) == 8) {
      }
      $n = int($n/9);
    }
    return $dir % 6;
  }

  sub Xcalc_dir6 {  # works
    my ($n) = @_;
    my $dir = 1;

    while ($n) {
      if (($n % 3) == 0) {
      }
      if (($n % 3) == 1) {
        # mirror
        $dir = 3 - $dir;
      }
      if (($n % 3) == 2) {
        $dir = $dir + 2;
      }
      $n = int($n/3);


      if (($n % 3) == 0) {
      }
      if (($n % 3) == 1) {
        # mirror
        $dir = 3 - $dir;
      }
      if (($n % 3) == 2) {
        $dir = $dir - 2;
      }
      $n = int($n/3);
    }
    return $dir % 6;
  }
  exit 0;
}