Rust Implementation of Problem 6

View source code here on GitHub!

Problem Solution

pub fn problems::p0006::p0006() -> utils::Answer
 1/*
 2Project Euler Problem 6
 3
 4This turned out to be really easy
 5
 6Problem:
 7
 8The sum of the squares of the first ten natural numbers is,
 91**2 + 2**2 + ... + 10**2 = 385
10
11The square of the sum of the first ten natural numbers is,
12(1 + 2 + ... + 10)**2 = 55**2 = 3025
13
14Hence the difference between the sum of the squares of the first ten natural
15numbers and the square of the sum is 3025 − 385 = 2640.
16
17Find the difference between the sum of the squares of the first one hundred
18natural numbers and the square of the sum.
19*/
20use crate::include::utils::Answer;
21
22pub fn p0006() -> Answer {
23    let group = 1..101;
24    let sum_of_squares = group.clone().fold(0, |x, y| x + y * y);
25    let sum: u32 = group.sum();
26    let square_of_sum = sum * sum;
27    return Answer::Int((square_of_sum - sum_of_squares).into());
28}

Tags: arithmetic-progression, sequence-summation