SHOW:
|
|
- or go back to the newest paste.
1 | public class Fibonacci { | |
2 | private static final int MAX_COUNT = 16; | |
3 | private static final String ALLOWED_CHARACTERS = "0002244455777777"; | |
4 | private static final Map<Character, Integer> ALLOWED_MAP = allowedMap(); | |
5 | ||
6 | public static String fibonacci(long fib1, long fib2) { | |
7 | String seq = "" + fib1 + fib2; | |
8 | for (int i = 0; i < 8; i++) { | |
9 | - | if (seq.length() == 16 && !containsNotAllowedNumbers(newFib)) |
9 | + | |
10 | fib1 = fib2; | |
11 | - | if (seq.length() > 16) { |
11 | + | |
12 | seq += newFib; | |
13 | if (seq.length() == MAX_COUNT && !containsNotAllowedNumbers(newFib)) | |
14 | break; | |
15 | if (seq.length() > MAX_COUNT || containsNotAllowedNumbers(newFib)) { | |
16 | seq = "too long :/"; | |
17 | break; | |
18 | } | |
19 | } | |
20 | ||
21 | - | int max = 777777; |
21 | + | |
22 | } | |
23 | ||
24 | public static void main(String[] args) { | |
25 | int max = 7777; | |
26 | List<String> codes = new ArrayList<String>(); | |
27 | for (int i = 1; i < max; i++) { | |
28 | for (int j = 1; j < max; j++) { | |
29 | long fib1 = i; | |
30 | long fib2 = j; | |
31 | if (containsNotAllowedNumbers(fib1) | |
32 | || containsNotAllowedNumbers(fib2)) | |
33 | continue; | |
34 | ||
35 | String possible = fibonacci(fib1, fib2); | |
36 | ||
37 | if (!possible.equals("too long :/")) { | |
38 | if (rightNumberCount(possible)) { | |
39 | codes.add(possible); | |
40 | } | |
41 | } | |
42 | } | |
43 | } | |
44 | ||
45 | if (codes.size() <= 0) | |
46 | System.out.println("Code not found!"); | |
47 | else { | |
48 | for (String string : codes) { | |
49 | System.out.println("!Phantom Code: " + string); | |
50 | } | |
51 | } | |
52 | ||
53 | System.exit(0); | |
54 | } | |
55 | ||
56 | private static boolean rightNumberCount(String possible) { | |
57 | System.out.println(possible); | |
58 | int nul = 0; | |
59 | int two = 0; | |
60 | int fou = 0; | |
61 | int fiv = 0; | |
62 | int sev = 0; | |
63 | int maxnul = 3; | |
64 | int maxtwo = 2; | |
65 | int maxfou = 3; | |
66 | int maxfiv = 2; | |
67 | int maxsev = 6; | |
68 | ||
69 | for (int i = 0; i < possible.length(); i++) { | |
70 | char c = possible.charAt(i); | |
71 | if (c == '0') | |
72 | nul++; | |
73 | else if (c == '2') | |
74 | two++; | |
75 | else if (c == '4') | |
76 | fou++; | |
77 | else if (c == '5') | |
78 | fiv++; | |
79 | else if (c == '7') | |
80 | sev++; | |
81 | } | |
82 | ||
83 | if (nul > maxnul) { | |
84 | System.out.println("0x" + nul); | |
85 | return false; | |
86 | } else if (two > maxtwo) { | |
87 | System.out.println("2x" + two); | |
88 | return false; | |
89 | } else if (fou > maxfou) { | |
90 | System.out.println("4x" + fou); | |
91 | return false; | |
92 | } else if (fiv > maxfiv) { | |
93 | System.out.println("5x" + fiv); | |
94 | return false; | |
95 | } else if (sev > maxsev) { | |
96 | System.out.println("7x" + sev); | |
97 | return false; | |
98 | } | |
99 | ||
100 | System.out.println("!Code found!"); | |
101 | return true; | |
102 | } | |
103 | ||
104 | private static Map<Character, Integer> allowedMap() { | |
105 | Map<Character, Integer> map = new HashMap<Character, Integer>(); | |
106 | ||
107 | for (int i = 0; i < ALLOWED_CHARACTERS.length(); i++) { | |
108 | char c = ALLOWED_CHARACTERS.charAt(i); | |
109 | Integer count = map.get(c); | |
110 | if (count == null) | |
111 | count = 1; | |
112 | ||
113 | map.put(c, count + 1); | |
114 | } | |
115 | ||
116 | return map; | |
117 | } | |
118 | ||
119 | private static boolean containsNotAllowedNumbers(long fib) { | |
120 | String str = String.valueOf(fib); | |
121 | ||
122 | return str.contains("1") || str.contains("3") || str.contains("6") | |
123 | || str.contains("8") || str.contains("9"); | |
124 | } | |
125 | } |