Solution 5

This commit is contained in:
Fabian Schmidt 2024-09-16 12:42:55 +02:00
parent ba623894f1
commit 3302b9d48b

23
src/bin/problem_5.rs Normal file
View File

@ -0,0 +1,23 @@
fn main() {
let result = square_of_sum(100) - sum_of_squares(100);
println!("Result: {}", result);
}
fn square_of_sum(range: i64) -> i64 {
(1..=range).sum::<i64>().pow(2)
}
fn sum_of_squares(range: i64) -> i64 {
(1..=range).map(|n| n.pow(2)).sum()
}
#[cfg(test)]
mod tests {
use crate::{square_of_sum, sum_of_squares};
#[test]
fn it_works() {
let result = square_of_sum(10) - sum_of_squares(10);
assert_eq!(result, 2640);
}
}