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.