Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "7068c59e-e498-4445-8602-eb899fd34d69", "metadata": {}, "source": [ "### Q28\n", "What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?" ] }, { "cell_type": "code", "execution_count": 1, "id": "809df8eb-02d6-45e3-9878-8fd63d691955", "metadata": {}, "outputs": [], "source": [ "def sum_of_square_first_n(n):\n", " return (n)*(n+1)*(2*n+1)//6\n", "\n", "def sum_of_first_n(n):\n", " return (1+n)*n//2\n", "\n", "def sum_diag_odd_spiral(n):\n", " if n % 2 == 0:\n", " print(\"'n' is even.\")\n", " return None\n", " return 4*(4*sum_of_square_first_n((n-1)//2) + sum_of_first_n((n-1)//2) + ((n-1)//2)) + 1\n", " " ] }, { "cell_type": "code", "execution_count": 2, "id": "4a2cf4bf-e6d5-4d25-b405-7bbaeea3138f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 4 µs, sys: 0 ns, total: 4 µs\n", "Wall time: 5.25 µs\n" ] }, { "data": { "text/plain": [ "669171001" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "sum_diag_odd_spiral(1001)" ] }, { "cell_type": "code", "execution_count": null, "id": "0fe93571-4b97-4c22-add9-cc93660943b9", "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 Q27
Next Notebook:
Project Euler Q29
Loading