{ "cells": [ { "cell_type": "markdown", "id": "a39ba505", "metadata": {}, "source": [ "# [Sum Square Difference](https://projecteuler.net/problem=6)\n", "\n", "In [problem 1](https://projecteuler.net/problem=1), we applied the following formula for [triangular numbers](https://en.wikipedia.org/wiki/Triangular_number):\n", "$$1 + 2 + 3 + \\cdots + n = \\frac{n(n+1)}{2}$$\n", "We can apply it again here and determine that\n", "$$(1 + 2 + 3 + \\cdots + 100)^2 = \\left(\\frac{100(101)}{2}\\right)^2 = 25502500$$\n", "\n", "A similar formula exists for computing sums of squares, also called the [square pyramidal numbers](https://en.wikipedia.org/wiki/Square_pyramidal_number):\n", "$$1^2 + 2^2 + 3^2 + \\cdots + n^2 = \\frac{n(n+1)(2n+1)}{6}$$\n", "(In fact, [Faulhaber's formula](https://en.wikipedia.org/wiki/Faulhaber%27s_formula) gives a formula for the sum of $k$th powers, but we obviously only need the cases $k=1$ and $k=2$ for this problem.) Consequently,\n", "$$1^2 + 2^2 + 3^2 + \\cdots + 100^2 = \\frac{100(101)(201)}{6} = 338350$$\n", "\n", "Therefore, the difference is $25502500 - 338350 = 25164150$.\n", "\n", "## Relevant sequences\n", "* Triangular numbers: [A000217](https://oeis.org/A000217)\n", "* Square pyramidal numbers: [A000330](https://oeis.org/A000330)\n", "\n", "#### Copyright (C) 2025 filifa\n", "\n", "This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)." ] } ], "metadata": { "kernelspec": { "display_name": "SageMath 9.5", "language": "sage", "name": "sagemath" }, "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.11.2" } }, "nbformat": 4, "nbformat_minor": 5 }