Polynomial long division: Difference between revisions

Added 11l
(Undo revision 105246 by 75.149.84.28 (talk) [This change resulted in the wrong result])
(Added 11l)
Line 87:
q: -27 -9 1 → x<sup>2</sup> - 9x - 27
r: -123 0 0 → -123
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>F degree(&poly)
L !poly.empty & poly.last == 0
poly.pop()
R poly.len - 1
 
F poly_div(&n, &D)
V dD = degree(&D)
V dN = degree(&n)
I dD < 0
exit(1)
[Float] q
I dN >= dD
q = [0.0] * dN
L dN >= dD
V d = [0.0] * (dN - dD) [+] D
V mult = n.last / Float(d.last)
q[dN - dD] = mult
d = d.map(coeff -> coeff * @mult)
n = zip(n, d).map((coeffN, coeffd) -> coeffN - coeffd)
dN = degree(&n)
E
q = [0.0]
R (q, n)
 
print(‘POLYNOMIAL LONG DIVISION’)
V n = [-42.0, 0.0, -12.0, 1.0]
V D = [-3.0, 1.0, 0.0, 0.0]
print(‘ #. / #. =’.format(n, D), end' ‘ ’)
V (q, r) = poly_div(&n, &D)
print(‘ #. remainder #.’.format(q, r))</lang>
 
{{out}}
<pre>
POLYNOMIAL LONG DIVISION
[-42, 0, -12, 1] / [-3, 1, 0, 0] = [-27, -9, 1] remainder [-123]
</pre>
 
=={{header|Ada}}==
1,481

edits