-1
1
Imagine simple math problem. You have a number, say it's 1000. And you want to find the next multiple of 32 that follows 1000. You automatically make some simple calculations and get the result. Typical algorithm looks like this:
int number = 1000, result;
result = (1 + (number/32)) * 32; // integer division
It's kind of easy and obvious, right? And now the challenge. Make this calculation without using *
, /
and loops.
Rules:
- Don't use
*
,/
and loops. - Assume that input number is already stored in variable
n
, store result in the variabler
(already declared). - The shortest code wins.
Sample input: (already stored in n
)
1000
Sample output: (value of r
)
1024
Sample input:
31
Sample output:
32
Sample input:
32
Sample output:
64
It is assumed that n
is a positive number.
810 characters in Javascript:
r=(n|31)+1
. I'd post this as an answer, but the question is really too trivial. – r3mainer – 2014-01-07T01:40:41.787Can you confirm whether numbers that area already an exact multiple of 32 should be rounded up? E.g. 32 -> 64 ? – Paul R – 2014-01-08T10:56:32.243
@PaulR That's already covered in the third sample set. – Iszi – 2014-01-08T16:28:14.803
@squeamishossifrage I'm not familiar with the pipe operator that you (and several others) appear to be using - what exactly does that do? – Iszi – 2014-01-08T16:54:54.740
@Iszi - thanks - I missed that. It's a pity because the more common use case is rounding up to the next multiple of N. – Paul R – 2014-01-08T18:01:14.713
@Iszi:
|
is bitwise OR in many languages, son|31
just sets the LS 5 bits of an integer. – Paul R – 2014-01-08T18:02:16.717@PaulR Aww, drat. The same operator in PowerShell is
-bor
and you have to force the order of operations with parenthesis. So, it ends up being 1 character longer than the solution I already have. – Iszi – 2014-01-08T19:51:01.960Isn't the next multiple of 32 after 1000. not 1000 + (1/Infinity). Since the OP doesn't restrict the output to Integer numbers – Adam Speight – 2014-01-12T18:59:19.477