# Find next multiple of 32 that follows given number without using *, / and loops

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 variable r (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.

• 10 characters in Javascript: r=(n|31)+1. I'd post this as an answer, but the question is really too trivial. Jan 7 '14 at 1:40
• Can you confirm whether numbers that area already an exact multiple of 32 should be rounded up? E.g. 32 -> 64 ? Jan 8 '14 at 10:56
• @PaulR That's already covered in the third sample set.
– Iszi
Jan 8 '14 at 16:28
• @squeamishossifrage I'm not familiar with the pipe operator that you (and several others) appear to be using - what exactly does that do?
– Iszi
Jan 8 '14 at 16:54
• @Iszi - thanks - I missed that. It's a pity because the more common use case is rounding up to the next multiple of N. Jan 8 '14 at 18:01

# JavaScript (26 13 chars)

alert((+prompt()>>5)+1<<5)

r=(n>>5)+1<<5

And this seems to be the best javascript answer at 10 chars:

r=(n|31)+1

• Snap! Geez @Greg, you had me on that one! Jan 7 '14 at 1:02
• Both work in Ruby. Jan 8 '14 at 20:35

## GolfScript (10 chars)

32n+31~&:r


Problems this trivial aren't interesting.

• Nevertheless, also trivial problems are golfable ;-) 31n|):r for 7 chars. Jan 7 '14 at 9:33

# C (11 chars)

r=(n|31)+1;


# Python (10 chars)

r=(n|31)+1


Work for negative numbers also

• This works for Ruby too
– Siva
Jan 8 '14 at 10:56

# Mathematica - 15 13

⌈##⌉&[n+1,32]


It's quite simple if Ceiling (⌈...⌉) isn't forbidden

EXCEL (14 characters):

=n-MOD(n;32)+32


n is declared name for a an input cell. put the formula in a cell declared as 'r'. No use of / and * ...

• Wow. I think it's a sad attestation to the difficulty of a challenge, when an Excel solution is actually quite competitive with the lengths of real programming/scripting solutions.
– Iszi
Jan 8 '14 at 16:59

## R, 12

r=n+32-n%%32


This uses modulo %%, subtraction -, and addition +.

## Julia, 8

r=n|31+1


(This is based on the C solution.)

Some parentheses can be saved in Julia due to the order in which the functions are called:

:(r=n|31+1) # : returns the ATK
:(r = +(|(n,31),1))


# PowerShell: 14

### R, 40

Just for kicks, a solution that doesn't use the modulo operator either:

r=tail(which(!cbind(1:n,31:0)[,2]),1)+32


cbind(1:n,31:0) creates an array with one column going from 1 to n and another from 31 to 0. That column is recycled to fit the size of the first one. which(!cbind(1:n,31:0)[,2]) returns the indices of the elements for which the second column is 0 (0 being FALSE, !0 is TRUE), i.e. the multiples of 32. Finally we take the last one using tail and add 32.

Solution at 34 characters with rep:

r=tail(which(!rep(31:0,l=n)),1)+32

• Doesn't rep() violate the "no loops" rule?
– Iszi
Jan 8 '14 at 16:32
• This is vector recycling not looping. If you have a look at the C definition of do_rep_len (the function called internally when using rep with argument l) you will see that there is no loop (well I don't see one anyway but I m no C expert). Jan 9 '14 at 7:53
• Ok to make it more explicit that this uses vector recycling i ll make a change. Jan 9 '14 at 8:04

## x86 Machine Code - 6 bytes

Assuming that the register 'ax' is 'n', and 'bx' is 'r' then

83C81F 40 89C3


Assembled from:

or ax, 0x1F
inc ax
mov bx, ax


## Game Maker Language, 8

r=n|31+1


Yes, | is evaluated before + (see here)

### GolfScript, 7 characters

31n|):r


Takes input from variable n and puts the result into r. Note that for testing it is advisable to use another variable name since n is also part of the p and puts built-ins.

## TI-BASIC, 12

32(1+N/32)→R

• Uses / and implied multiplication, and doesn't even work due to floating point rather than integer division. Jun 8 '15 at 1:35

C 45

int a=32,b,c;b=31;c=b%a;c=a-c;b=b+c;return c;