import java.util.ArrayList;
import java.util.LinkedList;
class Narcistic extends Thread {
private int start;
private int end;
public Narcistic(int start, int end) {
this.start = start;
this.end = end;
}
public void run() {
for (int i = start; i <= end; i++) {
int n = i;
LinkedList<Integer> data = new LinkedList<>();
while (n > 0) {
data.push(n % 10);
n = n / 10;
}
int n1 = 0;
for (Integer num : data) {
n1 += Math.pow(num, data.size());
}
if (i == n1) {
System.out.print(i +" ");
}
}
}
}
public class example23 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
System.out.print("5-digit Armstrongs: ");
Narcistic t1 = new Narcistic(10000, 93000);
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("\n6-digit Armstrongs: ");
Narcistic t2 = new Narcistic(100000, 999999);
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("\n7-digit Armstrongs: ");
Narcistic t3 = new Narcistic(1000000, 9999999);
t3.start();
try {
t3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("\nRunning time on threads: " + (end - start) + " milliseconds");
}
} i want to give these results
5-digit Armstrongs: 54748, 92727, 93084
6-digit Armstrongs: 548834
7-digit Armstrongs: 1741725, 4210818, 9800817, 9926315