z1 = complex(0, 0)
z2 = complex(0, 0)
z3 = complex(0, 0)
z4 = complex(0, 0)
def complex_quadratic_formula(a, b, c):
import cmath
z1 = complex((-b + cmath.sqrt(b * b - 4 * a * c)) / (2 * a))
z2 = complex((-b - cmath.sqrt(b * b - 4 * a * c)) / (2 * a))
print("z1 = ", z1)
print("z2 = ", z2)
def real_quadratic_formula(a, b, c):
if b * b - 4 * a * c >= 0:
import math
z1 = float((-b + math.sqrt(b * b - 4 * a * c)) / (2 * a))
z2 = float((-b - math.sqrt(b * b - 4 * a * c)) / (2 * a))
else:
import cmath
z1 = complex((-b + cmath.sqrt(b * b - 4 * a * c)) / (2 * a))
z2 = complex((-b - cmath.sqrt(b * b - 4 * a * c)) / (2 * a))
print("z1 = ", z1)
print("z2 = ", z2)
def cubic_equation(A, B, C, D):
# https://en.wikipedia.org/wiki/Cubic_function
# http://www.wolframalpha.com/widgets/view.jsp?id=3f4366aeb9c157cf9a30c90693eafc55
import cmath
import math
A2 = float(A * A)
B2 = float(B * B)
B3 = float(B * B * B)
C2 = float(C * C)
C3 = float(C * C * C)
D2 = float(D * D)
Delta = float(18 * A * B * C * D - 4.0 * B3 * D
+ B2 * C2 - 4.0 * A * C3 - 27.0 * A2 * D2)
Delta0 = float(B2 - 3.0 * A * C)
Delta1 = float(2.0 * B3 - 9.0 * A * B * C + 27.0 * A2 * D)
r = cmath.sqrt(-27.0 * A2 * Delta)
c = complex(0.5 * (Delta1 + r)) ** (1.0 / 3.0)
d = complex(0.5 * (Delta1 - r)) ** (1.0 / 3.0)
if c.real == 0 and c.imag == 0:
c = d
i = complex(0.0, 1.0)
chi = complex(-0.5, 0) + i * 0.5 * math.sqrt(3)
import array
ar = array.array('f', [0,0,0])
ai = array.array('f', [0,0,0])
for k in range(0, 3, 1):
chik = chi ** k
z = -(B + chik * c + Delta0 / (chik * c)) / (3.0 * A)
ar[k] = z.real
ai[k] = z.imag;
z1 = ar[0] + ai[0] * complex(0, 1);
z2 = ar[1] + ai[1] * complex(0, 1);
z3 = ar[2] + ai[2] * complex(0, 1);
print("z1 = ", z1)
print("z2 = ", z2)
print("z3 = ", z3)
def quartic_equation(A, B, C, D, E):
# https://en.wikipedia.org/wiki/Quartic_function
# https://keisan.casio.com/exec/system/1181809416
import cmath
A2 = float(A * A)
A3 = float(A * A2)
A4 = float(A2 * A2)
B2 = float(B * B)
B3 = float(B * B2)
B4 = float(B2 * B2)
C2 = float(C * C)
C3 = float(C * C2)
C4 = float(C2 * C2)
D2 = float(D * D)
D3 = float(D * D2)
D4 = float(D2 * D2)
E2 = float(E * E)
E3 = float(E * E2)
E4 = float(E2 * E2)
Delta = float(256 * A3 * E3 - 192 * A2 * B * D * E2
- 128 * A2 * C2 * E2 + 144 * A2 * C * D2 * E
- 27 * A2 * D4 + 144 * A * B2 * C * E2
- 6 * A * B2 * D2 * E - 80 * A * B * C2 * D * E
+ 18 * A * B * C * D3 + 16 * A * C4 * E
- 4 * A * C3 * D2 - 27 * B4 * E2 + 18 * B3 * C * D * E
- 4 * B3 * D3 - 4 * B2 * C3 * E + B2 * C2 * D2)
Delta0 = float(C2 - 3 * B * D + 12 * A * E)
Delta1 = float(2 * C3 - 9 * B * C * D
+ 27 * B2 * E + 27 * A * D2 - 72 * A * C * E)
p = float((8 * A * C - 3 * B2) / (8 * A2))
q = float((B3 - 4 * A * B * C + 8 * A2 * D) / (8 * A3))
R = cmath.sqrt(Delta1 * Delta1 - 4 * Delta0 * Delta0 * Delta0)
P = cmath.sqrt(-27 * Delta)
Q = complex(0.5 * (Delta1 + P)) ** complex(1.0 / 3.0)
S = 0.5 * cmath.sqrt(((-2.0 * p) / 3.0)
+ (Q + Delta0 / Q) / (3 * A))
a = complex(-B / (4 * A), 0)
s = 0.5 * cmath.sqrt(-4 * S * S - 2 * p + q / S)
z1 = a - S + s
z2 = a - S - s
s = 0.5 * cmath.sqrt(-4 * S * S - 2 * p - q / S)
z3 = a + S + s
z4 = a + S - s
print("z1 = ", z1)
print("z2 = ", z2)
print("z3 = ", z3)
print("z4 = ", z4)
option = int(1)
print("Menu")
print("1 Solve a linear equation")
print("2 Solve a quadratic eqution")
print("3 Solve a cubic equation")
print("4 Solve a quartic equation")
print("5 Quit")
option = int(input("Option 1 to 5: "))
while option != 5:
if option >= 1:
a = float(input("a = "))
b = float(input("b = "))
if option >= 2:
c = float(input("c = "))
if option >= 3:
d = float(input("d = "))
if option == 4:
e = float(input("e = "))
if option == 1:
z1 = -b / a
print("z1 = ", z1)
elif option == 2:
real_quadratic_formula(a, b, c)
elif option == 3:
cubic_equation(a, b, c, d)
elif option == 4:
quartic_equation(a, b, c, d, e)
option = int(input("Option 1 to 5: "))
Author: jamespatewilliamsjr
My whole legal name is James Pate Williams, Jr. I was born in LaGrange, Georgia approximately 70 years ago. I barely graduated from LaGrange High School with low marks in June 1971. Later in June 1979, I graduated from LaGrange College with a Bachelor of Arts in Chemistry with a little over a 3 out 4 Grade Point Average (GPA). In the Spring Quarter of 1978, I taught myself how to program a Texas Instruments desktop programmable calculator and in the Summer Quarter of 1978 I taught myself Dayton BASIC (Beginner's All-purpose Symbolic Instruction Code) on LaGrange College's Data General Eclipse minicomputer. I took courses in BASIC in the Fall Quarter of 1978 and FORTRAN IV (Formula Translator IV) in the Winter Quarter of 1979. Professor Kenneth Cooper, a genius poly-scientist taught me a course in the Intel 8085 microprocessor architecture and assembly and machine language. We would hand assemble our programs and insert the resulting machine code into our crude wooden box computer which was designed and built by Professor Cooper. From 1990 to 1994 I earned a Bachelor of Science in Computer Science from LaGrange College. I had a 4 out of 4 GPA in the period 1990 to 1994. I took courses in C, COBOL, and Pascal during my BS work. After graduating from LaGrange College a second time in May 1994, I taught myself C++. In December 1995, I started using the Internet and taught myself client-server programming. I created a website in 1997 which had C and C# implementations of algorithms from the "Handbook of Applied Cryptography" by Alfred J. Menezes, et. al., and some other cryptography and number theory textbooks and treatises.
View all posts by jamespatewilliamsjr