I have this function

$$f(x)=\sum_{k=1}^{\lfloor x\rfloor}\tan(k),\quad x\ge 1$$

Then I tried to plot several graphs with this code


but after half hour or so the computer hang on (memory issues) and I restarted it. Clearly the code is not the more efficient for this task.

My question is: can you give me some tips to improve the performance of mathematica to draw this function? By example, it is possible to define a recursion, instead of a function, and draw it? It seems more appropriate to use a recursion instead of a sum to draw this function.


1 Answer 1


One possibility is to use Accumulate and Interpolation to create the function:

if = Interpolation[Accumulate @ Tan[N @ Range[10^8]], InterpolationOrder->0]; //AbsoluteTiming

{79.0832, Null}

Now, we can plot if over various domains:

Plot[if[t], {t, 1, 10^2}]

enter image description here

Plot[if[t], {t, 1, 10^3}]

enter image description here

Plot[if[t], {t, 1, 10^8}]

enter image description here

If you need to go higher, then you will need to create a new InterpolatingFunction starting at 10^8.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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