Answer:
Step-by-step explanation:
Solution:
- We are to write a program for evaluating the sum to Nth of an arithmetic sequence such that the sequence starts from positive integer 1, 3 , 5 , 7 , .. n.
- The sum to nth for the arithmetic series is given by two parameters i.e first integer a = 1 and the distance between successive integers d = 2 in our case.
- For any general distance d we can write our sum to nth as:
Sum to nth = a + (a+d) + (a+2*d) + (a+3*d) .... (a + (n-1)*d)
- From above sequence we can see that every successive number is increased by distance d and added in previous answer.
- We will use an iteration loop for a variable "sum", which is cycled by a "range ( , , )" function.
- The parameters of the range functions corresponds to:
range ( first integer , last integer , step size )
range ( a , n + 1 , d )
- Then we can cast the loop as follows:
" int sum = 0
int d = 2
int a = 1
for i in range ( a , n + 1 , d )
sum += i
"
- We see that iteration parameter i starts from a = 1, with step size d = 2 and the sum is previously stored sum value plus i for the current loop.