public
Last active

Weird way to say Bhaskara in Ruby (or "((-b).± √ Δ(a, b, c))" / (2 * a) as a valid Ruby expression)

  • Download Gist
Bhaskara
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#!/usr/bin/env ruby
#-*- coding: utf-8 -*-
 
def (n)
Math::sqrt(n)
end
 
class Numeric
def ±(n)
r = [self + n, self - n]
def r./(n)
[first / n, last / n]
end
r
end
end
 
def Δ(a, b, c)
b**2 - (4 * a * c)
end
 
def roots(a, b, c)
((-b).± Δ(a, b, c)) / (2 * a)
end
 
# x² -5x + 6
puts roots 1, -5, 6

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.