Răspuns :
Explicație:
[tex]nr = 2k - 1 \iff k = \frac{nr + 1}{2} \\ [/tex]
[tex]S = 1 + 3 + 5 + 7 + ... + nr = \\ [/tex]
[tex] = 1 + 3 + 5 + 7 + ... + (2k - 1)\\ [/tex]
[tex]= (1 + 2 \cdot 0) + (1 + 2 \cdot 1) + (1 + 2 \cdot 2) + ... + [1 + 2 \cdot (k - 1)] \\ [/tex]
[tex]= \underbrace{1 + 1 + 1 + ... + 1}_{k} + 2 \cdot [0 + 1 + 2 + ... + (k - 1)] \\ [/tex]
[tex]= k + 2 \cdot \frac{(k-1)\cdot k}{2} = k + (k-1) \cdot k\\ [/tex]
[tex]= k \cdot (1 + k - 1) = \bf {k}^{2} = \bf \Big(\frac{nr + 1}{2}\Big) \\ [/tex]
► Varianta simpla, ciclu repetitiv
#include <iostream>
using namespace std;
int main() {
int n, suma=0;
cout << "Numar : ";
cin >> n;
for (int i = 1; i <= n; i += 2)
suma += i;
cout << "Suma este " << suma;
}
► Varianta eficienta, matematica
S=(1+2+3+...+nr) - (2+4+.....+nr-1)
S=(1+2+3+....+nr) - 2(1+2+...+(nr-1)/2)
◘ Stiind formula pentru suma gauss 1+2+...+x = x(x+1)/2 ajungem la
S = nr*(nr+1)/2 - (nr-1)/2*((nr-1)/2)+1)
#include <iostream>
using namespace std;
int main() {
int n, suma, nr;
cout << "Numar : ";
cin >> n;
if (n % 2 == 0) nr = n - 1;
else nr = n;
suma = nr * (nr + 1) / 2 - (nr - 1) / 2 * ((nr - 1) / 2 + 1);
cout << "Suma este " << suma;
}