# Cardano Formula


```python
print("I'm going to solve a cubic equation for you")
```

    I'm going to solve a cubic equation for you



```python
def discriminant():
    print("Ensure the coefficient of x^3 is 1")
    a = int(input("The coefficient of x^2 "))
    b = int(input("The coefficient of x "))
    c = int(input("The constant term "))
    discriminant = ((a**2)*(b**2)) + (18*a*b*c) - (4*(b**3)) - (4*(a**2)*c) - (27*(c**2))
    if discriminant > 0:
        print("The roots are real and distinct")
    elif discriminant == 0:
        print("The roots are real and equal")
    else:
        print("There is 1 real root and 2 imaginary roots")
    return discriminant;
discriminant()

```

    Ensure the coefficient of x^3 is 1
    The coefficient of x^2 1
    The coefficient of x 2
    The constant term 3
    There is 1 real root and 2 imaginary roots





    -175




```python
import math

def Cardano():
    print("Ensure the coefficient of x^3 is 1")
    a = float(input("The coefficient of x^3 "))
    b = float(input("The coefficient of x^2 "))
    c = float(input("The coefficient of x "))
    d = float(input("The constant term "))
    if a !=1:
        print("Please let a be equal to a by dividing the equation by a")
    else:
        while a ==1:
            
            discriminant = ((b**2)*(c**2)) + (18*b*c*d) - (4*(c**3)) - (4*(b**2)*d) - (27*(d**2))
            
            Q = (3*c - b)/9
            R = ((9*b*c)-(27*d) - (2 * pow(b,3)))/54
            S = ((R + math.sqrt((Q**3)+ (R**2))))**(1/3)
            T = ((R - math.sqrt((Q**3)+ (R**2))))**(1/3)
                
            if discriminant < 0:
                print (Q,R,S,T)
                
            else:
                print("Can't use cardano")
    return
Cardano()
```


```python
# importing library sympy
from sympy import symbols, Eq, solve
  
# defining symbols used in equations
# or unknown variables
x, y = symbols('x,y')
  
# defining equations
eq1 = Eq((x+y), 1)
print("Equation 1:")
print(eq1)
eq2 = Eq((x-y), 1)
print("Equation 2")
print(eq2)
  
# solving the equation
print("Values of 2 unknown variable are as follows:")
  
print(solve((eq1, eq2), (x, y)))
```

    Equation 1:
    Eq(x + y, 1)
    Equation 2
    Eq(x - y, 1)
    Values of 2 unknown variable are as follows:
    {x: 1, y: 0}



```python
import math

def cuberoot(x):
    if x < 0:
        x = abs(x)
        cube_root = x**(1/3)*(-1)
    else:
        cube_root = x**(1/3)
    return cube_root



def Cardano():
    print("Ensure the coefficient of x^3 is 1")
    a = float(input("The coefficient of x^3 "))
    b = float(input("The coefficient of x^2 "))
    c = float(input("The coefficient of x "))
    d = float(input("The constant term "))
    if a !=1:
        print("Please let a be equal to a by dividing the equation by a")
    else:
        if a ==1:
            
            discriminant = ((b**2)*(c**2)) + (18*b*c*d) - (4*(c**3)) - (4*(b**2)*d) - (27*(d**2))
            
            Q = (3*c - pow(b,2))/9
            R = ((9*b*c)-(27*d) - (2 * pow(b,3)))/54
            u = (Q**3)+ (R**2)
            u = math.sqrt(u)
            x = R+u
            y = R-u
            if R < 0:
                S = cuberoot(x)
                T = cuberoot(y)
                print (S)
            else:
                print('Tired')
                #S = (R + math.sqrt(u))**(1/3)
            #u = (Q**3)+ (R**2)
            #S = (abs(R) + math.sqrt(u))**(1/3)
            #T = (R - u)**(0.33)
                
            if discriminant < 0:
                print (Q,R)
              
            else:
                print("Can't use cardano")
    return
Cardano()
```

    Ensure the coefficient of x^3 is 1
    The coefficient of x^3 1
    The coefficient of x^2 3
    The coefficient of x 0
    The constant term 0.5
    -0.7937005259840998
    -1.0 -1.25



```python

```

f9ef5fa1b082919a5fbf117397ff71ce07c311de
ipynb
Jupyter Notebook
week14/Cardano.ipynb
Dumebi35/DumebiCSC101
6c63aa0879fdc1d99ec8600e29046413093fcfe2
MIT
week14/Cardano.ipynb
Dumebi35/DumebiCSC101
6c63aa0879fdc1d99ec8600e29046413093fcfe2
MIT
week14/Cardano.ipynb
Dumebi35/DumebiCSC101
6c63aa0879fdc1d99ec8600e29046413093fcfe2
MIT
Qwen/Qwen-72B
1. YES
2. YES