+ 1
C++
please help. rekursif algorithma input : 6 row : 1 2 3 4 5 6 amount : 21 https://code.sololearn.com/cjyqD2qzoNdG/?ref=app
6 Answers
+ 8
Umm.
int ret_sum(int x)
{
if (x > 0) return (x + ret_sum(x-1));
return 0;
}
+ 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);
}
+ 1
You forgot one line in @Hasty's solution
+ 1
okay. I'm retry.
@Baptiste
0
@Hatsy Rei don't work.