-1

I'm attempting to add all the multiples of 3 or 5 under 1000 using a recursive method.

Here's my code

public static void main(String args[]){

    //TODO: Find all the multiples of 3 or 5 below 1000

    //Functional


    System.out.println(addMultiples(1, 0));


    /*

    //Imperative solution

    int num = 0;

    for(int i = 1; i < 1000; i++){
        if(i % 3 == 0 || i % 5 == 0){
            System.out.println(i + " is a multiple of 3 or 5.");
            num += i;
        }
    }
    System.out.printf("The sum of all the multiples of 3 or 5 below 1000 is %d", num);
    */

}

public static int addMultiples(int num, int base){

    //Exit case
    if (num >= 1000)
        return base;
    else if (num % 3 == 0 || num % 5 == 0) 
        base += num;


    addMultiples(++num, base);

    //I was forced to return a  -1. Compiler said. :(
    return -1;
}

Why is this code returning a -1? It's clear that if num >= 1000, the recursion would stop. I tried putting a System.out.println(base) inside my exit condition and it was printing out the number that I wanted.

Also, why did the compiler tell me to return a -1?

mpmp
  • 2,409
  • 4
  • 33
  • 46

2 Answers2

4

Eventually the base case does get called and it returns base, whatever that is. But all calls below it in the stack ignore it and return -1.

Just have the recursive case return whatever the recursive call returns:

return addMultiples(++num, base);
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Can you please explain further what I did wrong. I understand that the exit case that I wanted was indeed executed but why did the stack ignore it? – mpmp May 14 '14 at 17:09
  • @MiguelPortugal, `return -1` is the main mistake you did. – Rahul May 14 '14 at 17:10
  • @MiguelPortugal You aren't using the return value. You're only invoking the method and not using the return value. – gparyani May 14 '14 at 17:10
  • @MiguelPortugal The stack didn't ignore it; you ignored it with `addMultiples(++num, base);`. – rgettman May 14 '14 at 17:11
  • 1
    I think the OP does not understand that Java uses pass-by-value for primitives – Ordous May 14 '14 at 17:11
  • Got it. Oh man. I fail so bad. I just wrote a diagram showing what I did... Thank you guys. I'm currently working with Scala... kind of messed my knowledge in Java. – mpmp May 14 '14 at 17:13
  • @Ordous It's a different problem, having to do with the return value, not the parameters. The OP's code ignores the return value instead of returning it. – rgettman May 14 '14 at 17:13
  • @Ordous Java uses pass-by-value for primitives and pass-by-reference for objects...that's confusing, isn't it? – gparyani May 14 '14 at 17:14
  • @MiguelPortugal Please accept the answer if you find it useful. – gparyani May 14 '14 at 17:15
  • @damryfbfnetsi Please see [Is Java “pass-by-reference” or “pass-by-value”?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) for clarification. – rgettman May 14 '14 at 17:16
  • @rgettman I know the difference; I was just referring to the point of view of a newbie. – gparyani May 14 '14 at 17:17
  • well when you say `passbyref` unless you are actually passing the ref; you are actually coping the reference which is passbyval. – Rahul May 14 '14 at 17:19
3

The answer is simple. Replace the following code

addMultiples(++num, base);
return -1;

with this:

return addMultiples(++num, base);

What you're doing in your code is that you are simply invoking the recursive method again and discarding its return value, and returning -1 if the base case is not satisfied. In the new code, you're actually using the return value and properly invoking the recursive method.

gparyani
  • 1,958
  • 3
  • 26
  • 39