rust-lang / rust-by-example

Learn Rust with examples (Live code editor included)
https://doc.rust-lang.org/stable/rust-by-example/
Apache License 2.0
7.03k stars 1.34k forks source link

"sum of all the squared odd numbers" vs "sum of all the odd squared numbers" #1441

Closed xenomorpheus closed 9 months ago

xenomorpheus commented 3 years ago

What needs to be fixed?

Open to interpretation but "sum of all the squared odd numbers", but I would have thought the subject is the 'odd numbers' rather than 'odd squared numbers'.

So the test or filter is is_odd(n) rather than is_odd(n_squared)

Likewise the test being "if n >= upper {" rather than n_squared.

The test is_odd(n) to be done before the calculation of n_squared, thus reducing wasted effort.

Even if the interpretation is that is_odd(n_squared) is correct, in practice we would still do these changes as I believe the only way to get an odd square is to start with an odd. Though it is midnight in my timezone and it has been a long day.

Aside we could always add 2 to n rather than 1, because I don't believe any even n won't produce an odd square.

Page(s) Affected

https://doc.rust-lang.org/beta/rust-by-example/fn/hof.html

Suggested Improvement

        if is_odd(n) {

                if n >= upper {
                    // Break loop if exceeded the upper limit
                    break;
                } 
               acc += n * n;
        }

And likewise for the functional

JohnTitor commented 3 years ago

Hey, this repo is for www.rust-lang.org and your issue should go to https://github.com/rust-lang/rust-by-example. Thanks Manish for transferring :)

marioidival commented 9 months ago

Hi, I'm closing this issue just to clean up the items that already exist. If you think this issue makes sense to stay open, please create a new issue with updated information and mention this one.

thanks!