49
\$\begingroup\$

Quoting this question on SO (Spoiler alert!):

This question has been asked in an Oracle interview.

How would you divide a number by 3 without using *, /, +, -, %, operators?

The number may be signed or unsigned.

The task is solvable, but see if you can write the shortest code.

Rules:

  • Perform the required integer division (/3)
  • Do not use the non-text-based operators *, /, +, -, or % (or their equivalents, such as __div__ or add()). This also applies to incrementing and decrementing operators, like i++ or i--. Use of operators for string concatenation and formatting are OK. Using these characters for different operators, such as unary - operator for negative numbers, or * to represent a pointer in C is OK as well.
  • Input value can be arbitrarily large (whatever your system can handle), both positive and negative
  • Input can be on STDIN or ARGV or entered any other way
  • Create the shortest code you can to do the above
\$\endgroup\$
  • 1
    \$\begingroup\$ How should the result be rounded when positive? How when negative? \$\endgroup\$ – dfeuer Jun 22 '19 at 22:37

66 Answers 66

0
\$\begingroup\$

Ruby, 21 bytes

->n{3.step(n,3).size}

Try it online!

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

JavaScript (bitwise), 92 bytes

Because this is interview question, I assume they expected to see implementation of division using bitwise operations, not tricks with strings. So here it is. Works for negative values as well.

f=(a,b)=>b?f(a^b,(a&b)<<1):a
x=prompt(s=0)
while(y=x>>2)s=f(s,y),x=f(x&3,y)
alert(f(s,x==3))
\$\endgroup\$
  • \$\begingroup\$ bitwise operations, not tricks with strings -- is your solution not the one heavily dependent on bitwise operations? \$\endgroup\$ – Jonathan Frech Jun 20 '19 at 12:24
  • \$\begingroup\$ @JonathanFrech it is. As I mentioned in answer, I think that what was initial interview question all about. \$\endgroup\$ – Daniil Tutubalin Jun 20 '19 at 13:49
0
\$\begingroup\$

Python 3, 29 chars

eval(str(x)+chr(47)*2+str(3))

first golf, feels cheaty to me but here goes!

other one for 44:

import numpy;print(int(numpy.mean([x,0,0])))

\$\endgroup\$
  • 2
    \$\begingroup\$ Technically, you're still using a forbidden operator, it's just not visible in your source. \$\endgroup\$ – Benjamin Urquhart Jun 20 '19 at 18:15
  • 2
    \$\begingroup\$ @BenjaminUrquhart However, this viewpoint opens a deep rabbit hole: it is unlikely that the chosen Python interpreter does not contain a forbidden character, making it -- again -- technically use one ... \$\endgroup\$ – Jonathan Frech Jun 20 '19 at 21:18
0
\$\begingroup\$

PHP, 66 bytes

<?='-'[$argn<=>0].intval(substr(base_convert($argn,10,3),0,-1),3);

Try it online!

Divides by lobbing off the least significant digit in the base 3 representation of the original number. The result is truncated (10/3 = 3), works with negative numbers and uses the PHP spaceship operator.

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

ReRegex, 22 bytes

a(_*)\1\1_*/$1/a#input

Takes IO in the form of unary _s via STDIN.

Simple program, matches three chunks of _'s as greedily as possible, and dumps out the first group. Remaining _'s are also matched away.

Try it online!

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

Scheme, 95 110 bytes

(define (h n)
  (length
   (let l ((a (iota n)))
     (if (> (length a) 2)
         (cons 1 (l (cdddr a)))
         '()))))

My original attempt using base conversion:

(define (g n)
  (string->number
   (list->string
    (reverse
     (cdr
      (reverse
       (string->list
        (number->string n 3))))))
   3))

I can't think of a better way of removing the last character from the string.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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