Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q39\n", "For which value of \\(p \\le 1000\\), is the number of solutions maximised?" ] }, { "cell_type": "code", "execution_count": 1, "id": "255771bb-8696-4c1c-aeb2-f56c3bc62237", "metadata": {}, "outputs": [], "source": [ "def max_comb_int_right_triangle(n):\n", " half_range = n//2\n", " c_squares = {i*i:i for i in range(2,half_range)}\n", " result = {}\n", " for ab_sum in range(2,half_range):\n", " for a in range(1,ab_sum//2+1):\n", " b = ab_sum-a\n", " ab_squares = a*a + b*b\n", " if ab_squares in c_squares:\n", " abc_sum = ab_sum+c_squares[ab_squares]\n", " if abc_sum > n:\n", " break\n", " else:\n", " result[abc_sum] = result.get(abc_sum,0) + 1\n", " return max(result, key=result.get)" ] }, { "cell_type": "code", "execution_count": 2, "id": "3d047ff9-52dc-4b20-b614-4192212ea5ad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 5.94 ms, sys: 1.04 ms, total: 6.98 ms\n", "Wall time: 6.71 ms\n" ] }, { "data": { "text/plain": [ "840" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "max_comb_int_right_triangle(1000)" ] }, { "cell_type": "code", "execution_count": null, "id": "cba0db36-d776-4352-a1ac-2196f6855de6", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }
Previous Notebook:
Project Euler Q38
Next Notebook:
Project Euler Q40
Loading