{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "We know that the formula for the [sum of squares](https://en.wikipedia.org/wiki/Square_pyramidal_number) is $\\sum_{k=1}^{n} k^2 = \\frac{2n^3 + 3n^2 + n}{6}$ and that $\\sum_{k=1}^{n} k = \\frac{n*(n+1)}{2}$ (see [Arithmetic progression](https://en.wikipedia.org/wiki/Arithmetic_progression)). So we simply need to do $\\sum_{k=1}^{n}k^2 - (\\sum_{k=1}^{n}k)^2 = \\frac{3n^4 + 2n^3 - 3n^2 - 2n}{12}$" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25164150" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def equation(n):\n", " return (3*n**4 + 2*n**3 - 3*n**2 - 2*n) // 12\n", "equation(100)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.4" }, "title": "Sum square difference" }, "nbformat": 4, "nbformat_minor": 2 }