Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Here's how I would write the loop version of this. It does involve an optimization since we know parity only depends on n - 1.

  #include <algorithm>
  
  template <typename T>
  bool even(T n)
  {
      bool is_even{true}, is_odd{false};
      for (; n--;)
      {
          std::swap(is_even, is_odd);
      }
      return is_even;
  }


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: