Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q45\n", "It can be verified that \\(T_{285} = P_{165} = H_{143} = 40755\\).\n", "Find the next triangle number that is also pentagonal and hexagonal." ] }, { "cell_type": "code", "execution_count": 1, "id": "820c6f59-d5d1-4f67-a379-b41d8bb337ac", "metadata": {}, "outputs": [], "source": [ "def is_hexagonal(num):\n", " k = int((2*num)**0.5) + 1\n", " return True if k % 2 == 0 and (k//2)*(k-1) == num else False\n", "\n", "def is_triangle(num):\n", " k = int((2*num)**0.5)\n", " return True if k*(k+1)//2 == num else False\n", "\n", "def pentagonal_num(n):\n", " return n*(3*n-1)//2\n", "\n", "def triangle_pentagonal_hexagonal_num(min_num):\n", " i = 1\n", " while True:\n", " p_i = pentagonal_num(i)\n", " if p_i > min_num and is_hexagonal(p_i) and is_triangle(p_i):\n", " return p_i\n", " i += 1" ] }, { "cell_type": "code", "execution_count": 2, "id": "1ef770af-4b63-4d07-abef-0cbe9483e888", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 12.8 ms, sys: 0 ns, total: 12.8 ms\n", "Wall time: 12.1 ms\n" ] }, { "data": { "text/plain": [ "1533776805" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "triangle_pentagonal_hexagonal_num(40755)" ] }, { "cell_type": "code", "execution_count": 3, "id": "619ca078-6bf4-4d7e-86c2-7041982a53af", "metadata": {}, "outputs": [], "source": [ "def triangle_pentagonal_hexagonal_num_2(min_num):\n", " i = 1\n", " while True:\n", " temp = (1+(48*i*i-24*i+1)**0.5)\n", " if temp%6 == 0 and int((temp//6)*(temp//2-1)//2) > min_num:\n", " return int((temp//6)*(temp//2-1)//2) \n", " i += 1" ] }, { "cell_type": "code", "execution_count": 4, "id": "aa66c1e7-4d84-4021-90bd-5e6703dacb84", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 4.13 ms, sys: 1.26 ms, total: 5.39 ms\n", "Wall time: 5.3 ms\n" ] }, { "data": { "text/plain": [ "1533776805" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "triangle_pentagonal_hexagonal_num_2(40755)" ] }, { "cell_type": "code", "execution_count": null, "id": "48799cef-e52c-49e6-b182-358c18a5ad36", "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 Q44
Next Notebook:
Project Euler Q46
Loading