Ask your own question, for FREE!
Mathematics 18 Online
OpenStudy (christos):

C++, Write a recursive function “Summation” that takes two integer numbers n and m and it returns the summation of all the numbers between n and m. If n > m the function returns zero. Examples: Summation(3, 7) = 3 + 4 + 5 + 6 + 7 = 25 Summation(7, 2) = 0 Summation(4,4) = 4 Summation(-3,1) = -5

OpenStudy (igigighjkl):

what grade is this (I'm in 6th).

OpenStudy (christos):

im not going to school

OpenStudy (igigighjkl):

are you on flvs?

OpenStudy (christos):

nope ;p

OpenStudy (igigighjkl):

o.o ?

OpenStudy (christos):

@ganeshie8

OpenStudy (christos):

can you help me with the problem plz

OpenStudy (igigighjkl):

ask Pooja 195.

OpenStudy (christos):

Thanks

OpenStudy (christos):

@pooja195

pooja195 (pooja195):

I don't know this stuff sorry! ;-;

OpenStudy (christos):

I see its ok

OpenStudy (igigighjkl):

... sorry.

OpenStudy (christos):

np

OpenStudy (dan815):

okay so basically

OpenStudy (dan815):

Summation(3, 7) this 7 has to keep going down by 1 until 3 for each recursion

OpenStudy (igigighjkl):

What type of math is this???

OpenStudy (christos):

int sum(int n,int m) { if ( n>m) return 0; else{ return n+sum(n,m-1); } } int main() { cout << sum(3,7); }

OpenStudy (christos):

closest I could come up with

OpenStudy (callisto):

return m+sum(n,m-1); ?

OpenStudy (christos):

results got better but still

OpenStudy (christos):

oh wait it worked

OpenStudy (christos):

How did you find out lol

OpenStudy (callisto):

If you return n+sum(n,m-1), you just keep adding n for (n-m+1) times.

OpenStudy (zarkon):

one more try ;)

OpenStudy (dan815):

sum(a,b) if b>a return b+sum(a,b-1) else return 0

OpenStudy (dan815):

sum(a,b) if b>a return a+sum(a+1,b) else return 0

OpenStudy (dan815):

this is okay too if u want to count up in each recursion its more natural

OpenStudy (dan815):

since thats how ur question is stated

ganeshie8 (ganeshie8):

\[S_n = a_n + S_{n-1}\]

OpenStudy (dan815):

http://prntscr.com/97l9uh

OpenStudy (callisto):

int sum(int n,int m) { if ( n>m) return 0; else{ return n+sum(n,m-1); } } sum (n,m) = n+sum(n,m-1) = n + (n + sum (n, m-2)) = 2n + sum(n, m-2) = 2n + (n + sum (n, m-3)) = 3n + sum (n, m-3) = ... = kn + (n + sum(n, m-(k+1)) = (k+1)n + sum(n, m-(k+1)) = ... Until m = n, we have = (n-1)n + (n + sum(n, m-n) = n*n + sum (n, m-n) = n*n + (n) + sum(n, m-(n+1)) = (n+1)n + 0 = (n+1)n So, you add n for n+1 times. Right? Still wrong?

OpenStudy (callisto):

Hey, I think counting up or counting down doesn't really matter. Both ways work. My concern is, how many times you added "n" in the asker's solution.

OpenStudy (dan815):

|dw:1448640402925:dw|

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!