Talk:Law of cosines - triples: Difference between revisions

→‎How to verify the number of triangles for the   extra credit?: The two different python methods that I developed together
(→‎How to verify the number of triangles for the   extra credit?: The two different python methods that I developed together)
Line 44:
 
::P.S. I had actually developed a python <code>method2</code> based on dicts (and the fact that dicts are ordered by default in Python3.6). I debugged the two implementations together and they agreed when tested against each other but the Python set version was slightly faster so I only published that. I'll invetstigate further tonight. [[User:Paddy3118|Paddy3118]] ([[User talk:Paddy3118|talk]]) 09:25, 25 September 2018 (UTC)
 
===The two different python methods that I developed together===
Here you go:
<lang python>N = 13
 
def method1(N=N):
squares = [x**2 for x in range(0, N+1)]
sqrset = set(squares)
tri90, tri60, tri120 = (set() for _ in range(3))
for a in range(1, N+1):
a2 = squares[a]
for b in range(1, a + 1):
b2 = squares[b]
c2 = a2 + b2
if c2 in sqrset:
tri90.add(tuple(sorted((a, b, int(c2**0.5)))))
continue
ab = a * b
c2 -= ab
if c2 in sqrset:
tri60.add(tuple(sorted((a, b, int(c2**0.5)))))
continue
c2 += 2 * ab
if c2 in sqrset:
tri120.add(tuple(sorted((a, b, int(c2**0.5)))))
return sorted(tri90), sorted(tri60), sorted(tri120)
#%%
if __name__ == '__main__':
print(f'Integer triangular triples for sides 1..{N}:')
for angle, triples in zip([90, 60, 120], method1(N)):
print(f' {angle:3}° has {len(triples)} solutions:\n {triples}')
_, t60, _ = method1(10_000)
notsame = sum(1 for a, b, c in t60 if a != b or b != c)
print('Extra credit:', notsame)
 
def method2(N=N): # Python 3.6+
sqr2root = {x**2: x for x in range(1, N+1)}
tri90, tri60, tri120 = (set() for _ in range(3))
for a2, a in sqr2root.items():
for b2, b in sqr2root.items():
if b > a:
break
c2 = a2 + b2
if c2 in sqr2root:
tri90.add(tuple(sorted((a, b, sqr2root[c2]))))
continue
ab = a * b
c2 -= ab
if c2 in sqr2root:
tri60.add(tuple(sorted((a, b, sqr2root[c2]))))
continue
c2 += 2 * ab
if c2 in sqr2root:
tri120.add(tuple(sorted((a, b, sqr2root[c2]))))
return sorted(tri90), sorted(tri60), sorted(tri120)
#%%
if __name__ == '__main__':
tt1 = method1()
tt2 = method2()
assert tt1 == tt2
#
method1_t60 = t60
_, method2_t60, _ = method2(10_000)
assert method1_t60 == method2_t60
notsame2 = sum(1 for a, b, c in method2_t60 if a != b or b != c)
print('Extra credit:', notsame2)
assert notsame == notsame2</lang>
{{out}}
<pre>Integer triangular triples for sides 1..13:
90° has 3 solutions:
[(3, 4, 5), (5, 12, 13), (6, 8, 10)]
60° has 15 solutions:
[(1, 1, 1), (2, 2, 2), (3, 3, 3), (3, 7, 8), (4, 4, 4), (5, 5, 5), (5, 7, 8), (6, 6, 6), (7, 7, 7), (8, 8, 8), (9, 9, 9), (10, 10, 10), (11, 11, 11), (12, 12, 12), (13, 13, 13)]
120° has 2 solutions:
[(3, 5, 7), (7, 8, 13)]
Extra credit: 17806
Extra credit: 17806</pre>
 
# method1 uses sets and is slightly faster than method2 which is why I only posted it.
# method 2 uses `sqr2root` mapping squares to their square root, and furthermore, does not use floating point fractional exponation in its generation.
# the seconf `if __name__...` block computes the same stats using method2 and asserts that the answers are equivalent.
<br>
Looking again at my code, Ithink it's right, but, maybe I'm not seeing an off by one error? I am not sure.
 
I guess the next thing to do is compare Houts dict based solution and look at the differences. [[User:Paddy3118|Paddy3118]] ([[User talk:Paddy3118|talk]])
Anonymous user