Skip to content

Commit 62ee0ca

Browse files
authored
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.
1 parent 7d21279 commit 62ee0ca

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/fn/hof.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,17 @@ fn main() {
2626
break;
2727
} else if is_odd(n_squared) {
2828
// Accumulate value, if it's odd
29-
acc += n_squared;
29+
acc += n;
3030
}
3131
}
3232
println!("imperative style: {}", acc);
3333
3434
// Functional approach
35-
let sum_of_squared_odd_numbers: u32 =
36-
(0..).map(|n| n * n) // All natural numbers squared
37-
.take_while(|&n_squared| n_squared < upper) // Below upper limit
38-
.filter(|&n_squared| is_odd(n_squared)) // That are odd
39-
.sum(); // Sum them
40-
println!("functional style: {}", sum_of_squared_odd_numbers);
35+
let sum: u32 =
36+
(0..).take_while(|&n| n * n < upper) // Below upper limit
37+
.filter(|&n| is_odd(n * n)) // That are odd
38+
.sum(); // Sum them
39+
println!("functional style: {}", sum);
4140
}
4241
```
4342

0 commit comments

Comments
 (0)