-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path092. Number Chain end at 1 or 89.java
78 lines (65 loc) · 2.4 KB
/
092. Number Chain end at 1 or 89.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before.
For example,
44 → 32 → 13 → 10 → 1 → 1
85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89
Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop. What is most amazing is that EVERY starting number will eventually arrive at 1 or 89.
How many starting numbers below ten million will arrive at 89?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
//611 - 8581146
public class Problem92 {
int tbia[] = new int[10000000];
public void chain() {
int count=0;
int sq[] = new int[10];
for(int i=0;i<=9;i++) sq[i]=i*i;
Arrays.fill(tbia, -1);
tbia[89]=1;
ArrayList<Integer> bia;
int i = 1;
while(i<10000000){
if (tbia[i]==1) {
count++;
}else{
bia = new ArrayList<Integer>();
bia.add(i);
int i1=i;
while(true) {
int sui=0;
while(i1 > 0){
int d = i1%10;
sui+=sq[d];
i1/=10;
}
if (tbia[sui]==1) {
Add01(1, bia);
count++;
break;
} else if (tbia[sui]==0 || bia.contains(sui)) {
Add01(0, bia);
break;
} else {
i1=sui;
bia.add(sui);
}
}
}
i++;
}
System.out.println(count + " - " + i);
}
public void Add01(int a01, ArrayList<Integer> bia){
for(int b=0;b<bia.size();b++){
tbia[bia.get(b)]=a01;
}
}
public static void main(String[] args) {
long start = 0, end=0;
Problem92 p = new Problem92();
start =(Calendar.getInstance()).getTimeInMillis();
p.chain();
end =(Calendar.getInstance()).getTimeInMillis();
System.out.println(start + " - " + end + " - " +(end-start));
}
}