1
\$\begingroup\$

It appears to me that this has been before in all languages except dart...

Referenced in order btw, any improvements, any tips, any feed back would be appreciated...

// The Goal: raise (x + 1) to a power and get a list of strings output

final int power = 6; //determines which power
final String firstVar = "y"; //
final int secondNumber = 1;

void main() {
  //reference first

  List<List<double>> pyrmide = [
    [1], // anything to the power of zero is one
    [1,1]]; // needs [1,1] to work properly

  //reference second

  for(int i = 1; i < power // this builds the pyrmide
      ; i += 1) { 
    List<double> nextLayer = [1,1]; // outside numbers

    for(int j = 0; j < (pyrmide.length - 1); // finds inside numbers
        j += 1) {
       nextLayer
         .insert(j + 1,
          pyrmide[i][j] + pyrmide[i][j + 1] // finds number from the above layers
          );
    }

    pyrmide.add(nextLayer); // adds to the pyrmide next layer to be found
  }

  List<String> output = []; // will output something similar to ["x^2", "2x", "1"]

  //reference third

  for(int k = 0; // k finds the power of secondvar
      k < pyrmide.length; k += 1) {
    int l = power - k; // finds the power of firstvar

    if (l != 0 && k != 0) // don't want x^0 or 1x^power
    {
    output.add("${(pyrmide[power][k]*secondNumber).toString()}$firstVar^${l.toString()}");
    } else if (l == 0) {
      output.add("${(pyrmide[power][k] * secondNumber).toString()}");
    } else if (k == 0) {
      output.add("$firstVar^$l");
    }
  }

  print(output);
}

  /* it turns that pyrmides predicts powers of (x+1)
   power of zero: 1
   power of one: 1,1 (x + 1)
   power of two: 1, 2, 1 (x^2 + 2x + 1)
  */

  /* write the pyrmide down, the outside numbers are always one
   you only have to find 1 inside number for the power of 2, 2 inside numbers for power of 3, so on
  */

  /* every term in any power of (x + 1) will always have its sum of the powers inside of it equal the power you raised (x + 1) to get to it*/
```
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.