0

I want to find the sum of numbers that is divisible by x using recursive method

Ex if n= 10, x=3, the code should return sum of 3+6+9

Write a recursive method sumDivByX(n, x), which finds the sum of all numbers from 0 to n that are divisible by x.

I asked my teacher about it and he told me "Firstly, total should be global. You should return 0 if n or x == 0. I only care if n is divisible by x. So I only add n to total (total+=n) if (n%x==0) otherwise do nothing. And do recursion sumDivByX(n-1,x) and return total as usual." I tried to correct it.

public static int sumDivByX(int n, int x) {
    int total = 0;
    if (n == 0 || x == 0) {
        return -1;
    }
    if (n % x >= 1) {
        return total = 0;
    } else if (n % x == 0) {
        return total += n;
    }
    return total + sumDivByX(n - 1, x);

}

When I run the program I get 0.

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
Songul
  • 45
  • 1
  • 3
  • 9

4 Answers4

3

Eliminate the returns inside your second and third if statements

public static int sumDivByX(int n, int x) {
    int total = 0;
    if (n == 0 || x == 0) {
        return 0;
    }
    if (n % x >= 1) {
        total = 0;
    } else if (n % x == 0) {
        total += n;
    }
    return total + sumDivByX(n - 1, x);

}

For a cuter, more compact version

public static int sumDivByX(int n, int x) {
    if (n == 0 || x == 0) {
        return 0;
    }
    return (n % x == 0 ? n : 0) + sumDivByX(n - 1, x);
}

Note - depending on the semantics you intend, you might want to have separate checks for x<=0 (possibly and error?) and n==0 (base case).

C.B.
  • 8,096
  • 5
  • 20
  • 34
  • I tested that code, and 3+6+9 should be 18 but it gives 17 instead, C.B. it would be fixed at returning 0 instead of -1 on 1st if – Frakcool Mar 27 '14 at 19:50
  • i tried to run the program by doing n=10 and x=3 the answer should be 18 but i get 17 @C.B – Songul Mar 27 '14 at 19:50
  • @user3469667 Depends on what you intend to happen. I've changed it to return 0 (base case), but if `x==0` is an error you should have that separeate – C.B. Mar 27 '14 at 19:52
  • and since this answer is the one that works, I highly recommend @user3469667 that if any answer works for you, check them as accepted, it's a way of thanking people who helped you – Frakcool Mar 27 '14 at 19:57
0

Step through your code and you'll see that it never recurses when n ==10 and x==3, since (10 % 3 == 1)

Dancrumb
  • 26,597
  • 10
  • 74
  • 130
0

When a method gets to a "return" statement it ends, in your case at the second if.

Your total is initialized by 0 everytime the method runs, so you should consider making it global.

Your method generates an exception if you try to use negative numbers as paramethers

Try this:

int total=0;
public static int subDivByX(int n, int X) {
   if (n>0 && x>0) {
      if (n%x==0){
          total += n;
      }
      return sumDivByX(n-1,x);
   } 
   else return -1;
}
Alexandru Severin
  • 6,021
  • 11
  • 48
  • 71
0

This seems to work

private static int sumDivByX(int n,int x) {
    if (n < x || x < 1 ) {
        return 0;
    }
    int d = n/x;
    return (x * d) + sumDivByX(n - x , x);
}

Recursion could cause a stackoverflow.

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65