17
\$\begingroup\$

Implement a division algorithm in your favourite language which handles integer division. It need only handle positive numbers - but bonus points if it handles negative and mixed-sign division, too. Results are rounded down for fractional results.

The program may not contain the /, \, div or similar operators. It must be a routine which does not use native division capabilities of the language.

You only need to handle up to 32-bit division. Using repeated subtraction is not allowed.

Input

Take two inputs on stdin separated by new lines or spaces (your choice)

740 
2

Output

In this case, the output would be 370.

The solution which is the shortest wins.

\$\endgroup\$
12
  • \$\begingroup\$ is 740,2 also permitted for the input? ie comma separated? \$\endgroup\$
    – gnibbler
    Commented Feb 4, 2011 at 10:44
  • 1
    \$\begingroup\$ @Aurel300 It is not required to have fractional output. 1/7 may produce 0.142857... or 0. \$\endgroup\$
    – Thomas O
    Commented Feb 4, 2011 at 12:24
  • 3
    \$\begingroup\$ is using exponentials and other math functions really allowed? they use division behind the scenes, because many solutions are doing ab⁻¹ \$\endgroup\$
    – Ming-Tang
    Commented Feb 5, 2011 at 7:12
  • 2
    \$\begingroup\$ this is shortest-time but I haven't seen anyone time the code \$\endgroup\$
    – phuclv
    Commented Jun 9, 2014 at 13:45
  • 1
    \$\begingroup\$ @sergiol Most programming languages provide the division operator dividend / divisor. Some programming languages also provide the division operation with reversed operands, so dividend / divisor = divisor \ dividend. This is what Thomas wanted to exclude. \$\endgroup\$ Commented Nov 30 at 10:22

34 Answers 34

1
2
0
\$\begingroup\$

Setanta (integer arithmetic only, polynomial time WRT # bits), 129 bytes

u:=go_uimh x:=u(leigh())y:=u(leigh())t:=[]nuair-a y<=x{t+=[y]y+=y}v:=0le i idir(fad@t,0){v*=2y=t[i-1]ma y<=x{v+=1x-=y}}scriobh(v)

try-setanta.ie link

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

Perl 5 -pl, 26 + 2 = 28 bytes

/ /;$_=1x$_;$_=()=/.{$'}/g

Try it online!

Implements unary division.

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

Japt, 4 bytes

*VpJ

Try it here

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

wow, there's a lot of python answers on here. julia's pretty similar, I wonder if it wins?

Julia, 38 bytes vs @gnibbler, 73

a\b=floor(Int,1e-5+exp(log(a)-log(b)))

Try it online!

Julia, 32 bytes vs @boothby, 37

a\b=length(split("1"^a,"1"^b))-1

Try it online!

Julia, 50 bytes vs @gnibbler, 72

a\b=(z=0;[z+=(b<<i<=a-z*b)*2^i for i=32:-1:0][33])

Try it online!

Julia, 31 bytes vs @gnibbler, 41

a\b=(z=a;while b*z>a z-=1end;z)

Try it online!

Julia, 38 bytes vs @JBernardo, 70*

a\b=floor(Int,1e-5+tan(angle(b+a*im)))

Try it online!

Julia, 28 bytes vs @Justin, 37

a\b=length(ones(a)[b:b:end])

Try it online!

Julia, 35 bytes vs @cemper93, 46

a\b=(i=0;while a>=b a-=b;i+=1end;i)

Try it online!

Julia, 53 bytes vs @memo, 94

a\b=(m<n=n*b>a ? m<(m+n)>>1 : n*b+b>a ? n : n<2n;0<1)

Try it online!

Julia, 26 bytes vs @fdvfcges, 40

a\b=floor(Int,1e-9+a*b^-1)

Try it online!

\$\endgroup\$
1
2

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.