12345678910111213141516171819202122232425262728293031 |
- #!/usr/bin/ruby
- #
- ## https://rosettacode.org/wiki/Roots_of_a_quadratic_function
- #
- var sets = [
- [1, 2, 1],
- [1, 2, 3],
- [1, -2, 1],
- [1, 0, -4],
- [1, -1e6, 1],
- ];
-
- func quadroots(a, b, c) {
- var root = (
- (b**2 - 4*a*c) -> complex.sqrt
- );
-
- a.complex!;
- b.complex!;
-
- [(-b + root) / (2 * a),
- (-b - root) / (2 * a)];
- }
-
- sets.each { |coefficients|
- say ("Roots for #{coefficients.dump}",
- "=> (#{quadroots(coefficients...).join(', ')})");
- }
|