Answer the question
In order to leave comments, you need to log in
How can the program (algorithm) for finding vampire numbers be improved? Is it possible?
In general, I read "Philosophy of Java". And there is a task to search for four-digit vampire numbers. I did it, but I think it's rather clumsy. It became interesting whether it is possible to improve this program.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
String first_pathStr,second_pathStr;
ArrayList<Integer> arrayList1 = new ArrayList<>();
ArrayList<Integer> arrayList2 = new ArrayList<>();
int first_pathInt,second_pathInt;
char [] cDate; // массив для записи цифр по отдельности из ХХХХ числа
for (int i = 1000; i < 10000 ; i++) {
String s = Integer.toString(i); // переводим i-тую переменную в строку
cDate = s.toCharArray(); // разбивает строку посимвольно и записываем в массив.
for(int j = 0; j <4;j++){
for(int o = j+1; o <4;o++){
first_pathStr = ""+cDate[j] + cDate[o];
second_pathStr = ""+cDate[o] + cDate[j];
first_pathInt = Integer.parseInt(first_pathStr);
second_pathInt = Integer.parseInt(second_pathStr);
arrayList1.add(first_pathInt);
arrayList2.add(second_pathInt);
}}
for(int u = 0,t_i = 5; u < arrayList1.size();u++,t_i--){
if(arrayList1.get(u) * arrayList1.get(t_i) == i || arrayList1.get(u)*arrayList2.get(t_i) == i)
System.out.println(i);
if( arrayList2.get(u)*arrayList2.get(t_i) == i)
System.out.println(i);
}
arrayList1.clear();
arrayList2.clear();
}
}}
Answer the question
In order to leave comments, you need to log in
The best thing to do with this piece of ... code is to make me unsee it.
Learn cycles. Learn to name variables meaningfully.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question