Free ends and the energy

If we label the first spin as \( s_1 \) and the second as \( s_2 \) we obtain the following expression for the energy $$ \begin{equation*} E=-Js_1s_2. \end{equation*} $$ The calculation of the energy for the one-dimensional lattice with free ends for one specific spin-configuration can easily be implemented in the following lines

for ( j=1; j < N; j++) {
     energy += spin[j]*spin[j+1]; 
}

where the vector \( spin[] \) contains the spin value \( s_k=\pm 1 \).