-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem49.java
141 lines (120 loc) · 3.64 KB
/
Problem49.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.javamultiplex.projecteuler;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Problem49 {
public static void main(String[] args) {
int start = 1489, end = 9999, i1 = 0, i2 = 0, i3 = 0;
int constant = 3330;
BigInteger num1 = null, num2 = null, num3 = null;
List<Integer> permutations = new ArrayList<>();
String result = null;
for (i1 = start; i1 <= end; i1 = i1 + 2) {
// Converting int to String and storing in BigInteger.
num1 = new BigInteger(String.valueOf(i1));
// Checking prime numbers
if (num1.isProbablePrime(1)) {
i2 = i1 + constant;
i3 = i2 + constant;
// Converting int to String and storing in BigInteger.
num2 = new BigInteger(String.valueOf(i2));
num3 = new BigInteger(String.valueOf(i3));
// Checking prime numbers
if (num2.isProbablePrime(1) && num3.isProbablePrime(1)) {
permutations = getPermuations(i1);
if (permutations.contains(i2) && permutations.contains(i3)) {
result = String.valueOf(i1) + String.valueOf(i2)
+ String.valueOf(i3);
break;
}
}
}
}
System.out.println("12-digit number is : " + result);
}
private static List<Integer> getPermuations(int i1) {
List<Integer> result = new ArrayList<>();
int n = 24; // 4!=24
int digits[] = getArrayFromInteger(i1);
int length = 4; // 4 digit number
int k = 0, i = 0, temp = 0;
// Adding given number to result because it is also permutation.
result.add(i1);
for (int j = 2; j <= n; j++) {
// Step 1 : Find largest index k such that digits[k]<digits[k+1]
k = getK(digits, length);
// Step 2 : Find largest index i such that digits[k]<digits[i]
i = getI(digits, length, k);
// Step 3 : Swap digits[i] and digits[k]
digits = swapKandI(digits, k, i);
// Step 4 : Reverse all the values in array from k+1 to end.
digits = reverseFromKtoEnd(digits, k, length);
temp = getIntegerFromArray(digits);
result.add(temp);
}
return result;
}
private static int getIntegerFromArray(int[] digits) {
int length = digits.length;
String temp = "";
int result = 0;
for (int i = 0; i < length; i++) {
// Converting int to String.
temp += String.valueOf(digits[i]);
}
// Converting String to int.
result = Integer.valueOf(temp);
return result;
}
private static int[] getArrayFromInteger(int i1) {
// Converting int to String.
String str = String.valueOf(i1);
int length = str.length();
int digits[] = new int[length];
for (int i = 0; i < length; i++) {
// Converting char to int.
digits[i] = str.charAt(i) - 48;
}
return digits;
}
private static int[] reverseFromKtoEnd(int[] digits, int k, int length) {
/*
* For reversing array elements we are storing elements from k+1 to
* length-1 into temp array.
*/
int temp[] = Arrays.copyOfRange(digits, k + 1, length);
int l = temp.length;
for (int i = k + 1; i < length; i++) {
digits[i] = temp[l - 1];
l--;
}
return digits;
}
private static int[] swapKandI(int[] digits, int k, int i) {
int temp = digits[k];
digits[k] = digits[i];
digits[i] = temp;
return digits;
}
private static int getI(int[] digits, int length, int k) {
int j = 0;
for (int i = length - 1; i > k; i--) {
if (digits[i] > digits[k]) {
j = i;
break;
}
}
return j;
}
private static int getK(int[] digits, int length) {
int k = 0;
for (int i = length - 1; i > 0; i--) {
if (digits[i] > digits[i - 1]) {
k = i - 1;
break;
}
}
return k;
}
}