Question about recursive even sum program [closed]?

Not that I like doing others' homework, but I'm interested whether I could solve that task quickly, so here's a C-like pseudocode which I haven't tested. Int evenSum( int number ) { if( number % 2 == 1 ) { return evenSum( number - 1 ); } if( number == 0 ) { return 0; } return number + evenSum( number - 2 ); }.

1 Sorry, this deserves a downvote. Solving other peoples’ homework = not cool. – Konrad Rudolph Jun 6 '10 at 11:59 @Konrad Rudolph: I know, but I found that challenging myself.

– sharptooth Jun 6 '10 at 12:00 6 @Konrad, you added the homework tag and, while you're probably right, I tend to prefer the view that people ask for what they want. If someone wants a solution, it is not my place to deny them. If they use it inappropriately, they'll either get caught out or they'll be zero threat to anyone in the workforce that isn't a lazy slob.

If OP had specified homework, that would hav ebeen a different matter, but I'm only one organism in the swarm here - others may think differently. – paxdiablo Jun 6 '10 at 12:07 1 @paxdiablo: Being a TA myself, I can only say that this attitude sucks. I’m not against providing help for homework (hey look!

I did so myself! ) but providing copypaste answers just supports antisocial behaviour. Rewarding such laziness (even the question is sloppy work, no effort has been put into at all)?

Just wrong. – Konrad Rudolph Jun 6 '10 at 12:36 1 @Konrad: I don't think we should ever downvote someone for giving a good answer to a question. Whether the question is homework or not, and the moral consequences of what the OP will do with the answer, are hardly sharptooth's responsibility.

If you dislike the situation, I say downvote the question. – Joren Jun 6 '10 at 18:06.

Initially, the solution is : int SumEven(int n) { return (n/2)*(1+(n/2)); } And to make it recursive: int SumEven(int n,unsigned int y) // y has completely no effect! { if(y==0) return (n/2)*(1+(n/2)); else return SumEvent(n,y-1) } (^_^).

Made me do a double take and read through the second time very carefully, love the changing smileys, bonus points if he submits this as the answer to his homework xD – suicideducky Jun 6 '10 at 12:25 I tried to use recursion in solving this problem... And that solution was the best I could think of.... – Betamoo Jun 6 '10 at 13:07.

This algorithm is quite easy. Try decomposing the problem: How would you add all numbers Now, just add a test in every recursive step to see whether the number is even.

It is relatively simple to write in c with a for loop sum = 0; for (int i=0; i.

(defun sum-twos (n) "This will sum the even numbers between 0 and N (inclusive)" (if (= 1 (mod n 2)) (sum-twos (- n 1)) (if (> n 0) (+ n (sum-twos (- n 2))) (+ n 0)))).

Tip: use cond instead of nested ifs. – Svante Jun 6 '10 at 12:37.

Algorithms only: def sumeven (n): if n F(n) is f(n-1) for all odd values of n. F(n) is n + f(n-1) for all even values of n. This can be optimised to skip odd values (other than the first), if necessary, by changing that last rule to: f(n) is n + f(n-2) for all even values of n.

Giving the algorithm: def sumeven (n): if n.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions