diff --git a/python/e090.py b/python/e090.py index 87c33fb..437b075 100644 --- a/python/e090.py +++ b/python/e090.py @@ -1,4 +1,3 @@ -# def choose(xs, n): """ Computes r choose n, in other words choose n from xs. diff --git a/python/e135.py b/python/e135.py new file mode 100644 index 0000000..ea0cfaf --- /dev/null +++ b/python/e135.py @@ -0,0 +1,37 @@ +def euler_135(): + n = 10**7 + threshold = 10**6 + lookup = {i: 0 for i in range(1, threshold)} + firstpositivestart = 1 + for x in range(1, n): + firstpositive = False + for dxy in range(firstpositivestart, x // 2 + 1): + z = x - 2 * dxy + if z <= 0: + break + y = x - dxy + xyz = x*x - y*y - z*z + if xyz <= 0: + continue + + # Start when xyz is already greater than 0. + if not firstpositive: + firstpositivestart = dxy + firstpositive = True + + # If xyz is greater than threshold there are no more solutions. + if xyz >= threshold: + break + lookup[xyz] += 1 + + r = 0 + for _, v in lookup.items(): + if v == 10: + r += 1 + return r + + +if __name__ == "__main__": + solution = euler_135() + print("e135.py: " + str(solution)) + assert solution == 4989 diff --git a/python/e136.py b/python/e136.py new file mode 100644 index 0000000..0fe877f --- /dev/null +++ b/python/e136.py @@ -0,0 +1,37 @@ +def euler_136(): + n = 10**8 + threshold = 50*10**6 + lookup = [0 for _ in range(0, threshold)] + firstpositivestart = 1 + for x in range(1, n): + firstpositive = False + for dxy in range(firstpositivestart, x // 2 + 1): + z = x - 2 * dxy + if z <= 0: + break + y = x - dxy + xyz = x*x - y*y - z*z + if xyz <= 0: + continue + + # Start when xyz is already greater than 0. + if not firstpositive: + firstpositivestart = dxy + firstpositive = True + + # If xyz is greater than threshold there are no more solutions. + if xyz >= threshold: + break + lookup[xyz] += 1 + + r = 0 + for v in lookup: + if v == 1: + r += 1 + return r + + +if __name__ == "__main__": + solution = euler_136() + print("e136.py: " + str(solution)) + assert solution == 2544559 diff --git a/python/e138.py b/python/e138.py new file mode 100644 index 0000000..de0548b --- /dev/null +++ b/python/e138.py @@ -0,0 +1,32 @@ +from math import gcd + + +def euler_138(): + count = 12 + r, m, n = 0, 2, 1 + while True: + if count == 0: + break + + if (m - n) % 2 == 1 and gcd(m, n) == 1: # Ensure m and n are coprime and not both odd + a = m * m - n * n + b = 2 * m * n + c = m * m + n * n + if a > b: + a, b = b, a + assert a * a + b * b == c * c + if a * 2 - 1 == b or a * 2 + 1 == b: + # print(f"{m=} {n=} l={c}") + r += c # c is L + count -= 1 + n = m + m *= 4 + m += 1 + return r + + +if __name__ == "__main__": + solution = euler_138() + print("e138.py: " + str(solution)) + assert solution == 1118049290473932 + diff --git a/python/e139.py b/python/e139.py new file mode 100644 index 0000000..471a2a2 --- /dev/null +++ b/python/e139.py @@ -0,0 +1,35 @@ +from math import gcd + + +def euler_139(): + limit = 100_000_000 + r, m, n = 0, 2, 1 + while True: + a = m * m - 1 * 1 + b = 2 * m * 1 + c = m * m + 1 * 1 + if a + b + c > limit: + return r + + for n in range(1, m): + if (m - n) % 2 == 1 and gcd(m, n) == 1: # Ensure m and n are coprime and not both odd + a = m * m - n * n + b = 2 * m * n + c = m * m + n * n + if a > b: + a, b = b, a + + ka, kb, kc = a, b, c + while ka + kb + kc < limit: + assert ka * ka + kb * kb == kc * kc + if kc % (kb - ka) == 0: + r += 1 + ka, kb, kc = ka + a, kb + b, kc + c + m += 1 + + +if __name__ == "__main__": + solution = euler_139() + print("e139.py: " + str(solution)) + assert solution == 10057761 + diff --git a/python/e141.py b/python/e141.py new file mode 100644 index 0000000..736b118 --- /dev/null +++ b/python/e141.py @@ -0,0 +1,81 @@ +from math import isqrt, gcd + + +def issqrt(n): + isq = isqrt(n) + return isq * isq == n + + +def euler_141_brute_force(): + import concurrent.futures + + def have_same_ratio(a, b, c, d): + if b == 0 or d == 0: + raise ValueError("Denominators must be non-zero") + return a * d == b * c + + def check_range(start, end): + local_sum = 0 + for i in range(start, end): + if i % 10000 == 0: + print(i) + n = i * i + for d in range(1, i): + q = n // d + r = n % d + if r == 0: + continue + assert r < d and d < q + if have_same_ratio(d, r, q, d): + print(f"{n=} {i=} {r=} {d=} {q=}") + local_sum += n + break + return local_sum + + s = 0 + m = 1_000_000 + range_size = 10_000 + + with concurrent.futures.ProcessPoolExecutor() as executor: + futures = [] + for start in range(1, m, range_size): + end = min(start + range_size, m) + futures.append(executor.submit(check_range, start, end)) + + for future in concurrent.futures.as_completed(futures): + s += future.result() + + return s + + +def euler_141(): + r = 0 + NMAX = 10**12 + a = 1 + while True: + if a**3 > NMAX: + break + for b in range(1, a): + if a**3 * b**2 > NMAX: + break + + if gcd(a, b) != 1: + continue + + c = 1 + while True: + n = a**3 * b * c**2 + c * b**2 + if n > NMAX: + break + if issqrt(n): + # print(n) + r += n + c += 1 + a += 1 + return r + + +if __name__ == "__main__": + solution = euler_141() + print("e141.py: " + str(solution)) + assert solution == 878454337159