Computer Science, asked by subbulakshmis327, 5 months ago

algorithm to find the sum of series 2+4+6+.....+n​

Answers

Answered by MithunSanthosh
0

Answer:

preprocess the inputs.

the input series :

S=2−4+6−8+10−12…….NS=2−4+6−8+10−12…….N

where N is even.

=2(1−2+3−4+5−6+….N/2)=2(1−2+3−4+5−6+….N/2)

=2[(1+3+5+7…..)−(2+4+6+…)]=2[(1+3+5+7…..)−(2+4+6+…)]

Now if (N/2) is even then there are total of N/4 odd and N/4 even terms in the series. otherwise add one to the number of odd terms.

Case I: N/2 is even

no: of odd terms,n1=N/4;n1=N/4;

no: of even terms n2=N/4;n2=N/4;

Sum of odds=(n1)²=(N/4)²(n1)²=(N/4)²

Sum of evens=(n2)(n2+1)=(N/4)²+(N/4)(n2)(n2+1)=(N/4)²+(N/4)

Hence sum of the actual series.

=2[(N/4)²−(N/4)²−(N/4)]=2[(N/4)²−(N/4)²−(N/4)]

=−N/2=−N/2

CaseII : N/2 is odd

n1=N/4+1n1=N/4+1

n2=N/4n2=N/4

Sum of odds=(n1)²=(N/4)²+(N/2)+1(n1)²=(N<

Similar questions