Problem with double sum on the calculation of Ricci tensor

I wrote a program in Mathematica to calculate the Riemann and Ricci tensors (obviously, passing trought Christoffel symbols first), however just later I thought of calculating Ricci tensor directily from the conextions, without calculating the Riemann tensor beforehand. The part of "Ricci tensor" (below) is doing it correctly, however the code of "calculating the Ricci tensor directly, without the Riemann tensor" gives the wrong answer. Yes, I already checked if the equations are correct... I think the problem is in the double sum made by the variable "rho" and "lambda", but I can't understand why at all...

 g = ({
{-Exp[2 α[r]], 0, 0, 0},
{0, Exp[2 β[r]], 0, 0},
{0, 0, r^2, 0},
{0, 0, 0, r^2 Sin[θ]^2}
}) ; T = ({
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
}); var = {t, r, θ, ϕ};
h = Inverse[g]; Print["Inverse Metric=" MatrixForm[h]];

Print[Style[
"Christoffel Symbol", {Bold, Underlined, Larger}]]; Γ[
i_, j_, k_] :=
Sum[1/2 h[[i,
l]] (D[g[[k, l]], var[[j]]] + D[g[[j, l]], var[[k]]] -
D[g[[k, j]], var[[l]]]), {l, 1, 4}]; i := 1; j := 1; k := 1;
For[i = 1, i <= 4, i++,
For[j = 1, j <= 4, j++,
For[If[j < k, k = j], k <= 4, k++,
If[Γ[i, j, k] =!=  0,
Print[Subscript[Γ, var[[j]], var[[k]]]^var[[i]],
"=", FullSimplify[Γ[i, j, k]]]]]]];

Print[Style["Riemann Tensor", {Bold, Underlined, Larger}]];
RiemannTensor[a_, b_, c_,
d_] := (D[Γ[a, d, b],
var[[c]]]) - (D[Γ[a, c, b],
var[[d]]]) + Γ[a, c, 1] Γ[1, d,
b] + Γ[a, c, 2] Γ[2, d,
b] + Γ[a, c, 3] Γ[3, d,
b] + Γ[a, c, 4] Γ[4, d,
b] - Γ[a, d, 1] Γ[1, b,
c] - Γ[a, d, 2] Γ[2, b,
c] - Γ[a, d, 3] Γ[3, b,
c] - Γ[a, d, 4] Γ[4, b,
c]; a := 1; b := 1; c := 1; d := 1;
For[a = 1, a <= 4, a++,
For[b = 1, b <= 4, b++,
For[c = 1, c <= 4, c++,
For[If[c < d, d = c], d <= 4, d++,
If[RiemannTensor[a, b, c, d] =!= 0,
Print[Subscript[R^var[[a]], var[[b]], var[[c]], var[[d]]], "=",
FullSimplify[RiemannTensor[a, b, c, d]]]]]]]];

Print[Style["Ricci Tensor", {Bold, Underlined, Larger}]];
RicciTensor[m_, n_] :=
Sum[RiemannTensor[soma, m, soma, n], {soma, 1, 4}]; m := 1; n := 1
For[m = 1, m <= 4, m++,
For[If[m < n, n = m], n <= 4, n++,
If[RicciTensor[m, n] =!= 0,
Print[Subscript[R, var[[m]], var[[n]]], "=",
FullSimplify[RicciTensor[m, n]]]]]];

Clear[RicciTensor];

Print[Style[
"Calculating Ricci Tensor directly, without Riemann Tensor", {Bold,
Underlined, Larger}]]; alpha := 1; beta := 1;
RicciTensor[alpha_, beta_] :=
Sum[D[Γ[rho, beta, alpha], var[[rho]]] -
D[Γ[rho, rho, alpha],
var[[beta]]] + Γ[rho, rho, lambda] Γ[
lambda, beta, alpha] - Γ[rho, beta,
lambda] Γ[lambda, rho, alpha], {lambda, 1, 4,
1}, {rho, 1, 4, 1}];
For[alpha = 1, alpha <= 4, alpha++,
For[If[alpha <= beta, beta = alpha], beta <= 4, beta++,
If[RicciTensor[alpha, beta] =!= 0,
Print[Subscript[R, var[[alpha]], var[[beta]]], "=",
FullSimplify[RicciTensor[alpha, beta]]]]]];

• it's a nice code and works properly. However, it's problematic in calculating Ricci tensor directly from Christoffel symbols. I replaced the suggested code by Michael, but it still doesn't work. I also don't understand why he suggested writing {rho, 1, 4, 1} instead of {rho, 1, 4} in the first summation for instance. That part of the code which doesn't work is the following: Thanks again! RicciTensor[m_, n_] := (Sum[ D[Γ[c, n, m], var[[c]]] - D[Γ[c, c, m], var[[n]]], {c, 1, 4, 1}] + Sum[Γ[c, c, d] Γ[d, n, m] - Γ[c, n, d] Γ[d, c, m], {d, 1, 4, 1}, {c, 1, 4, 1}]); m := 1; n := 1; For[m = 1, m <= Nov 22 '17 at 14:47

The first two sums should not be summed over $\lambda$; but since you've put them inside the double sum, Mathematica throws four copies of them in. Corrected code:
RicciTensor[alpha_, beta_] :=