-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob61.py
205 lines (159 loc) · 4.37 KB
/
prob61.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
import math
def T( n ):
return ( 0.5 * n * ( n + 1 ) )
def S( n ):
return ( n * n )
def P( n ):
return ( 0.5 * n * ( 3 * n - 1 ) )
def H( n ):
return ( n * ( 2*n - 1 ) )
def Hept( n ):
return ( 0.5 * n * ( 5*n - 3 ) )
def Oct( n ):
return ( n * ( 3*n - 2 ) )
def inverseT( x ):
"""
n * ( n + 1 ) - 2*x = 0
n^2 + n - 2*x = 0
D = b^2 - 4*a*g = 1 - 4*1*(-2*x) = 1 + 8*x
x1,2 = ( -b +- sqrt( D ) ) / 2*a
= ( -1 +- sqrt( 1 + 8*x ) ) / 2
"""
return ( 0.5 * ( -1 + math.sqrt( 1 + 8*x ) ) )
def inverseS( x ):
return ( (x**0.5) )
def inverseP( x ):
"""
n * ( 3*n - 1 ) - 2*x = 0
3*n^2 - n - 2*x = 0
D = b^2 - 4*a*g = 1 - 4*3*(-2*x) = 1 + 24*x
x1,2 = ( -b +- sqrt( D ) ) / 2*a
= ( 1 +- sqrt( 1 + 24*x ) ) / 6
"""
return ( ( 1 + math.sqrt( 1 + 24*x ) ) / 6 )
def inverseH( x ):
"""
n * ( 2*n - 1 ) - x = 0
2*n^2 - n - x = 0
D = b^2 - 4*a*g = 1 - 4*2*(-x) = 1 + 8*x
x1,2 = ( -b +- sqrt( D ) ) / 2*a
= ( 1 +- sqrt( 1 + 8*x ) ) / 4
"""
return ( 0.25 * ( 1 + math.sqrt( 1 + 8*x ) ) )
def inverseHept( x ):
"""
n*(5*n - 3) = 2*x
5*n^2 - 3*n - 2*x = 0
D = b^2 - 4*a*g = 9 - 4*5*(-2*x) = 9 + 40*x
x1,2 = ( -b +- sqrt( D ) ) / 2*a
= ( 3 +- sqrt( 9 + 40*x ) ) / 10
"""
return ( ( 3 + math.sqrt( 9 + 40*x ) ) / 10 )
def inverseOct( x ):
"""
n * ( 3*n - 2 ) = x
3*n^2 - 2*n - x = 0
D = b^2 - 4*a*g = 4 - 4*3*(-x)
= 4 + 12*x
x1,2 = ( -b +- sqrt( D ) ) / 2*a
= ( 2 +- sqrt( 4 + 12*x ) ) / 6
"""
return ( ( 2 + math.sqrt( 4 + 12*x ) ) / 6 )
def isT( x ):
tmp = inverseT( x )
return ( tmp == int( tmp ) )
def isS( x ):
tmp = inverseS( x )
return ( tmp == int( tmp ) )
def isP( x ):
tmp = inverseP( x )
return ( tmp == int( tmp ) )
def isH( x ):
tmp = inverseH( x )
return ( tmp == int( tmp ) )
def isHept( x ):
tmp = inverseHept( x )
return ( tmp == int( tmp ) )
def isOct( x ):
tmp = inverseOct( x )
return ( tmp == int( tmp ) )
def join_num( i, j ):
x = str(i)
y = str(j)
while len(x) < 2:
x = "0" + x
while len(y) < 2:
y = "0" + y
return ( ( x + y ) )
def go( L, Number, Sum, Numbers ):
global Solution_Found
if Solution_Found:
return
if len ( str(Number) ) != 4:
return
if len(L) == 0:
if int(Numbers[0])/100 != int(Numbers[-1])%100:
return
if len( set(Numbers) ) == 6:
#print Numbers
print "%d" % (Sum)
Solution_Found = True
return
for i in L:
for j in range( 0, 100 ):
tmp = join_num( Number%100, j )
val = int(tmp)
if len( str(val) ) != 4:
continue
Found = False
if i == 'T':
if isT( val ):
Found = True
elif i == 'S':
if isS( val ):
Found = True
elif i == 'P':
if isP( val ):
Found = True
elif i == 'H':
if isH( val ):
Found = True
elif i == 'He':
if isHept( val ):
Found = True
elif i == 'O':
if isOct( val ):
Found = True
if Found:
sl = L.copy()
sl.remove( i )
go( sl, val, Sum + val, Numbers + [tmp] )
start_list = set( ['T','S','P','H','He','O'] )
Solution_Found = False
for i in range( 1000, 10000 ):
if Solution_Found:
break
if isT( i ):
sl = start_list.copy()
sl.remove( 'T' )
go( sl, i, i, [i] )
if isS( i ):
sl = start_list.copy()
sl.remove( 'S' )
go( sl, i, i, [i] )
if isP( i ):
sl = start_list.copy()
sl.remove( 'P' )
go( sl, i, i, [i] )
if isH( i ):
sl = start_list.copy()
sl.remove( 'H' )
go( sl, i, i, [i] )
if isHept( i ):
sl = start_list.copy()
sl.remove( 'He' )
go( sl, i, i, [i] )
if isOct( i ):
sl = start_list.copy()
sl.remove( 'O' )
go( sl, i, i, [i] )