Coding complex exponentiation in Python
Algebra is a hard concept, and can be tough when complicating problems, the same way most schools do when it comes to teaching something as simple as exponentiating sums of variables. Even something as simple as the sum of 2 variables, usually a and b.
Students are taught to calculate (a + b)² in a very difficult way, which is multiplying (a + b) and (a + b)
But there is a simpler way, which involves a basic concept taught to kids in kindergarten — The number pyramid.
If you might have forgotten, the number pyramid basically takes two numbers at the top of the pyramid, adds them and puts the new number at the bottom. It goes like this.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
and so onThe numbers at the bottom are the sums of the two numbers at the top.
But they are actually the constant factors of the expression that forms when (a + b) is exponentiated by a number.
(a + b)⁰ = 1
(a + b)¹ = 1 1
(a + b)² = 1 2 1
(a + b)³ = 1 3 3 1
(a + b)⁴ = 1 4 6 4 1
(a + b)⁵ = 1 5 10 10 5 1The first literal factor of (a + b)ˣ is always aˣ, and the next factor is aˣ times b/a and so on.
(a + b)⁰ = 1
(a + b)¹ = 1a¹ + 1b¹
(a + b)² = 1a² + 2a¹b¹ + 1b²
(a + b)³ = 1a³ + 3a²b¹ + 3a¹b² + 1b³
(a + b)⁴ = 1a⁴ + 4a³b¹ + 6a²b² + 4a¹b³ + 1b⁴
(a + b)⁵ = 1a⁵ + 5a⁴b¹ + 10a³b² + 10a²b³ + 5a¹b⁴ + 1b⁵Now we can start implementing it in python, to create a mini wolfram alpha, a system of algorithms that can solve algebra problems.
Get Adit g’s stories in your inbox
Join Medium for free to get updates from this writer.
Step 1: Start by asking for user input and initializing all the variables.
a = int(input("(a + b)^")) #user enters the exponent
x = [] #The use of these lists will be explained later
y = []
b = "" #This string outputs the final answerStep 2: Make a function which converts numbers to superscript.
def get_super(z):
normal = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-=()"
super_s = "ᴬᴮᶜᴰᴱᶠᴳᴴᴵᴶᴷᴸᴹᴺᴼᴾQᴿˢᵀᵁⱽᵂˣʸᶻᵃᵇᶜᵈᵉᶠᵍʰᶦʲᵏˡᵐⁿᵒᵖ۹ʳˢᵗᵘᵛʷˣʸᶻ⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾"
#These consist of strings which hold input and output values
res = z.maketrans(''.join(normal), ''.join(super_s))
return z.translate(res)
#Translates the input with the required value and returns the outputStep 3: Make outputs for values that do not work with this program: If x equals to 0, 1 or 2, it may throw an error. To fix that, we can easily make separate codes for that
if a == 0:
print("1")
elif a == 1:
print("a + b")
elif a == 2:
print("a" + get_super("2") + " + 2ab + b" + get_super("2"))
#checks code if the value is 0, 1 or 2
else:
pass
#main code will be here. Delete the pass later on.Step 4: Make a list for the constant factors.
for i in range(0,a+1,1):
if i == 0 or i == 1:
x.append("1")
y.append("1")
# first value always equals to 1
else:
for j in range(1,i,1):
x[j] = int(y[j-1]) + int(y[j])
#In simple terms, this code uses the number pyramid to find values.
x.append("1")
#The last value also always equals to 1.
y.clear()
y.extend(x)
#The reason why we use 2 different lists is because we inspect the first list, x, but since it gets modified, we store the old value of x in a separate list called y. Step 5: Multiply each value of the list with the corresponding literal values.
for k in range(0,len(x),1):
if k == 0:
b += "a" + get_super(str(a)) + " + "
#First value always contains the first literal
elif k == len(x) - 1:
b += "b" + get_super(str(a))
#Last value always contains the last literal
elif k == 1:
b += str(x[k]) + "a" + get_super(str(a-k)) + "b + "
elif len(x) - k == 2:
b += str(x[k]) + "ab" + get_super(str(k)) + " + "
#These two conditions are done to omit powers of 1, which can just be represented by the variable itself
else:
b += str(x[k]) + "a" + get_super(str(a-k)) + "b" + get_super(str(k)) + " + "
#If the following four conditions are not true, then the main code executes.Step 6: Print the final value, which is b
print(b)Your code should be looking like this:
a = int(input("(a + b)^"))
x = []
y = []
b = ""
def get_super(z):
normal = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-=()"
super_s = "ᴬᴮᶜᴰᴱᶠᴳᴴᴵᴶᴷᴸᴹᴺᴼᴾQᴿˢᵀᵁⱽᵂˣʸᶻᵃᵇᶜᵈᵉᶠᵍʰᶦʲᵏˡᵐⁿᵒᵖ۹ʳˢᵗᵘᵛʷˣʸᶻ⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾"
res = z.maketrans(''.join(normal), ''.join(super_s))
return z.translate(res)
if a == 0:
print("1")
elif a == 1:
print("a + b")
elif a == 2:
print("a" + get_super("2") + " + 2ab + b" + get_super("2"))
else:
for i in range(0,a+1,1):
if i == 0 or i == 1:
x.append("1")
y.append("1")
else:
for j in range(1,i,1):
x[j] = int(y[j-1]) + int(y[j])
x.append("1")
y.clear()
y.extend(x)
for k in range(0,len(x),1):
if k == 0:
b += "a" + get_super(str(a)) + " + "
elif k == len(x) - 1:
b += "b" + get_super(str(a))
elif k == 1:
b += str(x[k]) + "a" + get_super(str(a-k)) + "b + "
elif len(x) - k == 2:
b += str(x[k]) + "ab" + get_super(str(k)) + " + "
else:
b += str(x[k]) + "a" + get_super(str(a-k)) + "b" + get_super(str(k)) + " + "
print(b)If, instead of (a + b)ˣ, it is (a - b)ˣ, simply use alternating signs starting from positive value, like this:
(a - b)⁰ = 1
(a - b)¹ = 1a¹ - 1b¹
(a - b)² = 1a² - 2a¹b¹ + 1b²
(a - b)³ = 1a³ - 3a²b¹ + 3a¹b² - 1b³
(a - b)⁴ = 1a⁴ - 4a³b¹ + 6a²b² - 4a¹b³ + 1b⁴
(a - b)⁵ = 1a⁵ - 5a⁴b¹ + 10a³b² - 10a²b³ + 5a¹b⁴ - 1b⁵This is how simple concepts can be used to solve complicated problems. The methods of exponentiation and simplified versions have been explained here. We have also created a python code specifically for calculating such problems. For an additional challenge, you can now try to make a program, but this time to calculate (a - b)ˣ instead of (a + b)ˣ.






