+ 1

C++

please help. rekursif algorithma input : 6 row : 1 2 3 4 5 6 amount : 21 https://code.sololearn.com/cjyqD2qzoNdG/?ref=app

26th Dec 2017, 7:45 AM
Rian Pratama
Rian Pratama - avatar
6 Answers
+ 8
Umm. int ret_sum(int x) { if (x > 0) return (x + ret_sum(x-1)); return 0; }
26th Dec 2017, 7:49 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Tail recursion, to prevent stack overflow : unsigned ssum(unsigned n, unsigned i){ if(n) return ssum(n - 1, i + n); return i; } unsigned sum(unsigned n){ return ssum(n,0); }
26th Dec 2017, 7:54 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
You forgot one line in @Hasty's solution
26th Dec 2017, 8:26 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
okay. I'm retry. @Baptiste
26th Dec 2017, 8:32 AM
Rian Pratama
Rian Pratama - avatar
0
@Hatsy Rei don't work.
26th Dec 2017, 7:59 AM
Rian Pratama
Rian Pratama - avatar
26th Dec 2017, 8:06 AM
Rian Pratama
Rian Pratama - avatar