-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpstairs.py
237 lines (209 loc) · 8.02 KB
/
pstairs.py
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
#-----------------------------------------------------------------#
# The Penrose-Staircase-Module v1.2 for PYTHON #
# (c) 2022, 2024, F. Lehr ferdinand@ferdinandlehr.de #
# https://www.ferdinandlehr.de #
# https://github.com/fl3000/Penrose-Staircase-Generator #
# #
# Module for calculating the n-th Penrose-Staircase. #
# #
# usage of this module: #
# #
# import pstairs #
# p = pstairs.PenroseStaircase(n) #
# A = p.a #
# B = p.b #
# C = p.c #
# D = p.d #
# L = p.l #
#-----------------------------------------------------------------#
import math
class PenroseStaircase:
#Calculates the number of staircases for a given (always even) stairsum.
def P2(self, g):
c=0
p=1
k=2
i=6
while(i<=g):
#print("i,g,c",i,g,c)
c=0
for j in range(p):
c+=1
#print(c)
p+=k
k=k+1
i+=2
return c
#Set of all PStairs to an upper limit : nth Stairsum
def tz(self, n):
r=0
for i in range(1, n+1):
r += self.P2(2*i+4)
return int(r)
#Calculates the nth stairsum
def NG(self, n):
return(n*2+4)
#Calculates the stairsum g for the nth Pstair
def SNP(self, n):
k=1
while(self.tz(k)<n):
k=k+1
return self.NG(k)
#calculates how many stairsums occur up to a given g (including stairsum for g)
def SBG(self, g):
n=1
while(self.NG(n)<g):
n=n+1
return n
#Calcs l of the nth PStair.
def LONP(self, n):
g=self.SNP(n)
#print("Stairsum =",g)
rpos=self.P2(g)
#print("Number of stairs with that stairsum p =", rpos)
m=int((g-4)/2) #calculate how many times d=l for a given g
#print("Position within that stairsum range rpos =", rpos)
#print("m =", m)
tpos = self.tz(self.SBG(g))#n+rpos-1
#print("Cursor position total tpos = ", tpos)
u=m
for j in range(m+1):
d=1 #divisor
l=-1 #length
for i in range(u):
l=g/d
d=d+1
tpos-=1
#print ("l =", l, "tpos=", tpos)
if (tpos+1)==n:
return l
u=u-1
# returns 3,4,4,5,5,5,6,6,6,6,...
def AINC(self, p):
n=1000000000
c=0
for i in range(1,n+1):
for k in range(1,i+1):
c+=1
if(c==p):
return i+2
#calcs a of the nth pstair
def A_of_NP(self, n):
g=self.SNP(n)
rpos=self.P2(g)
tpos = self.tz(self.SBG(g)-1)+1
#print("rpos,tpos,",rpos,tpos)
return(self.AINC(n-tpos+1))
#calcs b of the nth pstair
def B_of_NP(self, n):
a=self.A_of_NP(n)
g=self.SNP(n)
return int(((g+4)/2)-a)
# returns 2,2,3,2,3,4,2,3,4,5,2...
def CINC(self, p):
n=100000000
c=0
for i in range(1,n+1):
for k in range(1,i+1):
#print(i,k)
c+=1
if(c==p):
return k+1
#calcs c of the nth pstair
def C_of_NP(self, n):
g=self.SNP(n)
rpos=self.P2(g)
tpos = self.tz(self.SBG(g)-1)+1
#print("tpos=",tpos)
return(self.CINC(n-tpos+1))
#calcs d of the nth pstair
def D_of_NP(self, n):
a=self.A_of_NP(n)
b=self.B_of_NP(n)
c=self.C_of_NP(n)
return a+b-c
#calcs a of the nth pstair (directly by formula)
def DIRECT_A(self, n):
if (n==1):
self.a = 3
elif (n>1):
self.a = math.floor(1/6*math.sqrt(72*n - 63 - \
(12*math.floor(6**(1/3)*(n - 1)**(1/3) \
+ 1/18*6**(2/3)/(n - 1)**(1/3)) - 12)* \
math.floor(6**(1/3)*(n - 1)**(1/3) + \
1/18*6**(2/3)/(n - 1)**(1/3))*(math.floor( \
6**(1/3)*(n - 1)**(1/3) + 1/18*6**(2/3)/(n - \
1)**(1/3)) + 1)) + 1/2) + 2
return self.a
#calcs b of the nth pstair (directly by formula)
def DIRECT_B(self, n):
if (n==1):
self.b = 2
elif (n>1):
self.b = math.floor(6**(1/3)*(n - 1)**(1/3) + 1/18*6**(2/3)/(n - \
1)**(1/3)) + 2 - math.floor(1/6*math.sqrt(72*n - 63 - \
(12*math.floor(6**(1/3)*(n - 1)**(1/3) + 1/18*6**(2/3)/(n - \
1)**(1/3)) - 12)*math.floor(6**(1/3)*(n - 1)**(1/3) + \
1/18*6**(2/3)/(n - 1)**(1/3))*(math.floor(6**(1/3)*(n - \
1)**(1/3) + 1/18*6**(2/3)/(n - 1)**(1/3)) + 1)) + 1/2)
return self.b
#calcs c of the nth pstair (directly by formula)
def DIRECT_C(self, n):
if (n==1):
self.c = 2
elif (n>1):
self.c = n + 1 - 1/6*(math.floor(6**(1/3)*(n - 1)**(1/3) + \
1/18*6**(2/3)/(n - 1)**(1/3)) - 1)*math.floor(6**(1/3)*(n - \
1)**(1/3) + 1/18*6**(2/3)/(n - 1)**(1/3))*(math.floor(6**( \
1/3)*(n - 1)**(1/3) + 1/18*6**(2/3)/(n - 1)**(1/3)) + 1) - \
1/2*(math.floor(1/6*math.sqrt(72*n - 63 - (12*math.floor(6**( \
1/3)*(n - 1)**(1/3) + 1/18*6**(2/3)/(n - 1)**(1/3)) - 12)* \
math.floor(6**(1/3)*(n - 1)**(1/3) + 1/18*6**(2/3)/(n - \
1)**(1/3))*(math.floor(6**(1/3)*(n - 1)**(1/3) + 1/18*6**( \
2/3)/(n - 1)**(1/3)) + 1)) + 1/2) - 1)*math.floor(1/6* \
math.sqrt(72*n - 63 - (12*math.floor(6**(1/3)*(n - 1)**(1/3) \
+ 1/18*6**(2/3)/(n - 1)**(1/3)) - 12)*math.floor(6**(1/3)*(n - \
1)**(1/3) + 1/18*6**(2/3)/(n - 1)**(1/3))*(math.floor(\
6**(1/3)*(n - 1)**(1/3) + 1/18*6**(2/3)/(n - 1)**(1/3)) + 1)) + 1/2)
return self.c
#calcs d of the nth pstair (directly by formula)
def DIRECT_D(self, n):
self.a=self.DIRECT_A(n)
self.b=self.DIRECT_B(n)
self.c=self.DIRECT_C(n)
return self.a+self.b-self.c
#calcs a of the nth pstair (directly by formula)
def DIRECT_G(self, n):
if (n==1):
self.g = 6
elif (n>1):
self.g = 4 + 2*math.floor(6**(1/3)*(n - 1)**(1/3) + \
1/18*6**(2/3)/(n - 1)**(1/3))
return self.g
#calcs len of the nth pstair (directly by formula)
def DIRECT_L(self, n):
return self.DIRECT_G(n)/((self.DIRECT_A(n)/2) - \
(self.DIRECT_B(n)/2)-(self.DIRECT_C(n)/2)+(self.DIRECT_D(n)/2))
#calculate and print the nth pstair (v1.0)
def PStair_nth_v10(self, n):
self.a=self.A_of_NP(n)
self.b=self.B_of_NP(n)
self.c=self.C_of_NP(n)
self.d=self.a+self.b-self.c
self.g=self.SNP(n)
self.l=self.LONP(n)
#calculate and print the nth pstair (v1.1)
def PStair_nth(self, n):
self.a=self.DIRECT_A(n)
self.b=self.DIRECT_B(n)
self.c=int(self.DIRECT_C(n))
self.d=self.a+self.b-self.c
self.g=int(self.DIRECT_G(n))
self.l= self.g / ((self.a/2)-(self.b/2)-(self.c/2)+(self.d/2))
#print(n,",",self.a,",",self.b,",",int(self.c),",",int(self.d),\
#",",self.g,",",self.l)
def __init__(self, n):
self.valid=False
if (isinstance(n, int)==True and n>0):
self.PStair_nth(n)
self.valid=True