1
\$\begingroup\$

For all you American folk out there, remove the "s" in "maths".

Ugh, my maths teacher gave me so much algebra to do. There's just lines and lines of meaningless junk like Expand (x+1)(x+6) and Factorise x^2+2x+1.

My hands are literally dying from the inside.

So I figured out a way to hack the system - why not write a program in my book (and tell the teacher to run it)? It's genius!

Your task is to either expand or factorise the function listed. My hand's already dying, so I need to write as small a program as possible to save my fingers.

Input:

  • The input is either Expand {insert algebra here} or Factorise {insert algebra here}.
  • When the first word is Expand, you must expand the algebraic terms. You are guaranteed there won't be more than 5 terms.
    • In the example shown in the intro, the output is x^2+7x+6.
    • Some more inputs:
      • Expand (x+1)(x-1) = x^2-1
      • Expand x(x-1)(x+1) = x^3-x (notice the x on its own - your program must work for that.)
      • Expand (x+1)^2 = x^2+2x+1 (notice the ^2 on the outside of x+1 - your program must work for that.)
  • When the first word is Factorise, you must factorise the algebraic term.
    • In the example shown in the intro, the output is (x+1)^2.
    • Some more inputs:
      • Factorise x^2+3x+2 = (x+1)(x+2)
      • Factorise x^2+x = x(x+1) (if x alone is one of the factors, you must leave it outside the brackets.)
      • Factorise x^3+9x^2+26x+24 = (x+2)(x+3)(x+4) (there are more than 2 terms here - you are guaranteed that the maximum amount of terms is 5.)
      • Factorise x^2+2x+1 = (x+1)^2 (if there are multiple repeating terms, group them together - so no (x+1)(x+1), but (x+1)^2.

Specs:

  • The pronumeral will always be x.
  • The terms in the Expand bit always contain whole numbers from 1 to 20.
  • You are guaranteed that the algebra bit in the Factorise part will always be factorised into 5 or less terms, and all the numbers in each term will be whole, and from 1 to 20.
  • You are not allowed to use any built-in functions.

This is , so shortest program in bytes wins.

Just as a side-note: it would be appreciated if a pretty-printed output (i.e. With nice-looking exponents) - I'm looking at you, Mathematica - was included in an answer. You don't have to do it.

\$\endgroup\$

marked as duplicate by Mego, Blue, Loovjo, jimmy23013, Adnan code-golf Aug 26 '16 at 11:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ Just use WolframAlpha to help you. I think you can use the exact same syntax. \$\endgroup\$ – Leaky Nun Aug 10 '16 at 8:14
  • \$\begingroup\$ @LeakyNun Well, not anymore... no builtins. Also, is there anything that's vague or unclear about this challenge? \$\endgroup\$ – Qwerp-Derp Aug 10 '16 at 8:17
  • 9
    \$\begingroup\$ Half of this is a dupe of Factor a polynomial. The other half is close enough to count as a dupe of Multiply polynomials (it just wraps a loop). So there's not really anything new here, unless it's supposed to handle non-polynomials too (which isn't actually clear). \$\endgroup\$ – Peter Taylor Aug 10 '16 at 9:04

Browse other questions tagged or ask your own question.