Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Problem 6: Sum Square Difference

Problem

The sum of the squares of the first ten natural numbers is,

The square of the sum of the first ten natural numbers is,

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is .

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Notes

We have the following sums:

We want to find the difference between the square of the sum of the first integers and the sum of the squares of the first integers:

Solution

See it on GitHub.

//* Problem 6: Sum Square Difference
//*
//* the sum of the squares of the first ten natural numbers is,
//* 1² + 2² + ... + 10² = 385.
//* the square of the sum of the first ten natural numbers is,
//* (1 + 2 + ... + 10)² = 55² = 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.

//! time complexity: O(1)
// see the book for implementation details
// https://euler.newty.dev/problems/6.html#notes
use euler::prelude::*;

const N: u64 = 100;

fn solve() -> Solution {
    // see the derivation in: notes/p6.rs
    solution!(N * (N + 1) * (3 * N + 2) * (N - 1) / 12)
}

problem!(
    6,
    solve,
    "537942be3eb323c507623a6a73fa87bf5aeb97b7c7422993a82aa7c15f6d9cd6"
);