This answer is mostly for students who used an algebra approach. I don't know if Fourier himself thought up the series this way, but it is common today. I left a lot of steps out and mainly showed ideas that I struggled with when I first tried to motivate the Fourier Series.
I'll start off by observing a trigonometric polynomial:
$T(x) = c_0 + c_1 \cos(x) + c_2 \cos(2x)+...+c_n \cos(n x) + d_1 \sin(x) + ... + d_n \sin(n x)$
where $c_n$ and $d_n$ are some non-zero value.
The goal is write the orthogonal basis, from there I can find the coefficients.
So, I need declare the inner product:
$<\textbf{f},\textbf{g}> = \int_0^{2\pi} f(x)g(x)dx$
Before I can obtain a orthogonal basis, I should first get the orthonormal basis by using the Gram–Schmidt process. Where $\|\textbf{g}_0\| = \|\textbf{g}_1\| =\|\textbf{g}_2\| = ... =\|\textbf{g}_n\| = 1$.
$\|\textbf{f}\|^2 = <\textbf{f}><\textbf{f}> = 2\pi$
Thus, $\|\textbf{f}\| = \sqrt{2\pi}$
and
$e_0 = \frac{\textbf{f}}{\|\textbf{f}\|}$
$\textbf{g}_0 = e_0 = \frac{1}{\sqrt{2\pi}}$
$\textbf{g}_1 = e_1 = \frac{1}{\sqrt{\pi}}\cos(x)$
...
$\textbf{g}_n = e_n = \frac{1}{\sqrt{\pi}}\cos(nx)$...
$\textbf{g}_{n+1} = e_{n+1} = \frac{1}{\sqrt{\pi}}\sin((n+1)x)$...
$\textbf{g}_{2n} = e_{2n} = \frac{1}{\sqrt{\pi}}\sin(nx)$
The orthogonal basis yields:
$a_0 = \frac{2}{\sqrt{2\pi}}<\textbf{f},\textbf{g}_0>$ (Use 2 because it makes generalizing the coefficients possible.)
$a_1 = \frac{1}{\sqrt{\pi}}<\textbf{f},\textbf{g}_1>$...
$a_n = \frac{1}{\sqrt{\pi}}<\textbf{f},\textbf{g}_n>$...
$b_n = \frac{1}{\sqrt{\pi}}<\textbf{f},\textbf{g}_{2n}>$
Now that everything is divided into sines and cosines I can get the coefficients:
$a_n = \frac{1}{\pi}\int_0^{2\pi} f(x)\cos(nx)dx $
and
$b_n = \frac{1}{\pi}\int_0^{2\pi} f(x)\sin(nx)dx $
For the specific case of interest where
$$f(x)=2^x\tag{1}$$
the corresponding Fourier series is
$$\tilde{f}(x)=\frac{1}{\log (2)}+\underset{K\to \infty }{\text{lim}}\left(\sum\limits_{n=1}^K \left(\frac{\log(4)}{4 \pi^2 n^2+\log^2(2)}\, \cos(2 \pi n x)-\frac{4 \pi n}{4 \pi^2 n^2+\log^2(2)}\, \sin(2 \pi n x)\right)\right)\tag{2}$$
which is valid on the interval $0<x<1$.
Figure (1) below illustrates formula (2) for $\tilde{f}(x)$ in orange overlaid on the blue reference function $f(x)=2^x$ where formula (2) is evaluated at $K=100$.
Figure (1): Illustration of formula (2) for $\tilde{f}(x)$ in orange overlaid on $f(x)=2^x$ in blue
Best Answer
since your function is $2\pi$ periodic you only need any intervall of the length $2\pi$. As the following shows: Let $a\in\mathbb R$ then we can write $a=2\pi k+r$ with $r\in[0,2\pi)$ and $k\in \mathbb Z$.
$$\pi a_n=\int_a^{a+2\pi}f(x)\text{cos}(nx)dx=\int_{2\pi k+r}^{2\pi(k+1)+r}f(x)\text{cos}(nx)dx=\int_r^{2\pi +r}f(x+2\pi k)\text{cos}(nx+2\pi nk)dx$$ Where the last step is just an easy substitution. Now we use that $f$ is $2\pi$ periodic, so we get: $$\pi a_n=\int_r^{2\pi +r}f(x)\text{cos}(nx) dx=\int_r^{2\pi}f(x)\text{cos}(nx)dx+\int_{2\pi}^{2\pi+r}f(x)\text{cos}(nx)dx$$ Now we use again the periodicity on the right hand term: $$\int_{2\pi}^{2\pi+r}f(x)\text{cos}(nx)dx=\int_{0}^{r}f(x+2\pi)\text{cos}(nx+2\pi n)dx=\int_{0}^{r}f(x)\text{cos}(nx)dx$$ Combined it follows: $$\pi a_n=\int_a^{a+2\pi}f(x)\text{cos}(nx)dx=\int_0^{2\pi}f(x)\text{cos}(nx)dx$$
As you can see, the acutal interval ist not that important, you only need an interval of leingh $2\pi$.