Last active
November 2, 2023 00:57
-
-
Save Delation/8b4f198116bdc0b86420759bd48cb751 to your computer and use it in GitHub Desktop.
Pythagorean Tripes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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