Computing a Fourier Series using (discrete) Fast Fourier Transform
(1) Defining n=5 and generating a list X of 2^n equally spaced points in the interval :

n=5;
X = Table[2.0 Pi * i / 2^n, {i, 0, 2^n-1}]

(2) Define the function and sample it at these points to create an array of 2^n values:

f[x_] = x + (x-Pi)^2;
Y = f[X]

(3) Apply the Fast Fourier Transform to compute the coefficients of the Fourier series for our function

coeffs=Fourier[Y]/2^((n-2)/2)

Note that the scaling factor for 2^n input values would be 2^((n-2)/2)

(4) The The output of the FFT needs to be parsed into the Fourier coefficients a0, a_n, and b_n:

a0=Re[coeffs][[1]]/2
a=Re[coeffs][[2;;2^(n-1)]]
b=Im[coeffs][[2;;2^(n-1)]]

(5) Now we can define the Fourier series, using the coefficients we just computed:

fft[x_]=a0+Total[a*Table[Cos[i x],{i,1,2^(n-1)-1}]]+Total[b*Table[Sin[i*x],{i,1,2^(n-1)-1}]]

Finally, we check our approximate Fourier series by plotting it against the original function:

Plot[{fft[x],f[x]},{x,0,2Pi}]