Last active
August 27, 2024 20:59
-
-
Save laundmo/4851be265d100fb06ec668bfeb25b8bb to your computer and use it in GitHub Desktop.
largest pythagoras triple which fits in 10000 chars
This file contains 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
# theory: starting at 3,4,5 use the "maximise" function to compute what a multiple of this triple exactly inside the limit looks like | |
# this is far from a ideal triple, as the quotient of a/b closer to 1 means a more ideal triple starting point (triangle sides which are the most equal possible) | |
# so we generate all triples with a, b=a+1, c using the method described in this link: | |
# https://math.stackexchange.com/questions/3399189/looking-for-the-best-way-to-find-pythagorean-triples-where-b-a-pm1 | |
# then we maximise the found triple and check whether its product is larger than the previous largest | |
# we stop once all triples which unmaximised fit into the limit have been checked, as any larger ones can't be used anyways | |
# largest value which will fit | |
limit = int("9" * 10_000) | |
# amount of bits, easy way to check size of a number | |
bit_limit = (limit - 1).bit_length() | |
# stores the current biggest product, a, b, c | |
max_p = (0, 0, 0, 0) | |
# use m,n method for creating triples | |
m_p = 1 | |
n_p = 0 | |
# maximise function | |
# no clue if "maximise" is the correct term | |
# multiplies a,b,c up closer to limit | |
def maximise(a, b, c): | |
part_limit = limit // c | |
ga = a * part_limit | |
gb = b * part_limit | |
gc = c * part_limit | |
return ga, gb, gc | |
while True: | |
# create m,n for next triple from previous m,n | |
m = 2 * m_p + n_p | |
n = m_p | |
# m,n method for creating triples | |
m_sq = m**2 | |
n_sq = n**2 | |
a = m_sq - n_sq | |
b = 2 * m * n | |
c = m_sq + n_sq | |
# store m,n for next loop | |
m_p = m | |
n_p = n | |
# maximise triple | |
m_a, m_b, m_c = maximise(a, b, c) | |
# calculate the product | |
prod = m_a * m_b * m_c | |
# if its bigger, store this new product | |
if prod > max_p[0]: | |
max_p = (prod, m_a, m_b, m_c) | |
# this end condition considers the original triples | |
# as we only want to stop once the maximise() function and | |
# subsequent product comparison has checked all a,b=a+1 triples up to limit | |
# only checking c as it has to be the largest | |
if c.bit_length() > bit_limit: | |
break | |
# we have our final triple | |
prod, a, b, c = max_p | |
print(str(a) + "\n\n\n") | |
print(str(b) + "\n\n\n") | |
print(str(c) + "\n\n\n") | |
print("triple char lengths:", len(str(a)), len(str(b)), len(str(c))) | |
# this is just for printing the product in truncated scientific notation | |
import decimal | |
d = decimal.Decimal(prod) | |
print("{:.10E}".format(d)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment