Apply an indefinite integral to a given string. The only rules you will be using are defined as such:

∫cx^(n)dx = (c/(n+1))x^(n+1) + C, n ≠ -1
c, C, and n are all constants.


  • You must be able to integrate polynomials with any of the possible features:
    • A coefficient, possibly a fraction in the format (numerator/denominator).
    • Recognition that e and π are constants, and in their use, be able to form fractions or expressions containing them (can be held in a fraction like (e/denominator) or (numerator/e), or, if in exponents, x^(e+1))
      • Aside of these two special constants, all coefficients will be rational, real numbers.
    • An exponent, possibly a fraction, in the format x^(exponent)
      • Expressions with e or π in them, aside of themselves, will not be in exponents. (you will not have to integrate stuff like x^(e+1), but you might integrate x^(e))
    • Can use non-x 1-char variables (i.e. f)
      • This is only for ASCII ranges 65-90 and 97-122.
    • You do not have to use chain rule or integrate x^(-1).
  • Output must have padding (separation between terms, i.e. x^2 + x + C.
  • If it is unknown how to integrate with the above features, the program should print out "Cannot integrate "+input.
  • It must be a full program.


  • -10% if you print out the "pretty" exponents formatted for markdown (instead of x^2, x<sup>2</sup>).
  • -10% if you print out the equation (i.e. ∫xdx = (1/2)x^2 + C)





(1/2)x^(2) + C




f^(-1) + C


(1/7)x^(1/7) + 5


(1/56)x^(8/7) + 5x + C




(π/(e+1))x^(e+1) + C




Cannot integrate (f+1)^(-1)
  • 1
    \$\begingroup\$ Surprised we don't already have this question - but I couldn't find a dup. +1 \$\endgroup\$ – Digital Trauma Dec 15 '15 at 21:34
  • 3
    \$\begingroup\$ 1. I presume that other than e and π, the only values in coefficients will be rational numbers? I.e. it's not necessary to handle multivariable polynomials? 2. When you say "non-x 1-char variables", are you restricting to a-zA-Z or do you intend to include other Unicode ranges? \$\endgroup\$ – Peter Taylor Dec 15 '15 at 21:36
  • 1
    \$\begingroup\$ Do you think there should be a bonus if someone's program prints ln(x) + C for an input of x^(-1)? \$\endgroup\$ – Arcturus Dec 15 '15 at 21:38
  • 1
    \$\begingroup\$ @Ampora No - that opens up a whole can of worms dealing with coefficients of ln. \$\endgroup\$ – Addison Crump Dec 15 '15 at 21:39
  • 1
    \$\begingroup\$ @LeifWillerts 1) I meant that x^(e+1) will not be an integrand, but it may be the result of an integration. 2) There will not be multiple letter variables. 3) Yes. 4) Yes, but it should be (1/56)x^(1/7+1) + C (I made a mistake in the examples). \$\endgroup\$ – Addison Crump Feb 2 '16 at 13:31

Mathematica 478 * 0.9 = 430.2

φ=(α=ToExpression;Π=StringReplace;σ="Cannot integrate "<>#1;Λ=DeleteDuplicates@StringCases[#1,RegularExpression["[a-df-zA-Z]+"]];μ=Length@Λ;If[μ>1,σ,If[μ<1,Λ="x",Λ=Λ[[1]]];Ψ=α@Π[#1,{"e"->" E ","π"->" π "}];Φ=α@Λ;Θ=α@Π[#1,{"e"->" 2 ","π"->" 2 "}];λ=Exponent[Θ,Φ,List];Θ=Simplify[Θ*Φ^Max@@Abs@λ];Θ=PowerExpand[Θ/.Φ->Φ^LCM@@Denominator@λ];If[Coefficient[Ψ,Φ,-1]==0&&PolynomialQ[Θ,Φ],"∫("<>#1<>")d"<>Λ<>" = "<>Π[ToString[Integrate[Ψ,Φ],InputForm],{"E"->"e","Pi"->"π"}]<>" + C",σ]])&

This creates a true function φ that takes one String as Input. (Does that count as complete program for Mathematica?)

The ungolfed version would be:

    σ="Cannot integrate "<>#1;
        Ψ=ToExpression@StringReplace[#1,{"e"->" E ","π"->" π "}];
        Θ=ToExpression@StringReplace[#1,{"e"->" 2 ","π"->" 2 "}];
            "∫("<>#1<>")d"<>Λ<>" = "<>StringReplace[ToString[Integrate[Ψ,Φ],InputForm],{"E"->"e","Pi"->"π"}]<>" + C",

Note that the Greek letters are necessary to be able to use all the other letters in the input.

| improve this answer | |

MATLAB, 646 x 0.9 = 581.4 bytes

I=int(sym(strsplit(s,' + ')),r);S=[];for i=I
S=[S char(i) ' + '];end
b=0;o=[];for i=1:nnz(S)
o=[o c];end
o=regexprep(char([8747 40 t ')d' r ' = ' o 67]),{'pi','exp\(1\)','exp','\^([^\(])',['1/' r]},{p,'e','e^','^($1)',[r '^(-1)']});catch
if e||~isempty(strfind(o,'log'))
disp(['Cannot integrate ' t]);else

This is currently a work-in-progress using MATLABs built in symbolic integration capabilities. Currently the requirements have been updated so the format now matches the requirements. It also does qualify for the second -10% bonus.

If anyone wants to pitch in and suggest ways of correcting the output, or use this code as a basis for another answer, feel free :). If I can find the time, I'll keep playing with it and see if I can think how to reformat the output.

Update: Ok, so after a bit more work, here is how the code currently stands. It is still a work in progress, but now getting closer to matching the required output.

t=input('','s'); %Get input as a string
p=char(960); %Pi character
s=regexprep(t,{p,'pi([a-zA-Z])','([a-zA-Z])pi','([\)e\d])([a-zA-Z])','([a-zA-Z])(([\(\d]|pi))','e^(\(.+?\))','e'},{'pi','pi*$1','$1*pi','$1*$2','$1*$2','exp($1)','exp(1)'}); %Reformat input to work with built in symbolic integration
r=[s(regexp(s,'\<[a-zA-Z]\>')),'x'];r=r(1); %determine the variable we are integrating
e=0; %Assume success
    I=int(sym(strsplit(s,' + ')),r); %Integrate each term seperately to avoid unwanted simplificaiton
    for i=I
        S=[S char(i) ' + ']; %Recombine integrated terms
    %Now postprocess the output to try and match the requirements
    for i=1:nnz(S)
        %Work through the integrated string character by character
        b=b+(c=='(')-(c==')'); %Keep track of how many layers deep of brackets we are in
        if(c=='*'&&S(i+1)==r)||(b&&c==' ') %If a '*' sign preceeds a variable. Also deblank string.
            c=''; %Delete this character
        o=[o c]; %merge into new output string.
    o=regexprep([char(8747) '(' t ')d' r ' = ' o 'C'],{'pi','exp\(1\)','exp','\^([^\(])',['1/' r]},{p,'e','e^','^($1)',[r '^(-1)']});
    e=1; %failed to integrate
if e||~isempty(strfind(o,'log'))
    disp(['Cannot integrate ' t])  %bit of a hack - matlab can integrate 1/x, so if we get a log, we pretend it didn't work.
    disp(o)% Display it.

Here are some examples of what it currently produces. As you can see, it's not quite right, but getting closer.


(1/7)x^(1/7) + 5


∫(x)dx = x^(2)/2 + C
∫(-f^(-2))df = f^(-1) + C
∫((1/7)x^(1/7) + 5)dx = x^(8/7)/8 + 5x + C
∫(πx^(e))dx = (πx^(e+1))/(e+1) + C
Cannot integrate (f+1)^(-1)
| improve this answer | |
  • \$\begingroup\$ I assume the problem with output that you're having is that the fractions don't simplify/go into a single coefficient? \$\endgroup\$ – Addison Crump Dec 20 '15 at 18:13
  • \$\begingroup\$ @FlagAsSpam, the fractions are simplifying, but the trouble is they end up on the wrong side of the variable. For instance in the third example it results in x^(8/7)/8 which while mathematically correct is not in the form you want it - (1/8)x^(8/7). \$\endgroup\$ – Tom Carpenter Dec 20 '15 at 18:27
  • \$\begingroup\$ Considering that you are the only answer so far, I might consider changing that if no more answers come through in a day or two to "any mathematically correct, valid output" for fractions. \$\endgroup\$ – Addison Crump Dec 20 '15 at 18:46
  • \$\begingroup\$ Your answer is valid - You do not have to simplify fractional output anymore. c: \$\endgroup\$ – Addison Crump Dec 21 '15 at 21:46
  • \$\begingroup\$ I will golf it down a bit then and count the bytes. \$\endgroup\$ – Tom Carpenter Dec 22 '15 at 6:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.