(*{{3,4,8,16},{2,9}};*)

Dynamic[{j,i,g, mark}]
Clear[n,B,R,Tile,A,U,mark,T,g,x];

L=Expand[Cyclotomic[3,x^(2)]];

n=18;

B=Array[0&,Length[L]];
For[j=2, j≤Length[L], B[[j]]=Mod[L[[j,2]],n];
j++];
B⋃{}


(*{{3,4,8,16},{2,9}};*)

Dynamic[{j,i,g, mark}]
Clear[n,B,R,Tile,A,U,mark,T,g,x];

n=18;


B={0,2,4}

Length[B];

Tile=Array[0&,1000000];
R=Array[0&,n];
A=Array[200&,{n/Length[B],n,Length[B]}];
U=Array[0&,n/Length[B]];
U[[1]]=B;
mark=Array[1&,n/Length[B]+1];
T=Array[{0}&,n/Length[B]];
g=1;

For[i=1, i>0,
If[Length[U[[i]]]==n,  Tile[[g]]=T[[i]]⋃{};g=g+1;T[[i]]=T[[i-1]];mark[[i]]=1;A[[i]]=Array[200&,{n,Length[B]}];i=i-1,
For[j=1, j≤n,k=100;
If[Not[MemberQ[U[[i]],Mod[j,n]]], k=0;
For[m=1, m≤Length[B],
If[U[[i]]⋂ Mod[(j-B[[m]])+B,n]=={}, k=k+1; A[[i,j,m]]=Mod[j-B[[m]],n]];
m++]];
R[[j]]=k;
j++];
min=Min[R]; 
If[(Not[min==0]),
t=Catch[Do[If[R[[ii]]==min, Throw[ii]],{ii,n}]];
If[mark[[i]]≤Length[B],
For[b=mark[[i]], b<=Length[B],mark[[i]]=b+1;z=0;
If[Not[A[[i,t,b]]==200], U[[i+1]]=U[[i]]⋃Mod[A[[i,t,b]]+B,n]; T[[i+1]]=T[[i]]⋃{A[[i,t,b]]}; b=Length[B]+1;z=1;i=i+1;];
b++];
If[z==0, mark[[i]]=1;A[[i]]=Array[200&,{n,Length[B]}];i=i-1];
, mark[[i]]=1;A[[i]]=Array[200&,{n,Length[B]}];T[[i]]=T[[i-1]];i=i-1];
, mark[[i]]=1;A[[i]]=Array[200&,{n,Length[B]}];T[[i]]=T[[i-1]];i=i-1];
];
];

Tile=Drop[Tile⋃{},1];
Length[Tile]
Tile


(*Next i shift the elements by multiples of 18. Thus we get all tiles mod 144.*)
{{0,1,6,7,12,13},{0,3,6,9,12,15},{0,5,6,11,12,17}}

Tilea=Array[0&,100000];
h=1;
For[k=0,k≤7,
For[j=0, j≤7,
For[m=0,m≤7,
For[m2=0, m2≤7,
For[m3=0, m3≤7,
Tilea[[h]]={0,18k+1,18j+6,18m+7, 18m2+12, 18m3+13}⋃{};h=h+1;
m3++];
m2++];
m++];
j++];
k++];

Tilea=Drop[Tilea⋃{0},1];

Tileb=Array[0&,100000];
h=1;
For[k=0,k≤7,
For[j=0, j≤7,
For[m=0,m≤7,
For[m2=0, m2≤7,
For[m3=0, m3≤7,
Tileb[[h]]={0,18k+3,18j+6,18m+9, 18m2+12, 18m3+15}⋃{};h=h+1;
m3++];
m2++];
m++];
j++];
k++];

Tileb=Drop[Tileb⋃{0},1];

Tilec=Array[0&,100000];
h=1;
For[k=0,k≤7,
For[j=0, j≤7,
For[m=0,m≤7,
For[m2=0, m2≤7,
For[m3=0, m3≤7,
Tilec[[h]]={0,18k+5,18j+6,18m+11, 18m2+12, 18m3+17}⋃{};h=h+1;
m3++];
m2++];
m++];
j++];
k++];


Tile=Tilea⋃Tileb⋃Tilec;
Length[Tile]


(*Dynamic[{j,i,g, mark}]
Clear[n,B,R,Tile,A,U,mark,T,g,x];

n=144;

B={0,2,4};
B=B⋃(B+18)⋃(B+36)⋃(B+54)⋃(B+72)⋃(B+90)⋃(B+108)⋃(B+126)

Length[B];

Tile=Array[0&,1000000];
R=Array[0&,n];
A=Array[200&,{n/Length[B],n,Length[B]}];
U=Array[0&,n/Length[B]];
U[[1]]=B;
mark=Array[1&,n/Length[B]+1];
T=Array[{0}&,n/Length[B]];
g=1;

For[i=1, i>0,
If[Length[U[[i]]]==n,  Tile[[g]]=T[[i]]⋃{};g=g+1;T[[i]]=T[[i-1]];mark[[i]]=1;A[[i]]=Array[200&,{n,Length[B]}];i=i-1,
For[j=1, j≤n,k=100;
If[Not[MemberQ[U[[i]],Mod[j,n]]], k=0;
For[m=1, m≤Length[B],
If[U[[i]]⋂ Mod[(j-B[[m]])+B,n]=={}, k=k+1; A[[i,j,m]]=Mod[j-B[[m]],n]];
m++]];
R[[j]]=k;
j++];
min=Min[R]; 
If[(Not[min==0]),
t=Catch[Do[If[R[[ii]]==min, Throw[ii]],{ii,n}]];
If[mark[[i]]≤Length[B],
For[b=mark[[i]], b<=Length[B],mark[[i]]=b+1;z=0;
If[Not[A[[i,t,b]]==200], U[[i+1]]=U[[i]]⋃Mod[A[[i,t,b]]+B,n]; T[[i+1]]=T[[i]]⋃{A[[i,t,b]]}; b=Length[B]+1;z=1;i=i+1;];
b++];
If[z==0, mark[[i]]=1;A[[i]]=Array[200&,{n,Length[B]}];i=i-1];
, mark[[i]]=1;A[[i]]=Array[200&,{n,Length[B]}];T[[i]]=T[[i-1]];i=i-1];
, mark[[i]]=1;A[[i]]=Array[200&,{n,Length[B]}];T[[i]]=T[[i-1]];i=i-1];
];
];

Tile=Drop[Tile⋃{},1];
Length[Tile]
Tile

H=Array[{0}&,Length[Tile[[1]]]+1];
K=Tile[[3]];
For[j=1, j≤Length[K], 
H[[j+1]]=Mod[(K[[j]]+B)⋃ H[[j]],n];
j++];
H[[Length[K]+1]]⋃{} 
*)

n=144;
For[j=1, j≤Length[Tile], P=Tile[[j]];
For[i=2, i<=Length[P], 
If[MemberQ[Tile,Mod[P-P[[i]],n]⋃{}], Tile[[j]]=0;i=Length[P]+1];
i++];
j++]; 
Tile=Drop[Tile⋃{},1];
Length[Tile]
Tile

(*Here comes Fourier-selection old-style.*)

Dynamic[{j,v,vvv,vvvv}]

T2=Tile;

Clear[H];

Length[T2]

H=Array[0&,{Length[T2],144}];
MF=Array[0&,Length[T2]];
MT=Array[0&,Length[T2]];
For[j=1, j≤Length[T2], 
For[k=1, k≤144,
If[MemberQ[T2[[j]],Mod[k,144]],H[[j,k]]=1];
k++];
j++];

v=1;
For[j=2, j≤Length[T2],
For[k=1, k≤144,
If[(Not[Chop[Fourier[H[[1]]]][[k]]==Chop[Fourier[H[[j]]]][[k]]])∧(Chop[Fourier[H[[1]]]][[k]]==0∨Chop[Fourier[H[[j]]]][[k]]==0), MF[[v]]=H[[j]];MT[[v]]=T2[[j]]; v=v+1; k=145];
k++];
j++]; 


MT=Drop[MT⋃{},1];
MF=Drop[MF⋃{},1];
Length[MF]

MMF=Array[0&,Length[T2]];
MMT=Array[0&,Length[T2]];
vvv=1;

For[j=2, j≤Length[MF],
For[k=1, k≤144,
If[(Not[Chop[Fourier[MF[[1]]]][[k]]==Chop[Fourier[MF[[j]]]][[k]]])∧(Chop[Fourier[MF[[1]]]][[k]]==0∨Chop[Fourier[MF[[j]]]][[k]]==0), MMF[[vvv]]=MF[[j]];MMT[[vvv]]=MT[[j]]; vvv=vvv+1; k=145];
k++];
j++]; 

MMT=Drop[MMT⋃{},1];
MMF=Drop[MMF⋃{},1];
Length[MMF]

MMMF=Array[0&,Length[T2]];
MMMT=Array[0&,Length[T2]];
vvvv=1;

For[j=2, j≤Length[MMF],
For[k=1, k≤144,
If[(Not[Chop[Fourier[MMF[[1]]]][[k]]==Chop[Fourier[MMF[[j]]]][[k]]])∧(Chop[Fourier[MMF[[1]]]][[k]]==0∨Chop[Fourier[MMF[[j]]]][[k]]==0), MMMF[[vvvv]]=MMF[[j]];MMMT[[vvvv]]=MMT[[j]]; vvvv=vvvv+1; k=145];
k++];
j++]; 

MMMT=Drop[MMMT⋃{},1];
MMMF=Drop[MMMF⋃{},1];
Length[MMMF]


(*{{3,4,8,16},{2,9}};*)

M4F=Array[0&,Length[T2]];
M4T=Array[0&,Length[T2]];
v4=1;

For[j=2, j≤Length[MMMF],
For[k=1, k≤144,
If[(Not[Chop[Fourier[MMMF[[1]]]][[k]]==Chop[Fourier[MMMF[[j]]]][[k]]])∧(Chop[Fourier[MMMF[[1]]]][[k]]==0∨Chop[Fourier[MMMF[[j]]]][[k]]==0), M4F[[v4]]=MMMF[[j]];M4T[[v4]]=MMMT[[j]]; v4=v4+1; k=145];
k++];
j++]; 

M4T=Drop[M4T⋃{},1];
M4F=Drop[M4F⋃{},1];
Length[M4F]

(*{{3,4,8,16},{2,9}};*)

Dynamic[{j,v5}]
M5F=Array[0&,Length[T2]];
M5T=Array[0&,Length[T2]];
v5=1;

For[j=2, j≤Length[M4F],
For[k=1, k≤144,
If[(Not[Chop[Fourier[M4F[[1]]]][[k]]==Chop[Fourier[M4F[[j]]]][[k]]])∧(Chop[Fourier[M4F[[1]]]][[k]]==0∨Chop[Fourier[M4F[[j]]]][[k]]==0), M5F[[v5]]=M4F[[j]];M5T[[v5]]=M4T[[j]]; v5=v5+1; k=145];
k++];
j++]; 

M5T=Drop[M5T⋃{},1];
M5F=Drop[M5F⋃{},1];
Length[M5F]

(*{{3,4,8,16},{2,9}};*)

Dynamic[{j,v6}]
M6F=Array[0&,Length[T2]];
M6T=Array[0&,Length[T2]];
v6=1;

For[j=2, j≤Length[M5F],
For[k=1, k≤144,
If[(Not[Chop[Fourier[M5F[[1]]]][[k]]==Chop[Fourier[M5F[[j]]]][[k]]])∧(Chop[Fourier[M5F[[1]]]][[k]]==0∨Chop[Fourier[M5F[[j]]]][[k]]==0), M6F[[v6]]=M5F[[j]];M6T[[v6]]=M5T[[j]]; v6=v6+1; k=145];
k++];
j++]; 

M6T=Drop[M6T⋃{},1];
M6F=Drop[M6F⋃{},1];
Length[M6F]


(*{{3,4,8,16},{2,9}};*)

F1=Array[1&,144];
F2=Array[1&,144];
F3=Array[1&,144];
F4=Array[1&,144];
F5=Array[1&,144];
F6=Array[1&,144];

J={T2[[1]],MT[[1]],MMT[[1]],MMMT[[1]],M4T[[1]],M5T[[1]]}
For[j=1,j≤144,
If[Chop[Fourier[H[[1]]]][[j]]==0, F1[[j]]=0];
j++];
For[j=1,j≤144,
If[Chop[Fourier[MF[[1]]]][[j]]==0, F2[[j]]=0];
j++];
For[j=1,j≤144,
If[Chop[Fourier[MMF[[1]]]][[j]]==0, F3[[j]]=0];
j++];
For[j=1,j≤144,
If[Chop[Fourier[MMMF[[1]]]][[j]]==0, F4[[j]]=0];
j++];
For[j=1,j≤144,
If[Chop[Fourier[M4F[[1]]]][[j]]==0, F5[[j]]=0];
j++];
For[j=1,j≤144,
If[Chop[Fourier[M5F[[1]]]][[j]]==0, F6[[j]]=0];
j++];

F1
F2
F3
F4
F5
F6

(*{{3,4,8,16},{2,9}};*)

FF1=Array[0&,144];
FF2=Array[0&,144];
FF3=Array[0&,144];
FF4=Array[0&,144];
FF5=Array[0&,144];
FF6=Array[0&,144];

For[j=1,j≤144,
If[F1[[j]]==0, FF1[[j]]=j];
j++];
For[j=1,j≤144,
If[F2[[j]]==0, FF2[[j]]=j];
j++];
For[j=1,j≤144,
If[F3[[j]]==0, FF3[[j]]=j];
j++];
For[j=1,j≤144,
If[F4[[j]]==0, FF4[[j]]=j];
j++];
For[j=1,j≤144,
If[F5[[j]]==0, FF5[[j]]=j];
j++];
For[j=1,j≤144,
If[F6[[j]]==0, FF6[[j]]=j];
j++];

FF1=Drop[FF1⋃{},1]-1;
FF2=Drop[FF2⋃{},1]-1;
FF3=Drop[FF3⋃{},1]-1;
FF4=Drop[FF4⋃{},1]-1;
FF5=Drop[FF5⋃{},1]-1;
FF6=Drop[FF6⋃{},1]-1;

FF={FF1,FF2,FF3,FF4,FF5,FF6};

For[m=1,m≤6,
For[j=1,j≤Length[FF[[m]]],
FF[[m,j]]=GCD[FF[[m,j]],144];
j++];
m++];
For[j=1,j≤6,
FF[[j]]=FF[[j]]⋃{};
j++];
FF;
FT=144/FF
CFT=Array[0&,Length[FT]];
For[j=1,j≤Length[FT],
CFT[[j]]=Complement[Divisors[144],FT[[j]]];
j++];
CFT