| Bytes | Lang | Time | Link |
|---|---|---|---|
| 006 | Uiua | 241213T195613Z | Joao-3 |
| 207 | jbasher2 | 241213T162953Z | madeforl |
| 614 | Javascript ES6 14 Bytes | 241213T145008Z | ThePlane |
| 011 | AWK | 241213T140210Z | xrs |
| nan | wow | 241212T211308Z | guest430 |
| 004 | Japt | 241212T231738Z | Shaggy |
| nan | Perl 5 pl | 241212T230406Z | Xcali |
| 129 | Setanta integer arithmetic only | 241202T064550Z | bb94 |
| 045 | Tcl | 241201T143403Z | sergiol |
| 054 | Pascal | 241130T101038Z | Kai Burg |
| 033 | Retina 0.7.3 | 160309T193413Z | mbomb007 |
| 043 | C | 161208T021422Z | Karl Nap |
| 037 | Python | 140404T235615Z | Justin |
| 7264 | Bash | 130920T001118Z | Hovercou |
| 040 | Python | 130918T181730Z | fdvfcges |
| 094 | Python | 130917T175320Z | memo |
| 046 | Q | 130917T143336Z | tmartin |
| 054 | Python | 130917T035238Z | user8777 |
| 026 | DC | 130916T194943Z | Fors |
| 045 | Python | 130916T190726Z | gnibbler |
| 037 | Python | 130512T030449Z | boothby |
| 004 | Smalltalk | 130507T211054Z | aka.nice |
| 006 | APL | 121014T104918Z | marinus |
| 042 | Common Lisp | 121014T011517Z | Strigoid |
| 070 | Python | 110829T011510Z | JBernard |
| 028 | Ruby 1.9 | 110810T214454Z | LBg |
| 046 | Python | 110812T151433Z | cemper93 |
| 096 | Haskell | 110806T000307Z | Thomas E |
| 077 | Scala | 110809T234917Z | user unk |
| 061 | JavaScript | 110809T031426Z | Casey Ch |
| 073 | Python | 110204T110548Z | gnibbler |
| 055 | PHP | 110205T020630Z | Kevin Br |
| 034 | Mathematica | 110205T152107Z | Dr. beli |
| 082 | PHP | 110204T133951Z | Aurel B& |
| 017 | Yabasic | 110205T020951Z | PleaseSt |
| 036 | JavaScript | 110205T015030Z | PleaseSt |
| 072 | Python | 110204T115928Z | gnibbler |
| 041 | Python | 110204T104907Z | gnibbler |
Uiua, 6 bytes(SBCS)
⌊⍜∩ₙ₂-
This uses the built-in ₙ logarithm primitive and its ⍜ under inverse to do \$\lfloor2^{\log_2a-\log_2b}\rfloor\$.
jbasher2, 244 207 bytes
-37 bytes from using the temp variable 'that' instead of 'd'
create a with type number
ask for input
set that to a
create b with type number
ask for input
set that to b
create c with type number
while that < a
add 1 by c
set that to c
multiply c by b
endwhile
output c
bruteforcing lol
Javascript (ES6): 14 Bytes
(a,b)=>a*b**-1
Explanation:
(a,b)=> // non recursive function with 2 params
a* // a * 1/b == a/b
b**-1 // b^-1 = 1/(b^1) = 1/b
I do believe this is the best and fastest method of division, especially if you are running things on the GPU.
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)))
Julia, 32 bytes vs @boothby, 37
a\b=length(split("1"^a,"1"^b))-1
Julia, 50 bytes vs @gnibbler, 72
a\b=(z=0;[z+=(b<<i<=a-z*b)*2^i for i=32:-1:0][33])
Julia, 31 bytes vs @gnibbler, 41
a\b=(z=a;while b*z>a z-=1end;z)
Julia, 38 bytes vs @JBernardo, 70*
a\b=floor(Int,1e-5+tan(angle(b+a*im)))
Julia, 28 bytes vs @Justin, 37
a\b=length(ones(a)[b:b:end])
Julia, 35 bytes vs @cemper93, 46
a\b=(i=0;while a>=b a-=b;i+=1end;i)
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)
Julia, 26 bytes vs @fdvfcges, 40
a\b=floor(Int,1e-9+a*b^-1)
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)
Pascal, 54 B
In the era Pascal was conceived, slide rules were a standard tool among engineers.
This was probably motivation to include ln (natural logarithm) and exp (exponenantion) to be part of the language.
For a non‑zero dividend and non‑zero divisor we can use the relationship
$$
\frac{\text{dividend}}{\text{divisor}} =
e^{\ln\left(\text{dividend}\right)\,-\,\ln\left(\text{divisor}\right)}
$$
function q(e,d:real):real;begin q:=exp(ln(e)−ln(d))end
−8 B if you want to deal with negative dividends and divisors, too, you need a processor supporting the built‐in real exponentiation operator ** of Extended Pascal:
function q(e,d:real):real;begin q:=e*d**−1 end
+7 B if you consider x−1 a division operation:
function q(e,d:complex):real;begin q:=re(exp(ln(e)−ln(d)))end
Note that the built‐in data type real only provides a “reasonable approximation.”
What is “reasonable” depends on the implementation.
You may get the result that
$$
740\:/\:2 = 369.\overline{9}
$$
This, however, is outside of the domain of the programming language.
Apply the built‐in function round if it can be concluded that the quotient is integral.
Retina 0.7.3, 33 bytes
Takes space-separated input with the divisor first. Dividing by zero is undefined.
\d+
$*
^(.+) (\1)+.*$
$#+
.+ .*
0
C, 43 bytes
Implements a/b in a linear search. Handles only positive arguments.
i;f(a,b){i=0;while(1)if(++i*b>a)return--i;}
Ungolfed:
i;
f(a, b){
i = 0;
while (1)
if (++i * b > a)
return --i;
}
Python, 37
x,y=input()
print len(('0'*x)[y-1::y])
Constructs a string of length x ('0'*x) and uses extended slicing to pick every yth character, starting from the index y-1. Prints the length of the resulting string.
Like Gnibbler, this takes comma separated input. Removing it costs 9 chars:
i=input
x,y=i(),i()
print len(('0'*x)[y-1::y])
Bash, 72 64 characters
read x y;yes ''|head -n$x>f;ls -l --block-size=$y f|cut -d\ -f5
Output an infinite number of newlines, take the first x, put them all into a file called f, then get the size of f in blocks the size of y. Took manatwork's advice to shave off eight characters.
Python, 40 characters
print(float(input())*float(input())**-1)
Python, 94 characters
A recursive binary search:
a,b=input()
def r(m,n):return r(m,m+n>>1)if n*b>a else n if n*b+b>a else r(n,2*n)
print r(0,1)
Q, 46
{-1+(#){x-y}[;y]\[{s[x-y]<>(s:signum)x}[y];x]}
.
q){-1+(#){x-y}[;y]\[{s[x-y]<>(s:signum)x}[y];x]}[740;2]
370
q){-1+(#){x-y}[;y]\[{s[x-y]<>(s:signum)x}[y];x]}[740;3]
246
Python 54
Takes comma delimited input.
- Makes a string of dots of length x
- Replaces segments of dots of length y with a single comma
- Counts commas.
Words because markdown dies with a list followed by code?:
x,y=input()
print("."*x).replace("."*y,',').count(',')
DC: 26 characters
?so?se0[1+dle*lo>i]dsix1-p
I do admit that it is not the fastest solution around.
Python - 45 chars
Takes comma separated input, eg 740,2
x,y=input()
print-1+len((x*'.').split('.'*y))
Python, 37
Step 1. Convert to unary.
Step 2. Unary division algorithm.
print('1'*input()).count('1'*input())
Smalltalk, Squeak 4.x flavour
define this binary message in Integer:
% d
| i |
d <= self or: [^0].
i := self highBit - d highBit.
d << i <= self or: [i := i - 1].
^1 << i + (self - (d << i) % d)
Once golfed, this quotient is still long (88 chars):
%d|i n|d<=(n:=self)or:[^0].i:=n highBit-d highBit.d<<i<=n or:[i:=i-1].^1<<i+(n-(d<<i)%d)
But it's reasonnably fast:
[0 to: 1000 do: [:n |
1 to: 1000 do: [:d |
self assert: (n//d) = (n%d)]].
] timeToRun.
-> 127 ms on my modest mac mini (8 MOp/s)
Compared to regular division:
[0 to: 1000 do: [:n |
1 to: 1000 do: [:d |
self assert: (n//d) = (n//d)]].
] timeToRun.
-> 31 ms, it's just 4 times slower
I don't count the chars to read stdin or write stdout, Squeak was not designed for scripting.
FileStream stdout nextPutAll:
FileStream stdin nextLine asNumber%FileStream stdin nextLine asNumber;
cr
Of course, more stupid repeated subtraction
%d self>d and:[^0].^self-d%d+1
or plain stupid enumeration
%d^(0to:self)findLast:[:q|q*d<=self]
could work too, but are not really interesting
APL (6)
⌊*-/⍟⎕
/ is not division here, but foldr. i.e, F/a b c is a F (b F c).
If I can't use foldr because it's called /, it can be done in 9 characters:
⌊*(⍟⎕)-⍟⎕
Explanation:
⎕:input()⍟⎕:map(log, input())-/⍟⎕:foldr1(sub, map(log, input()))*-/⍟⎕:exp(foldr1(sub, map(log, input())))⌊*-/⍟⎕:floor(exp(foldr1(sub, map(log, input()))))
Common Lisp, 42 charaacters
(1-(loop as x to(read)by(read)counting t))
Accepts space or line-separated input
Python, 70
Something crazy I just thought (using comma separated input):
from cmath import*
x,y=input()
print round(tan(polar(y+x*1j)[1]).real)
If you accept small float precision errors, the round function can be dropped.
Ruby 1.9, 28 characters
(?a*a+?b).split(?a*b).size-1
Rest of division, 21 characters
?a*a=~/(#{?a*b})\1*$/
Sample:
a = 756
b = 20
print (?a*a+?b).split(?a*b).size-1 # => 37
print ?a*a=~/(#{?a*b})\1*$/ # => 16
For Ruby 1.8:
a = 756
b = 20
print ('a'*a+'b').split('a'*b).size-1 # => 37
print 'a'*a=~/(#{'a'*b})\1*$/ # => 16
Python, 46 bytes
Nobody had posted the boring subtraction solution, so I could not resist doing it.
a,b=input() i=0 while a>=b:a-=b;i+=1 print i
Haskell, 96 characters
main=getLine>>=print.d.map read.words
d[x,y]=pred.snd.head.filter((>x).fst)$map(\n->(n*y,n))[0..]
Input is on a single line.
The code just searches for the answer by taking the divisor d and multiplying it against all integers n >= 0. Let m be the dividend. The largest n such that n * d <= m is picked to be the answer. The code actually picks the least n such that n * d > m and subtracts 1 from it because I can take the first element from such a list. In the other case, I would have to take the last, but it's hard work to take the last element from an infinite list. Well, the list can be proven to be finite, but Haskell does not know better when performing the filter, so it continues to filter indefinitately.
Scala 77
def d(a:Int,b:Int,c:Int=0):Int=if(b<=a)d(a-b,b,c+1)else c
d(readInt,readInt)
JavaScript, 61
A=Array,P=prompt,P((','+A(+P())).split(','+A(+P())).length-1)
This makes a string the length of the dividend ,,,,,, (6) and splits on the divisor ,,, (3), resulting in an array of length 3: ['', '', ''], whose length I then subtract one from. Definitely not the fastest, but hopefully interesting nonetheless!
Python - 73 chars
Takes comma separated input, eg 740,2
from math import*
x,y=input()
z=int(exp(log(x)-log(y)))
print(z*y+y<=x)+z
PHP, 55 characters
<?$a=explode(" ",fgets(STDIN));echo$a[0]*pow($a[1],-1);
Output (740/2): http://codepad.viper-7.com/ucTlcq
Mathematica: 34 chars
Solves symbolically the equation (x a == b)
Solve[x#[[1]]==#[[2]],x]&@Input[]
PHP - 82 characters (buggy)
$i=fgets(STDIN);$j=fgets(STDIN);$k=1;while(($a=$j*$k)<$i)$k++;echo($a>$i?--$k:$k);
This is a very simple solution, however - it doesn't handle fractions or different signs (would jump into an infinite loop). I won't go into detail in this one, it is fairly simple.
Input is in stdin, separated by a new line.
PHP - 141 characters (full)
$i*=$r=($i=fgets(STDIN))<0?-1:1;$j*=$s=($j=fgets(STDIN))<0?-1:1;$k=0;$l=1;while(($a=$j*$k)!=$i){if($a>$i)$k-=($l>>=2)*2;$k+=$l;}echo$k*$r*$s;
Input and output same as the previous one.
Yes, this is almost twice the size of the previous one, but it:
- handles fractions correctly
- handles signs correctly
- won't ever go into an infinite loop, UNLESS the second parameter is 0 - but that is division by zero - invalid input
Re-format and explanation:
$i *= $r = ($i = fgets(STDIN)) < 0 ? -1 : 1;
$j *= $s = ($j = fgets(STDIN)) < 0 ? -1 : 1;
// First, in the parentheses, $i is set to
// GET variable i, then $r is set to -1 or
// 1, depending whether $i is negative or
// not - finally, $i multiplied by $r ef-
// fectively resulting in $i being the ab-
// solute value of itself, but keeping the
// sign in $r.
// The same is then done to $j, the sign
// is kept in $s.
$k = 0; // $k will be the result in the end.
$l = 1; // $l is used in the loop - it is added to
// $k as long as $j*$k (the divisor times
// the result so far) is less than $i (the
// divided number).
while(($a = $j * $k) != $i){ // Main loop - it is executed until $j*$k
// equals $i - that is, until a result is
// found. Because a/b=c, c*b=a.
// At the same time, $a is set to $j*$k,
// to conserve space and time.
if($a > $i) // If $a is greater than $i, last step
$k -= ($l >>= 2) * 2; // (add $l) is undone by subtracting $l
// from $k, and then dividing $l by two
// (by a bitwise right shift by 1) for
// handling fractional results.
// It might seem that using ($l>>=2)*2 here
// is unnecessary - but by compressing the
// two commands ($k-=$l and $l>>=2) into 1
// means that curly braces are not needed:
//
// if($a>$i)$k-=($l>>=2)*2;
//
// vs.
//
// if($a>$i){$k-=$l;$l>>=2;}
$k += $l; // Finally, $k is incremented by $l and
// the while loop loops again.
}
echo $k * $r * $s; // To get the correct result, $k has to be
// multiplied by $r and $s, keeping signs
// that were removed in the beginning.
Yabasic - 17 characters
input a,b:?a*b^-1
JavaScript - 36 characters
p=prompt;alert(p()*Math.pow(p(),-1))
Python - 72 chars
Takes comma separated input, eg 740,2
x,y=input();z=0
for i in range(32)[::-1]:z+=(1<<i)*(y<<i<=x-z*y)
print z
Python - 41 chars
Takes comma separated input, eg 740,2
x,y=input();z=x
while y*z>x:z-=1
print z