Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I got a proposed exercise from a Java2 book. This one wants me to find out the numeric centers.

A numeric center splits two list of numbers that make the same result when you add the numbers in each list. For example, 6 is the first numeric center because 1+2+3+4+5=15 and 7+8=15 too. The second numeric center is 35 because 1+2+3..+34=595 and 36+37+38+39=595 too.

 //I'll call the Numeric Center cn from here.
public class cnumerico {
    public static void main(String args[]){
        int n, index, fsum, ssum;
        n=204; //The higher number we would get.
        index=1; //No explanation need I guess. 
        fsum=0; //Stands for first sum.
        ssum=1; //Stands for second sum.
        do{
            for (int i=1;i<index;i++){
                fsum=fsum+i; //We make the first sum from 1 until it reaches the index.
                }
            for (int j=index+1;j<index*2;j++){
                if (ssum==fsum){
                    System.out.println(index+" es un centro numerico.");
                    break;
                    //In this if I check if the numbers are equal, if it is we found our cn so I stop the loop.
                    }else{
                        ssum=ssum+j;
                        }
                //If we still didn't find our cn this else does the sum from index(+1 because the cn musn't be in the sum).
                }
            index++;
            fsum=0;ssum=0; //Resetting the sums to start again.
        }while (index<n+1);
    }
}

It works, but I have a feeling that I could do it much simpler.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

About the code itself(not the algorithm used):

  1. Class name usually starts with a capital letter in Java.

  2. Variables' names: it is better to use more meaningful names. For example, a name firstSum is much clearer then fsum.

  3. Variable declaration and initialization: it is a good practice to make a scope of each variable as narrow as possible. It means that fsum and ssum should be declared inside the loop's body.

  4. If the number of iterations of a loop is known, it makes more sense to use for loop then do ... while. Conversely, when the number of iterations is not known, using while is better than using for and break.

  5. Surrounding operators by whitespaces makes the code more readable:

    for (int j = index + 1; j < index * 2; j++) 
    

    looks better than

    for (int j=index+1;j<index*2;j++){
    
  6. You should use proper and consistent indentation(closing bracket indented by 4 spaces looks strange).

  7. I would declare a magic number(204) as a final static field of this class, not as a local variable(and a name n is not good for it because it is not meaningful).

So an improved version of your code can look like this:

public class NumericalCenter {

    private final static int MAX_CANDIDATE = 204;

    public static void main(String args[]) {
        for (int candidate = 1; candidate <= MAX_CANDIDATE; candidate++) {
            int firstSum = 0;
            for (int i = 1; i < candidate; i++)
                firstSum += i;
            int secondSum = 0;
            int i = candidate + 1;
            while (secondSum < firstSum) {
                secondSum += i;
                i++;
            }
            if (firstSum == secondSum)
                System.out.println(candidate + " is a numerical center");
        }
    }
}

Now about the algorithm itself: let's assume that a candidate number is fixed(let's call it x). Then we get an equation: $${n(n+1) \over 2} - {x(x+1) \over 2} = {x(x-1) \over 2}$$

It is a quadratic equation(with respect to n), so it can be solved in O(1)(which gives O(MAX_CANDIDATE) time complexity in total). However, it is not necessary for MAX_CANDIDATE = 204.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.