-
Notifications
You must be signed in to change notification settings - Fork 0
/
CantorDiagonal.java
85 lines (70 loc) · 2.3 KB
/
CantorDiagonal.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
79
80
81
82
83
84
85
package Chp00;
import java.util.ArrayList;
/*
a simulation of restricted/limited Cantor diagonalisation
with finite decimals, e.g. 10.
*/
/*
interesting facts:
1 count after first repeat of generated number is 10*numOfDigits - numOfExisting number
2 the first decimal of first existing number e.g. 1 of 0.14... has strongest effect on
the corresponding decimal of first repeating number, usually +1
*/
public class CantorDiagonal {
public static String pi= "0.1415926536";
public static String ee= "0.7182818284";//decimal part of e
public static String st= "0.4142135624";// of sqrt 2
public static String fi= "0.5000000000";
public static String gi= "0.6111111111";
public static String hi= "0.9876543210";
public static String num;//new generated number
public static ArrayList list;//to store numbers
public static int count=0;
public static int compute(int numOfDigits){
boolean addable = true;
int count2=0;
while (addable){
num = "0.";
int ptr = (list.size()<=numOfDigits)? 0 :(list.size()-numOfDigits);//list index pointer
int ptr2 = numOfDigits-(list.size()-ptr)+2;//0to9 string index pointer
for (int i = ptr; i < list.size(); i++) {
String temp= (String) list.get(i);
int temp2 = Character.getNumericValue(temp.charAt(ptr2));
num += (temp2+1)%9;
}
if (list.size()<numOfDigits){
for (int i = 0; i <numOfDigits-list.size() ; i++) {
num += "0";
}
}
if (list.contains(num)){
addable = false;
System.out.println("Repeat: "+num);
}else {
list.add(num);
count++;
System.out.println(num);
}
}
return count;
}
public static void main(String[] args) {
list = new ArrayList();
list.add(pi);
// list.add(ee);
// list.add(st);
// list.add(fi);
// list.add(gi);
// list.add(hi);
int digi = compute(10);
System.out.println("--------------");
System.out.println(digi);
}
}
/*
0.2856842736 pi
0.8614131413 ee
0.5648753463 st
0.6222222222 fi
0.7333333333 gi
0.1234567801 hi*/