Heptalingual Linear & Quadratic Solvers
C++
Linear
#include <iostream>
#include <cmath>
using namespace std;
void linear(double a, double b) {
if (a == 0) {
if (b == 0) {
cout << "Infinite solutions" << endl;
} else {
cout << "No solution" << endl;
}
return;
}
double x = -b / a;
cout << "Solution: x = " << x << endl;
}
int main(){
linear(2, 4);
linear(0, 0);
linear(0, 3);
return 0;
}
Quadratic
#include <iostream>
#include <cmath>
using namespace std;
void quadratic(double a, double b, double c) {
double d = b*b - 4*a*c;
if (d < 0) {
cout << "No real roots" << endl;
} else {
double x1 = (-b + sqrt(d)) / (2*a);
double x2 = (-b - sqrt(d)) / (2*a);
cout << "Roots: x1 = " << x1 << ", x2 = " << x2 << endl;
}
}
int main() {
quadratic(1, -3, 2);
quadratic(1, 2, 5);
return 0;
}
JavaScript
Linear
function linear(a, b) {
if (a === 0) {
if (b === 0) {
console.log("Linear: Infinite solutions");
} else {
console.log("Linear: No solution");
}
} else {
let x = -b / a;
console.log(`Linear: Solution x = ${x}`);
}
}
linear(2, -4);
linear(0, 0);
linear(0, 5);
Quadratic
function quadratic(a, b, c) {
let d = b * b - 4 * a * c;
if (d < 0) {
console.log("Quadratic: No real roots");
return;
}
let x1 = (-b + Math.sqrt(d)) / (2 * a);
let x2 = (-b - Math.sqrt(d)) / (2 * a);
console.log(`Quadratic: Roots: x1 = ${x1}, x2 = ${x2}`);
}
quadratic(1, -3, 2);
Lisp
Linear
(defun linear (a b)
(cond
((= a 0)
(if (= b 0)
(format t "Infinite solutions~%")
(format t "No solution~%")))
(t
(let ((x (/ (- b) a)))
(format t "Solution: x = ~,2f~%" x)
x))))
(linear 2 4)
(linear 0 0)
(linear 0 3)
Quadratic
(defun quadratic (a b c)
(let ((d (- (* b b) (* 4 a c))))
(if (< d 0)
(format t "No real roots.~%")
(let* ((sqrt-d (sqrt (coerce d 'double-float)))
(x1 (/ (+ (- b) sqrt-d) (* 2 a)))
(x2 (/ (- b sqrt-d) (* 2 a))))
(format t "Roots: x1 = ~,2f, x2 = ~,2f~%" (float x1) (float x2))
(values x1 x2)))))
(quadratic 1 -3 2)
(quadratic 1 2 5)
Lua
Linear
function linear(a, b)
if a == 0 then
if b == 0 then
print("Infinite solutions")
else
print("No solution")
end
return nil
end
local x = -b / a
print("Solution: x = " .. x)
return x
end
Quadratic
function quadratic(a, b, c)
local d = b^2 - 4 * a * c
if d < 0 then
print("No real roots")
return nil, nil
end
local sqrt_d = math.sqrt(d)
local x1 = (-b + sqrt_d) / (2 * a)
local x2 = (-b - sqrt_d) / (2 * a)
print("Roots: x1 = " .. x1 .. ", x2 = " .. x2)
return x1, x2
end
Python
Linear
def linear(a, b):
if a == 0:
if b == 0:
print("Infinite solutions")
else:
print("No solution")
return None
x = -b / a
print(f"Solution: x = {x:.2f}")
return x
Quadratic
import math
def quadratic(a, b, c):
d = b*b - 4*a*c
if d < 0:
print("No real roots.")
return None
sqrt_d = math.sqrt(d)
x1 = (-b + sqrt_d) / (2*a)
x2 = (-b - sqrt_d) / (2*a)
print(f"Roots: x1 = {x1:.2f}, x2 = {x2:.2f}")
return x1, x2
Tcl
Linear
proc linear {a b} {
if {$a == 0} {
if {$b == 0} {
puts "Infinite solutions"
} else {
puts "No solution"
}
return
}
set x [expr {- $b / $a}]
puts "Solution: x = $x"
}
Quadratic
proc quadratic {a b c} {
set d [expr {$b*$b - 4*$a*$c}]
if {$d < 0} {
puts "No real roots"
return
}
set sqrt_d [expr {sqrt($d)}]
set x1 [expr {(-$b + $sqrt_d)/(2*$a)}]
set x2 [expr {(-$b - $sqrt_d)/(2*$a)}]
puts "Roots: x1 = $x1, x2 = $x2"
}
Smalltalk
Linear
solver := Dictionary new.
solver at: #solve put: [:a :b :c | (c - b) / a ].
solution := (solver at: #solve) value: 2 value: 5 value: 11.
solution displayNl.
Quadratic
a := 0. "or whatever value"
b := -8.
c := 2.
disc := b*b - 4*a*c.
denom := 2*a.
denom := denom = 0 ifTrue: [1] ifFalse: [denom].
root1 := (0 - b + disc sqrt) / denom.
root2 := (0 - b - disc sqrt) / denom.
solution := Array with: root1 with: root2.
'Demo: roots ->' displayNl.
solution displayNl.