-2
\$\begingroup\$

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.

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

23 Answers 23

5
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ Snap! Geez @Greg, you had me on that one! \$\endgroup\$ Jan 7, 2014 at 1:02
  • \$\begingroup\$ Both work in Ruby. \$\endgroup\$
    – steenslag
    Jan 8, 2014 at 20:35
3
\$\begingroup\$

GolfScript (10 chars)

32n+31~&:r

Problems this trivial aren't interesting.

\$\endgroup\$
1
  • 5
    \$\begingroup\$ Nevertheless, also trivial problems are golfable ;-) 31n|):r for 7 chars. \$\endgroup\$
    – Howard
    Jan 7, 2014 at 9:33
3
\$\begingroup\$

C (11 chars)

r=(n|31)+1;

Python (10 chars)

r=(n|31)+1

Work for negative numbers also

\$\endgroup\$
1
  • \$\begingroup\$ This works for Ruby too \$\endgroup\$
    – Siva
    Jan 8, 2014 at 10:56
1
\$\begingroup\$

Mathematica - 15 13

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

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

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Iszi
    Jan 8, 2014 at 16:59
1
\$\begingroup\$

R, 12

r=n+32-n%%32

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

\$\endgroup\$
1
\$\begingroup\$

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))
\$\endgroup\$
1
\$\begingroup\$

PowerShell: 14

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

 

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
2
  • 1
    \$\begingroup\$ this returns 32 for n=32 \$\endgroup\$
    – steenslag
    Jan 8, 2014 at 16:42
  • \$\begingroup\$ oops ... corrected (but longer) \$\endgroup\$
    – lebatsnok
    Jan 8, 2014 at 20:22
1
\$\begingroup\$

Python (11 chars)

r=n+32-n%32

Using modulo

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

r=(n|31)+1
\$\endgroup\$
0
\$\begingroup\$

C (16 characters):

r=((n>>5)+1)<<5;
\$\endgroup\$
0
\$\begingroup\$

Kona (21)

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

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

\$\endgroup\$
0
\$\begingroup\$

J - 11 characters

r=:2^>.2^.n
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

C - 19 characters

not the shortest, but a different method

j=i%32?i+32-i%32:i;
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
3
  • \$\begingroup\$ Doesn't rep() violate the "no loops" rule? \$\endgroup\$
    – Iszi
    Jan 8, 2014 at 16:32
  • \$\begingroup\$ 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). \$\endgroup\$
    – plannapus
    Jan 9, 2014 at 7:53
  • \$\begingroup\$ Ok to make it more explicit that this uses vector recycling i ll make a change. \$\endgroup\$
    – plannapus
    Jan 9, 2014 at 8:04
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Game Maker Language, 8

r=n|31+1

Yes, | is evaluated before + (see here)

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
-1
\$\begingroup\$

TI-BASIC, 12 

32(1+N/32)→R
\$\endgroup\$
1
  • \$\begingroup\$ Uses / and implied multiplication, and doesn't even work due to floating point rather than integer division. \$\endgroup\$
    – lirtosiast
    Jun 8, 2015 at 1:35
-2
\$\begingroup\$

C 45

int a=32,b,c;b=31;c=b%a;c=a-c;b=b+c;return c;
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Please format your answer and add language and character count as title. \$\endgroup\$
    – Howard
    Jan 8, 2014 at 9:27
  • \$\begingroup\$ 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 \$\endgroup\$
    – user14143
    Jan 8, 2014 at 9:40
  • 1
    \$\begingroup\$ Please 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. \$\endgroup\$
    – Paul R
    Jan 8, 2014 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.