5
5
from functools import reduce
6
6
from itertools import combinations
7
7
8
- dist = lambda d1 , d2 : math .sqrt ((d1 ** 2 )+ (d2 ** 2 ))
8
+ # a smarter distance func
9
+ dist2 = lambda s : math .sqrt (s )
10
+
9
11
# spider is at start of a
10
12
# fly is at the end of c
11
- def shortest_dist (a , b , c ):
12
- p1 = dist (a , b + c )
13
- p2 = dist (c , a + b )
14
- p3 = dist (b , a + c )
13
+ # square lookup:
14
+ square_lookup = {}
15
+ familiar_pair = {}
16
+ familiar_combinations = {}
17
+ seen_results = {}
18
+ def shortest_dist2 (a , b , c ):
19
+ if a not in square_lookup :
20
+ square_lookup [a ] = a ** 2
21
+ if b not in square_lookup :
22
+ square_lookup [b ] = b ** 2
23
+ if c not in square_lookup :
24
+ square_lookup [c ] = c ** 2
25
+ base = square_lookup [c ]+ square_lookup [b ]+ square_lookup [a ]
26
+ s1 = base + (2 * b * c )
27
+ s2 = base + (2 * a * c )
28
+ s3 = base + (2 * a * b )
29
+ if s1 not in familiar_pair :
30
+ familiar_pair [s1 ] = dist2 (s1 )
31
+ if s2 not in familiar_pair :
32
+ familiar_pair [s2 ] = dist2 (s2 )
33
+ if s3 not in familiar_pair :
34
+ familiar_pair [s3 ] = dist2 (s3 )
35
+ p1 = familiar_pair [s1 ]
36
+ p2 = familiar_pair [s2 ]
37
+ p3 = familiar_pair [s3 ]
15
38
return min (p1 , p2 , p3 )
16
39
17
40
def ncr (n , r ):
@@ -20,12 +43,6 @@ def ncr(n, r):
20
43
denom = reduce (op .mul , range (1 , r + 1 ), 1 )
21
44
return numer // denom
22
45
23
- def dim_generator (M ):
24
- for i in range (1 , M + 1 ):
25
- for j in range (1 , M + 1 ):
26
- for k in range (1 , M + 1 ):
27
- yield (i , j , k )
28
-
29
46
def dim_generator2 (M ):
30
47
for i in range (1 , M + 1 ):
31
48
for j in range (1 , M + 1 ):
@@ -38,6 +55,7 @@ def dim_generator2(M):
38
55
seen_dims = {}
39
56
total_ints = 0
40
57
upper_lim = 1000000
58
+ upper_lim = 1000
41
59
upper_lim = 500
42
60
for a ,b ,c in dim_generator2 (upper_lim ):
43
61
S = [a ,b ,c ]
@@ -46,7 +64,7 @@ def dim_generator2(M):
46
64
if dim_set in seen_dims :
47
65
continue
48
66
seen_dims [dim_set ] = True
49
- distance = shortest_dist (a ,b ,c )
67
+ distance = shortest_dist2 (a ,b ,c )
50
68
if distance .is_integer ():
51
69
total_ints += 1
52
70
print (total_ints )
0 commit comments