| Bytes | Lang | Time | Link |
|---|---|---|---|
| 092 | APLNARS | 250228T135941Z | Rosario |
| 123 | Python | 250303T192750Z | Username |
| 063 | APL Dyalog Unicode 18.0 | 200924T044940Z | Bubbler |
| 108 | Javascript with lambdas | 141023T153522Z | Qwertiy |
| 255 | Python 2 | 141004T062548Z | Justin |
| 094 | J | 110211T192917Z | Eelvex |
| 126 | Haskell | 110211T152629Z | J B |
APL(NARS), 92 chars
{{⍬≡↑⌽⍵:(⊂¯1↓⍵),⊂,0⋄0=≢1↓⍵:(⊂,0),⍵⋄(⊂¯1↓⍵),¯1↑⍵}⍺{0>k←-/≢¨⍵⍺:⊂,⍵⋄d,⍺∇1↓⍵-(⍺,k⍴0)×d←÷/↑¨⍵⍺}⍵}
Input: 1 2 3 4 means x^3+2x^2+3x^1+4
Input: divisor function dividend
Output: quotient, remainder
Im sorry for the one has upvote this, but i find some bugs... Possible there are other bugs, not too much test... last few test:
f←{{⍬≡↑⌽⍵:(⊂¯1↓⍵),⊂,0⋄0=≢1↓⍵:(⊂,0),⍵⋄(⊂¯1↓⍵),¯1↑⍵}⍺{0>k←-/≢¨⍵⍺:⊂,⍵⋄d,⍺∇1↓⍵-(⍺,k⍴0)×d←÷/↑¨⍵⍺}⍵}
2 5 f 1 3
┌2────────────┐
│┌1───┐ ┌1───┐│
││ 0.5│ │ 0.5││
│└~───┘ └~───┘2
└∊────────────┘
1 0 ¯5 f 12 ¯5 3 ¯1
┌2─────────────────┐
│┌2─────┐ ┌2──────┐│
││ 12 ¯5│ │ 63 ¯26││
│└~─────┘ └~──────┘2
└∊─────────────────┘
2 0 ¯5 f 12 ¯5 3 ¯1
┌2────────────────────┐
│┌2──────┐ ┌2────────┐│
││ 6 ¯2.5│ │ 33 ¯13.5││
│└~──────┘ └~────────┘2
└∊────────────────────┘
1 2 f 1 2 3 4
┌2─────────────┐
│┌3─────┐ ┌1──┐│
││ 1 0 3│ │ ¯2││
│└~─────┘ └~──┘2
└∊─────────────┘
1 1 f 1 6 12 7
┌2────────────┐
│┌3─────┐ ┌1─┐│
││ 1 5 7│ │ 0││
│└~─────┘ └~─┘2
└∊────────────┘
1 3 ¯1 f 5 ¯2 ¯3 171 4
┌2───────────────────┐
│┌3────────┐ ┌2─────┐│
││ 5 ¯17 53│ │ ¯5 57││
│└~────────┘ └~─────┘2
└∊───────────────────┘
1 f 12 ¯5 3 ¯1
┌2─────────────────┐
│┌4──────────┐ ┌1─┐│
││ 12 ¯5 3 ¯1│ │ 0││
│└~──────────┘ └~─┘2
└∊─────────────────┘
2 f 12 ¯5 3 ¯1
┌2──────────────────────┐
│┌4───────────────┐ ┌1─┐│
││ 6 ¯2.5 1.5 ¯0.5│ │ 0││
│└~───────────────┘ └~─┘2
└∊──────────────────────┘
1 1 1 1 1 1 f 12 ¯5 3 ¯1
┌2─────────────────┐
│┌1─┐ ┌4──────────┐│
││ 0│ │ 12 ¯5 3 ¯1││
│└~─┘ └~──────────┘2
└∊─────────────────┘
3 f 4
┌2──────────────────┐
│┌1───────────┐ ┌1─┐│
││ 1.333333333│ │ 0││
│└~───────────┘ └~─┘2
└∊──────────────────┘
1 1 1 1 1 1 f 0
┌2────────┐
│┌1─┐ ┌1─┐│
││ 0│ │ 0││
│└~─┘ └~─┘2
└∊────────┘
this below is a little more long of the recursive one, but i like it more
r←a D w;q;k;d
r←,w⋄q←⍬
→3×⍳0>k←-/≢¨r a⋄q←q,d←÷/↑¨r a⋄r←1↓r-(a,k⍴0)×d⋄→2
→4×⍳q≢⍬⋄q←,0
→5×⍳r≢⍬⋄r←,0
r←(⊂q),⊂r
//14+9+49+13+13+10= 108 it would returns the same of the recursive one. It is need a function remove the more 0 in the remainder. For example in
1 1 1 1 f 1 0 0 0 ¯2
┌2────────────────┐
│┌2────┐ ┌3──────┐│
││ 1 ¯1│ │ 0 0 ¯1││
│└~────┘ └~──────┘2
└∊────────────────┘
it is better that is just -1 and not 0x^2+0x-1.
Python, 123 bytes
f=lambda x,y,z=[],l=len:z+x if l(x)<l(y)else f([X[0]-X[1]*x[0]/y[0]for X in zip(x[:l(y)],y)][1:]+x[l(y):],y,z+[x[0]/y[0]])
Divides term by term.
Run with print(f(dividend, divisor))
APL (Dyalog Unicode) 18.0, 63 bytes
{0≥d←1+⍺-⍥≢⍵:⍬⍺⋄q(r↓⍨⊥⍨0=⌽r←⍺-m+.×q←(d↑⍺)⌹d↑m←⍉{⍵,⍨⍺⍴0}∘⍵⍤0⍳d)}
The TIO link (which uses 17.1) has a polyfill for ⍥.
Well, it uses ⌹ to solve a system of equations, but technically it's not a library, so...
How it works
Illustration for the inputs x←12 ¯5 3 ¯1 and y←1 0 ¯5:
- We know that, from the lengths of
xandy, the quotient will have two terms. - Construct a matrix which, when matrix-multiplied with the quotient, will give the polynomial product of quotient with
y:
1 0
0 1
¯5 0
0 ¯5
- Calculate the quotient
qso that first two terms ofxagree with those ofy×q, using matrix division⌹. - Calculate the remainder
rusingx - y×q, then remove leading zeros ofr. (qis guaranteed to have no leading zeros.)
{ ⍝ x←dividend, y←divisor
d←1+⍺-⍥≢⍵ ⍝ Degree of the quotient; 1 + length of x - length of y
0≥d: ⍬⍺ ⍝ If degree is zero or lower: quotient is zero, remainder is x
m←⍉{⍵,⍨⍺⍴0}∘⍵⍤0⍳d ⍝ Construct the matrix
⍳d ⍝ 0..d-1
{ }∘⍵⍤0 ⍝ For each number i, yielding a matrix row
⍵,⍨⍺⍴0 ⍝ Make i copies of 0, and append y
⍝ (Implicitly right-pad with zeros for short rows)
⍉ ⍝ Transpose
q←(d↑⍺)⌹d↑m ⍝ Get the quotient
d↑m ⍝ Take first d rows of the matrix
(d↑⍺) ⍝ Take first d numbers of x
⌹ ⍝ Matrix divide aka. solve linear system of equations
r←⍺-m+.×q ⍝ Get the remainder
q(r↓⍨⊥⍨0=⌽r) ⍝ Remove leading zeros from r, and join with q
⊥⍨0=⌽ ⍝ Reverse r, test equal to zero, and count trailing ones (⊥⍨)
r↓⍨ ⍝ Drop that many numbers from start of r
q( ) ⍝ Join with q
}
Javascript with lambdas, 108
f=(a,b)=>{for(n=b.length;a.length>=n;a.shift())for(b.push(k=a[q=0]/b[0]);q<n;++q)a[q]-=k*b[q];b.splice(0,n)}
It replaces first argument by reminder and second by result.
Example of usage in Firefox:
f(x=[12,-5,3,-1], y=[1,0,-5]), console.log(x, y)
// Array [ 63, -26 ] Array [ 12, -5 ]
Sorry for the bug. Already fixed.
Python 2, 260 258 257 255 bytes
exec'''def d(p,q):
R=range;D=len(p);F=len(q)-1;d=q[0];q=[q[i]/-d@R(1,F+1)];r=[0@R(D)];a=[[0@R(F)]@R(D)]
@R(D):
p[i]/=d;r[i]=sum(a[i])+p[i]
for j in R(F):
if i<D-F:a[i+j+1][F-j-1]=r[i]*q[j]
return r[:D-F],[d*i@r[D-F:]]'''.replace('@',' for i in ')
This executes:
def d(p,q):
R=range;D=len(p);F=len(q)-1;d=q[0];q=[q[i]/-d for i in R(1,F+1)];r=[0 for i in R(D)];a=[[0 for i in R(F)] for i in R(D)]
for i in R(D):
p[i]/=d;r[i]=sum(a[i])+p[i]
for j in R(F):
if i<D-F:a[i+j+1][F-j-1]=r[i]*q[j]
return r[:D-F],[d*i for i in r[D-F:]]
Use like so:
>>>d([12., -5., 3., -1.],[1.,0.,-5.])
([12.0, -5.0], [63.0, -26.0])
J, 94
f=:>@(0&{)
d=:0{[%~0{[:f]
D=:4 :'x((1}.([:f])-((#@]{.[)f)*d);([:>1{]),d)^:(>:((#f y)-(#x)))y'
eg.
(1 0 _5) D (12 _5 3 _1;'')
63 _26 | 12 _5
Explanation of some snippets, given that a: (12 -5 3 -1) and b: (1 0 -5)
length of a:
#a
4
make a and b same order by appending zeroes to b:
(#a) {. b
1 0 -5 0
divide higher powers (first elements) of a, b:
(0{a) % (0{b)
12
multiply b by that and subtract it from a:
a - 12*b
12 0 _60
repeat n times b = f(a,b):
a f^:n b
Haskell, 126
For a start:
l s _ 0=s
l(x:s)(y:t)n=x/y:l(zipWith(-)s$map(*(x/y))t++repeat 0)(y:t)(n-1)
d s t=splitAt n$l s t n where n=length s-length t+1
Sample use:
*Main> d [12, -5, 3, -1] [1, 0, -5]
([12.0,-5.0],[63.0,-26.0])