There is a "problem" in:
sum = 2*n+1(on the "if" just after the loop)
sum = 2*n+1 actually sets the value of sum to 2*n+1, and then evaluates if that is "true"(different from 0) or false(equals 0) .This is not "wrong" in the sense that it is actually valid code, however it is not intended behavior. To fix it you should put "==" instead of "=", because that will actually compare the numbers;