Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <algorithm>
- namespace correct {
- struct Foo {};
- int global_variable = 0;
- int64_t solve(int n) {
- int64_t result = 0;
- for (int i = std::min(0, n); i <= std::max(0, n); ++i) {
- result += i;
- }
- return result;
- }
- } //namespace correct
- namespace fast {
- struct Foo {};
- int global_variable = 0;
- int64_t solve(int n) {
- return 1ll * n * (n + 1) / 2;
- }
- } //namespace fast
- int main() {
- for (int n : {0, 1, 2, 1'000'000}) { // C++11: 1000000
- assert(fast::solve(n) == correct::solve(n));
- }
- assert(fast::solve(-1) == correct::solve(-1)); // Oops!
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement