# Divide a number by 3 without using *, /, +, -, % operators [closed]

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
• How should the result be rounded when positive? How when negative? Jun 22, 2019 at 22:37

# Ruby, 21 bytes

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


Try it online!

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

• bitwise operations, not tricks with strings -- is your solution not the one heavily dependent on bitwise operations? Jun 20, 2019 at 12:24
• @JonathanFrech it is. As I mentioned in answer, I think that what was initial interview question all about. Jun 20, 2019 at 13:49

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

• Technically, you're still using a forbidden operator, it's just not visible in your source. Jun 20, 2019 at 18:15
• @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 ... Jun 20, 2019 at 21:18

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

# 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!

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