I got this code example online for a Non-recursive version of Pascal’s Triangle and made modifications to it so that it limits the triangle size to an input n, prints the last line based on the int n=1+ 16 [where +16 = the power to be solved], then outputs the entire binomial expansion format and corresponding numerical and exponential values in the console.
I’d like to adjust the output values of the program to solve based on complex number sets (ex, (a+bi)^2, (a+bi)^3, (a+bi)^4, etc) rather than just real number values of (a+b)^n as it is now.
How could this code be updated so that the output values are representative of complex numbers?
In other words how could this algorithm be included,
When raising complex numbers to a power:
i1 = i, i2 = –1, i3 = –i, and i4 = 1
This pattern repeats into higher powers:
i5 = i , i6 = –1, i7 = – i, and so on
Because powers of the imaginary number i can be simplified, the new solution doesn’t include powers of i, but that of non-powered i’s and negative values.
(with all the Pascal’s Triangle values remaining the same as those without complex numbers, we just need to include the negative values and single i’s into the existing program output values)
//Non-recursive version of Pascal Triangle
//Power of Pasccal's Triangle, +n
int n=1+ 2;
PFont font;
int[][] arr = new int[n][n];
int step=12;
String ll="";
int k=1;
int k1=n-1;
int k2=n-1;
int l=0;
int l1=0;
void setup()
{
size(1600,600);
smooth();
font=createFont("Courier New",10);
textFont(font);
textAlign(CENTER);
fill(0);
}
void draw(){
background(220);
for (int line = 0; line < n; line++)
{
ll="";
for (int i = 0; i <= line; i++)
{
if (line == i || i == 0)
arr[line][i] = 1;
else
arr[line][i] = arr[line-1][i-1] + arr[line-1][i];
ll=ll+str(arr[line][i])+" ";
//Print out only single line, defined by +n
if (i==n-1 && k<=1){
println("Example:");
println("n=2 ; (a+b)^2 = 1 * a^(2)b^(0) + 2 * a^(1)b^(1) + 1 * a^(0)b^(2)");
println("");
println("Format:");
println("n=Input ; (a+b)^n = X * a^(va1)b^(0) + X * a^(va2)b^(vb2) ... + X * a^(0)b^(vb3)");
println("");
println("Numerical Coefficients(x) =");
println(ll);
k++;
}
}
text(ll,mouseX,mouseY+step*(line+1)-height/2);
}
if (l1<1){
println("-----------------------");
println("LEFT TO RIGHT");
println("Exponent Variables(va) =");
l1++;
}
if (k1>0){
for (int i2 = n-1; i2 >= 0; i2--){
println("a=",i2);
k1--;
}
}
if (l<1){
println("-----------------------");
println("LEFT TO RIGHT");
println("Exponent Variables(vb) =");
l++;
}
if (k2<n){
for (int i2 = 0; i2 <= n-1; i2++){
println("b=",i2);
k2++;
}
}
}