Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "10de6f9e-ad54-4574-a969-2c2dad4d7dbe", "metadata": {}, "source": [ "### Q6\n", "Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum." ] }, { "cell_type": "code", "execution_count": 1, "id": "1f89a629-be90-41be-91f8-d10ce10f553f", "metadata": {}, "outputs": [], "source": [ "def sum_of_square_first_n(n):\n", " return (n)*(n+1)*(2*n+1)//6\n", "\n", "def square_of_sum_first_n(n):\n", " return ((1+n)*n//2)**2" ] }, { "cell_type": "code", "execution_count": 2, "id": "292df797-9fe0-43ff-adad-ee64be5138ce", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 3 µs, sys: 1 µs, total: 4 µs\n", "Wall time: 4.53 µs\n" ] }, { "data": { "text/plain": [ "25164150" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "square_of_sum_first_n(100)-sum_of_square_first_n(100)" ] }, { "cell_type": "code", "execution_count": null, "id": "7663368f-9394-4353-9d83-e6b9e67b414b", "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 Q5
Next Notebook:
Project Euler Q7
Loading