From 62ee0cac578ef538bc4965c78be900da31357c29 Mon Sep 17 00:00:00 2001 From: Lucas Furlanetto Pascoali Date: Sat, 20 Dec 2025 23:37:12 -0300 Subject: [PATCH] Fix calculation of "the sum of all the numbers with odd squares under 1000" in HOF.md The algorithm to "Find the sum of all the numbers with odd squares under 1000" is wrong. It was calculating the sum of odd squares under 1000. --- src/fn/hof.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/fn/hof.md b/src/fn/hof.md index 9be5b41fec..705881593a 100644 --- a/src/fn/hof.md +++ b/src/fn/hof.md @@ -26,18 +26,17 @@ fn main() { break; } else if is_odd(n_squared) { // Accumulate value, if it's odd - acc += n_squared; + acc += n; } } println!("imperative style: {}", acc); // Functional approach - let sum_of_squared_odd_numbers: u32 = - (0..).map(|n| n * n) // All natural numbers squared - .take_while(|&n_squared| n_squared < upper) // Below upper limit - .filter(|&n_squared| is_odd(n_squared)) // That are odd - .sum(); // Sum them - println!("functional style: {}", sum_of_squared_odd_numbers); + let sum: u32 = + (0..).take_while(|&n| n * n < upper) // Below upper limit + .filter(|&n| is_odd(n * n)) // That are odd + .sum(); // Sum them + println!("functional style: {}", sum); } ```