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

-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 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.

gthacoder

Posted 2014-01-06T23:55:50.123

Reputation: 941

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.787

Can 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, so n|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.960

Isn'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

Answers

5

JavaScript (26 13 chars)

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

Re-read the problem statement. Here's my amended answer:

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

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

r=(n|31)+1

Greg

Posted 2014-01-06T23:55:50.123

Reputation: 167

Snap! Geez @Greg, you had me on that one! – WallyWest – 2014-01-07T01:02:22.053

Both work in Ruby. – steenslag – 2014-01-08T20:35:14.220

3

GolfScript (10 chars)

32n+31~&:r

Problems this trivial aren't interesting.

Peter Taylor

Posted 2014-01-06T23:55:50.123

Reputation: 41 901

5Nevertheless, also trivial problems are golfable ;-) 31n|):r for 7 chars. – Howard – 2014-01-07T09:33:40.017

3

C (11 chars)

r=(n|31)+1;

Python (10 chars)

r=(n|31)+1

Work for negative numbers also

AMK

Posted 2014-01-06T23:55:50.123

Reputation: 506

This works for Ruby too – Siva – 2014-01-08T10:56:59.467

1

Mathematica - 15 13

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

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

ybeltukov

Posted 2014-01-06T23:55:50.123

Reputation: 1 841

1

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 * ...

user14011

Posted 2014-01-06T23:55:50.123

Reputation: 161

1Wow. 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 – 2014-01-08T16:59:06.977

1

Python (11 chars)

r=n+32-n%32

Using modulo

Alternative (10 chars), using bitwise OR like everbody else:

r=(n|31)+1

jur

Posted 2014-01-06T23:55:50.123

Reputation: 171

1

R, 12

r=n+32-n%%32

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

Sven Hohenstein

Posted 2014-01-06T23:55:50.123

Reputation: 2 464

1

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))

Sven Hohenstein

Posted 2014-01-06T23:55:50.123

Reputation: 2 464

1

R (10 16)

r=-n%%32+n
r=-(n=n+1)%%32+n

R (recursive, long)

r<-(function (n) if(n%%32) Recall(n+1) else n)(n+1)

Python (9 14)

r=-n%32+n
n+=1
r=-n%32+n

lebatsnok

Posted 2014-01-06T23:55:50.123

Reputation: 383

1this returns 32 for n=32 – steenslag – 2014-01-08T16:42:02.637

oops ... corrected (but longer) – lebatsnok – 2014-01-08T20:22:26.373

1

PowerShell: 14

$r=$n-$n%32+32

 

Iszi

Posted 2014-01-06T23:55:50.123

Reputation: 2 369

0

C (16 characters):

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

Vatine

Posted 2014-01-06T23:55:50.123

Reputation: 161

0

Kona (21)

 r:{_(1+(x%32))%(%32)}

No *, /, nor a loop in the above.

Kyle Kanos

Posted 2014-01-06T23:55:50.123

Reputation: 4 270

0

J - 11 characters

r=:2^>.2^.n

swish

Posted 2014-01-06T23:55:50.123

Reputation: 7 484

0

C - 19 characters

not the shortest, but a different method

j=i%32?i+32-i%32:i;

Hax0r778

Posted 2014-01-06T23:55:50.123

Reputation: 39

0

J (10)

This returns n if n is a multiple of 32. So it doesn't satisfy everything I think you want.

r=:n-_32|n

If we need to be stringent on that rule, the following works.

(14)

r=:>:n-_32|>:n

cardinaliti

Posted 2014-01-06T23:55:50.123

Reputation: 71

0

Perl 6 (9 bytes)

r=n+|31+1

r and n can be declared as sigilless variables (my \variable). In Perl 6, bitwise operators (like +|)'s precedence is better, so no parens are needed.

Konrad Borowski

Posted 2014-01-06T23:55:50.123

Reputation: 11 185

0

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

plannapus

Posted 2014-01-06T23:55:50.123

Reputation: 8 610

Doesn't rep() violate the "no loops" rule? – Iszi – 2014-01-08T16:32:08.290

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).

– plannapus – 2014-01-09T07:53:09.030

Ok to make it more explicit that this uses vector recycling i ll make a change. – plannapus – 2014-01-09T08:04:48.980

0

Perl5 14

$r=($n+32)&~31

In Perl5 variables needs sigilia. $n gets added 32 and we strip the last 5 bits by anding by the inverse of 31.

Sylwester

Posted 2014-01-06T23:55:50.123

Reputation: 3 678

0

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

Robert Sørlie

Posted 2014-01-06T23:55:50.123

Reputation: 1 036

0

Game Maker Language, 8

r=n|31+1

Yes, | is evaluated before + (see here)

Timtech

Posted 2014-01-06T23:55:50.123

Reputation: 12 038

0

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.

Howard

Posted 2014-01-06T23:55:50.123

Reputation: 23 109

-1

TI-BASIC, 12 

32(1+N/32)→R

Timtech

Posted 2014-01-06T23:55:50.123

Reputation: 12 038

Uses / and implied multiplication, and doesn't even work due to floating point rather than integer division. – lirtosiast – 2015-06-08T01:35:54.477

-2

C 45

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

user14143

Posted 2014-01-06T23:55:50.123

Reputation: 9

2Please format your answer and add language and character count as title. – Howard – 2014-01-08T09:27:16.940

hi howards i am new to it can u plz help in updating and i am seeing a -1 symbol againts my post what is that – user14143 – 2014-01-08T09:40:16.207

1Please re-read the question and look at some of the other answers to see what your answer should look like both in terms of functionality and formatting. – Paul R – 2014-01-08T10:52:18.717