Skip to content

Instantly share code, notes, and snippets.

@Delation
Last active November 2, 2023 00:57
Show Gist options
  • Save Delation/8b4f198116bdc0b86420759bd48cb751 to your computer and use it in GitHub Desktop.
Save Delation/8b4f198116bdc0b86420759bd48cb751 to your computer and use it in GitHub Desktop.
Pythagorean Tripes
import math
def brute_search(recursion_depth:int = 200):
nums = list(range(recursion_depth))
i = 1
while i < len(nums):
a = nums[i]
for b in range(a, recursion_depth):
c = math.sqrt(a**2 + b**2) # Pythagorean theorem
if c.is_integer():
if b in nums[i:]:
nums.remove(b)
yield (a, b, int(c))
i += 1
def formula_search(recursion_depth:int = 50): # Not as refined - recursion_depth is much more intensive
for m in range(1, recursion_depth):
for n in range(m + 1, recursion_depth):
a = -1 * (m**2 - n**2)
b = 2 * m * n
c = m**2 + n**2
yield (a, b, c)
if __name__ == '__main__':
for search in (brute_search, formula_search):
print(search.__name__)
for triple in search():
print(triple)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment