# How to define a summation with function calling itself?

I am new to Mathematica. I want to do this f(x)=summation f(t), summation is over t, where 1<=t<=x-1 and mod(x+1,t+1)=1, i.e. when x+1 is divided by t+1, remainder is 1 and f(1)=1. Should i use loop or something else and how? Thanks.

-

If I understand your question, you want something like:

f[1] = 1;
f[x_] := f[x] = Sum[Boole[Mod[x + 1, t + 1] == 1] f[t], {t, 1, x - 1}]

ListLinePlot[f[#] & /@ Range[1, 100], InterpolationOrder -> 0,
PlotRange -> {{1, 100}, All}, AxesOrigin -> {1, 0}]


Note the use of the :=...=... construct: this creates "memoization" of results (since the sum will repeatedly use the same values), providing a potentially huge performance boost.

-
Thanks a lot, it really works... –  suruchi Feb 8 at 18:31
@suruchi: Glad to help. That's an interesting function - does it have some special purpose? –  rasher Feb 9 at 3:11