Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "ce91c649-5444-4a61-badb-d19baa962bd8", "metadata": {}, "source": [ "### Q9\n", "There exists exactly one Pythagorean triplet for which \\(a+b+c = 1000\\) . Find the product abc." ] }, { "cell_type": "code", "execution_count": 1, "id": "6225aaad-d009-437d-b6ef-da9eb5bd74b7", "metadata": {}, "outputs": [], "source": [ "def prod(x_vector):\n", " result = 1\n", " for i in x_vector:\n", " result *= i\n", " return result\n", "\n", "def pythagorean_triplet_for_sum(triplet_sum):\n", " if triplet_sum%2:\n", " return None\n", " b = triplet_sum\n", " for a in range(1,triplet_sum//2):\n", " temp_b = triplet_sum*triplet_sum//2 - triplet_sum*a\n", " b = temp_b//(triplet_sum-a)\n", " if b < a :\n", " break\n", " if temp_b % (triplet_sum-a) == 0: \n", " return [a,b,triplet_sum-a-b]\n", " return None" ] }, { "cell_type": "code", "execution_count": 2, "id": "9480fea1-e76d-41a0-9dd6-2112242ece05", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 42 µs, sys: 10 µs, total: 52 µs\n", "Wall time: 54.1 µs\n" ] }, { "data": { "text/plain": [ "31875000" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "prod(pythagorean_triplet_for_sum(1000))" ] }, { "cell_type": "code", "execution_count": null, "id": "6ca13261-6a0d-4bad-bb1b-9dc6142c1639", "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 Q8
Next Notebook:
Project Euler Q10
Loading