Anonymous user
Polynomial long division: Difference between revisions
→{{header|Ruby}}: reform of the display
(Updated D entry) |
(→{{header|Ruby}}: reform of the display) |
||
Line 1,464:
def degree(ary)
idx = ary.rindex
idx
end
Line 1,483:
g = [-3, 1, 0, 0]
q, r = polynomial_long_division(f, g)
puts "#{f} / #{g} => #{q} remainder #{r}"
# =>
g = [-3, 1, 1, 0]
q, r = polynomial_long_division(f, g)
puts "#{f} / #{g} => #{q} remainder #{r}"
# =>
Implementing the algorithms on the [[wp:Polynomial long division|wikipedia page]] -- uglier code but nicer user interface
Line 1,553:
g = [1, -3]
q, r = polynomial_division(f, g)
puts "#{f} / #{g} => #{q} remainder #{r}"
# =>
g = [1, 1, -3]
q, r = polynomial_division(f, g)
puts "#{f} / #{g} => #{q} remainder #{r}"
# =>
Best of both worlds: {{trans|Tcl}}
<lang ruby>def
if g.length == 0 or (g.length == 1 and g[0] == 0)
raise ArgumentError, "denominator is zero"
Line 1,568:
return [[0], f] if f.length < g.length
q, n = [], f.dup
▲ while n.length >= d.length
n[0, g.length].zip(g).each_with_index do |pair, i|
▲ q << Float(n[0]) / d[0]
n[i] = pair[0
end
n.shift
Line 1,585 ⟶ 1,584:
g = [1, -3]
q, r = polynomial_division(f, g)
puts "#{f} / #{g} => #{q} remainder #{r}"
# =>
g = [1, 1, -3]
q, r = polynomial_division(f, g)
puts "#{f} / #{g} => #{q} remainder #{r}"
# =>
=={{header|Slate}}==
|