Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. import math
  2.  
  3. triple_lim = 20
  4. triple_list = []
  5.  
  6. def genTriple(m, n):
  7.     a = m*m - n*n
  8.     b = 2*m*n
  9.     triple_list.append((a, b))
  10.  
  11. def gcd(m,n):
  12.     while n != 0:
  13.         t = n
  14.         n = m % n
  15.         m = t
  16.     return m
  17.  
  18. def lcm(x, y):
  19.     a = x
  20.     b = y
  21.     while x != y:
  22.         if x > y:
  23.             y += b
  24.         else:
  25.             x += a
  26.     return x
  27.  
  28. def genBrick(t1, t2):
  29.  
  30.     for i in range(2):
  31.         for j in range(2):
  32.                 a1 = t1[i]
  33.                 b1 = t1[(i+1)%2]
  34.                 a2 = t2[j]
  35.                 b2 = t2[(j+1)%2]
  36.                 a = lcm(a1, a2)
  37.                        
  38.                 b1 *= a/a1
  39.                 a1 = a
  40.  
  41.                 b2 *= a/a2
  42.                 a2 = a
  43.                
  44.                 if math.sqrt(b1*b1 + b2*b2)%1 == 0:
  45.                     print(a, b1, b2)   
  46.  
  47. for n in range(1,triple_lim):
  48.     for m in range(n+1, triple_lim, 2):
  49.         if gcd(m,n) == 1:
  50.             genTriple(m,n)
  51.  
  52. for i in range(len(triple_list)):
  53.     for j in range(i+1, len(triple_list)):
  54.         genBrick(triple_list[i], triple_list[j])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement