<jupyter_start><jupyter_text>Problem 6  Sum square differenceThe sum of squares of the first ten natural numbers is,$$1^2 + 2^2 + ... + 10^2 = 385$$The square of the sum if the first ten natural numbers is,$$(1 + 2 + ... + 10)^2 = 55^2 = 3025$$Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is $3025 - 385 = 2640$. Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.  SolutionThe sum of the square is,$$\sum_{i=1}^ni^2=\frac{n(n+1)(2n+1)}{6}$$The square of the sum is,$$\left(\sum_{i=1}^ni\right)^2=\frac{n^2(n+1)^2}{4}$$The difference is,$$\left(\sum_{i=1}^ni^2\right)-\left(\sum_{i=1}^ni\right)^2=\frac{n(n+1)(2n+1)}{6}-\frac{n^2(n+1)^2}{4}=\frac{n(n+1)(3n^2-n-2)}{12}$$<jupyter_code>def compute(n: int) -> int:
    return n * (n + 1) * (3 * n ** 2 - n - 2) // 12
compute(10)
compute(100)
%timeit -n 100 -r 1 -p 6 compute(100)<jupyter_output>361 ns ± 0 ns per loop (mean ± std. dev. of 1 run, 100 loops each)
{'fraction_non_alphanumeric': 0.1873198847262248, 'fraction_numerical': 0.08741594620557157, 'mean_word_length': 4.289340101522843, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '223819', 'license': 'MIT', 'n_tokens_mistral': 442, 'n_tokens_neox': 384, 'n_words': 131, 'path': 'problems/0006/solution.ipynb', 'repo_name': 'Dynortice/didactic-rotary-phone', 'size': '1041'}