0

I saw the algorithms in How do I find the next multiple of 10 of any integer?, and the one I came up with doesn't seem to match any of the answers in that post, so I'm wondering if there's an issue with what I came up with below:

if(n % 10 != 0)
{
  return ((n / 10) + 1) * 10;
}

return n;

The conditional check is just in case n is a decadel number (e.g., 10, 980, etc..), in which case we return the original number.

n / 10 gives us the truncated multiple of 10 (n is an integer and n/10 performs integer division in the language I'm using (C++)), and then we simply add 1 to this and multiply by 10. Alternatively we could do n / 10 * 10 + 10

roulette01
  • 1,984
  • 2
  • 13
  • 26
  • Are you sure "`n / 10` gives us the truncated multiple of 10"? Is `/` defined such that it returns an integer? If not, you need a flooring function on `n/10`. If it is/you use a flooring function, your algorithm seems to be correct. Pay attention to negative `n`s... – iAmOren Sep 28 '20 at 14:15
  • @iAmOren I should have been more clear. `n` is an integer, and I typically use C++, so `n / 10` would have performed integer division. I think in Python, I would have to use the floor function. – roulette01 Sep 28 '20 at 14:17
  • Your alternate is not good: `n / 10 * 10 + 10` for n=10 (for example), will be 20... Perhaps: `(n -1) / 10 * 10 + 10`, or: `10 * ((n - 1) / 10 + 1)`. – iAmOren Sep 28 '20 at 14:21
  • @iAmOren But I have the `if` statement check that first checks if `n` is a decadel number. If it is it will not engage int hat operation. – roulette01 Sep 28 '20 at 14:26
  • You tagged this as language-agnostic, but `/`, and `%` are language dependent. A comment of the first was already made, but also `%` can be a modulo or a remainder operator, which gives different results for negative integers. For instance, `-1 % 10` is a different number in Python than it is in JavaScript. – trincot Sep 28 '20 at 14:33

2 Answers2

1

Depending on how division works for negative integers, your solution may not work for negative integers. But let's assume non-negative integers...

Your idea is then a variant of this answer:

int inline roundup10(int n) {
  return ((n - 1) / 10 + 1) * 10;
}

The only difference is that you do not first subtract 1 from n, and therefore you have to have an if to isolate a case using the modulo operator.

With C-style division, the quoted solution does not work with n=0, and so another answer gives a better variant:

A = (A + 9) / 10 * 10

Your solution is also a look alike of this solution:

res = (n / 10)*10 + ((n % 10) ? 10:0);

The ternary here matches your if condition. The only difference is that here the division and multiplication happen in both cases, and the conditional only applies to adding the 10 or not, while you have that addition as a 1 (before the multiplication with 10).

Surely one can think of many variations of the solutions that you can find on that Q&A page, playing with the order of operations, but it will be hard to think of something that is fundamentally different and still short and sweet.

trincot
  • 317,000
  • 35
  • 244
  • 286
0

Your code does work if n is not negative, but it's better to do it this way:

n += 9 - (n+9)%10

It has fewer operations and no branches.

You can also do it using truncating division like this

n = (n+9)/10 * 10

Note that if you're rounding up to a multiple of a binary power, then you can do the same thing with shifting and masking instead of % and /, which will work properly with negative numbers.

These methods are not safe if n is greater than MAX_INTEGER - 9. If that is a possibility, then you can do it without branches like this:

n += 9 - ((n%10)+9)%10

or using a branch:

n += 9 - (n > 10 ? n-1 : n+9) % 10
Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87