patternjavaMinor
Numeric center in Java
Viewed 0 times
centerjavanumeric
Problem
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.
It works, but I have a feeling that I could do it much simpler.
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.
Solution
About the code itself(not the algorithm used):
-
Class name usually starts with a capital letter in Java.
-
Variables' names: it is better to use more meaningful names. For example, a name
-
Variable declaration and initialization: it is a good practice to make a scope of each variable as narrow as possible. It means that
-
If the number of iterations of a loop is known, it makes more sense to use
-
Surrounding operators by whitespaces makes the code more readable:
looks better than
-
You should use proper and consistent indentation(closing bracket indented by 4 spaces looks strange).
-
I would declare a magic number(204) as a
So an improved version of your code can look like this:
Now about the algorithm itself: let's assume that a candidate number is fixed(let's call it
$${n(n+1) \over 2} - {x(x+1) \over 2} = {x(x-1) \over 2}$$
It is a quadratic equation(with respect to
-
Class name usually starts with a capital letter in Java.
-
Variables' names: it is better to use more meaningful names. For example, a name
firstSum is much clearer then fsum. -
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.-
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. -
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++){-
You should use proper and consistent indentation(closing bracket indented by 4 spaces looks strange).
-
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.Code Snippets
for (int j = index + 1; j < index * 2; j++)for (int j=index+1;j<index*2;j++){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");
}
}
}Context
StackExchange Code Review Q#75450, answer score: 3
Revisions (0)
No revisions yet.