Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • 107
    the divide instruction often returns both quotient and remainder at the same time so there's no need to multiply, just q = x/y + (x % y != 0); is enough Commented Jan 25, 2014 at 11:17
  • 1
    @LưuVĩnhPhúc Seriously you need to add that as the answer. I just used that for my answer during a codility test. It worked like a charm though I am not certain how the mod part of the answer works but it did the job. Commented Aug 26, 2014 at 0:56
  • 3
    @AndreasGrapentin the answer below by Miguel Figueiredo was submitted nearly a year before Lưu Vĩnh Phúc left the comment above. While I understand how appealing and elegant Miguel's solution is, I'm not inclined to change the accepted answer at this late date. Both approaches remain sound. If you feel strongly enough about it, I suggest you show your support by up-voting Miguel's answer below. Commented Aug 26, 2014 at 2:51
  • 3
    Strange, I have not seen any sane measurement or analysis of the proposed solutions. You talk about speed on near-the-bone, but there is no discussion of architectures, pipelines, branching instructions and clock cycles. Commented Dec 18, 2016 at 19:35
  • 1
    See also: stackoverflow.com/questions/63436490/… Commented May 8, 2021 at 3:23