Answer the question
In order to leave comments, you need to log in
How to write p-ary enumeration in case of searching for arithmetic progressions?
Good afternoon! Help with solving the problem, preferably with code.
N natural numbers are written into the array, no more than 20 in total. It is necessary to "delete" (that is, as I understand it, skip in the loop) some of them (no more than N-3) so that from all the remaining ones it is possible to compose a fragment of an arithmetic progression .
I tried to write it myself, but apparently I'm missing something and I can't understand why the algorithm doesn't search for all progressions.
Vector <Integer> result = new Vector<>();
int dif = 0;
for(int i = 0; i<len; i++) {
result.add(myArray[i]); //myArray содержит N чисел
for (int j = i+1; j < len; j++) {
dif = myArray[j]-myArray[i];
result.add(myArray[j]);
for(int k = j+1; k<len; k++) {
if ((myArray[k] - result.lastElement()) == dif) {
result.add(myArray[k]);
}
}
if (result.size() > 2) {
System.out.println(result.toString());
}
result.clear();
}
}
Answer the question
In order to leave comments, you need to log in
I'll answer myself, because I must have written an algorithm. Check it out.
for(int i=0; i<len; i++){
for(int j=i+1; j<len; j++){
result.add(myArray[i]);
dif = myArray[j]-myArray[i];
for(int k=j; k<len; k++) {
if (myArray[k] - result.lastElement() == dif) {
result.add(myArray[k]);
}
}
if (result.size() > 2) {
System.out.println(result.toString());
}
result.clear();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question