开发者

sum of series 1*3-3*5+5*7 [closed]

开发者 https://www.devze.com 2022-12-13 17:36 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its开发者_如何学Python current f
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its开发者_如何学Python current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

please help to print series as well sum of series like 1*3-3*5+5*7 up to n terms i have used code like this in php

class series {
    function ser(){
        $i = 0;
        $k = 3;
        $m = 0;

        for($j = 1; $j < 3; $j++) {
            if($j % 2 == 0 ) {
                $m = $i + ($i * $k);
            } else {
                $m=$m-($i*$k);

            }
        }

        //$m = $m + $i;
        echo "$m";
    }
}

$se = new series();
$se->ser();

Just i have tested for 2 times


With a few simple operations one can find a formula for the sum S. If n is even (sum Se) adding pairs of terms yields

 Se = (1*3 - 3*5) + (5*7 - 7*9) + (9*11 - 11*13) ...
 Se = -4*(  3 + 7 + 11 + ...  )

The terms in the parenthesis can be splitted and summed up:

 Se = -4*( 1+2 + 3+4 + 5+6 + ...  )
 Se = -4*( n*(n+1)/2 )
 Se = -2*n*(n+1)

If n is odd (sum So) the last term must be added to Se:

 So = Se + 4*n*n-1
 So = +2*n*(n+1) - 1

The implementation in C:

int series ( unsigned int n )
{
  if ( n%2 == 0 )
    return -2*n*(n+1);
  else
    return +2*n*(n+1) - 1;
}


This is probably homework, but here goes anyway. Hopefully you learn something from this.

The code above is horrible. Over-complicated for nothing... Here's a very simple version for you. I have no idea of what language this is in, but I'll do something similar for you... Go get a book on programming, that will be a wise investment of your time.

function my_sum(int $count) {
    $result = 0;
    $sign = 1;
    for ($i=1; $i<=$count; $i++) {
        $result = $result + $sign * (2*$i-1) * (2*$i+1);
        $sign = - $sign;
    }
    return $result;
}

Hope this helps... You probably get the idea with this.


I prefer the recursive function and by this way you can stackoverflow (woot!) :) :

public static int serie(int n){
    if(n<1){
        return 0;
    }else{
        return (n%2==0?-1:1)*(4*n*n-1)+serie(n-1);
    }
}


Or, use the following to compute the first n terms of your series. Sorry haven't figured out how to make SO display LaTeX properly, perhaps someone can edit it for me, but if you do please leave a comment with instructions please !

\frac{1}{2} \left(-4 (-1)^n n^2-4 (-1)^n n+(-1)^n-1\right)

Or, as generated by the wonderful EquationSheet.com:

sum of series 1*3-3*5+5*7 [closed]


(source: equationsheet.com)

0

精彩评论

暂无评论...
验证码 换一张
取 消