-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnagrammaticPrimes.java
124 lines (118 loc) · 3.11 KB
/
AnagrammaticPrimes.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
/** #number-theory */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
class AnagrammaticPrimes {
public static boolean isPrime(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
// @ https://oeis.org/A003459
public static boolean checkAnagramaticPrime(int n) {
boolean ret = true;
// n = 1111111...
if (n > 1000) {
while (n % 10 == 1) {
n /= 10;
}
if (n != 0) ret = false;
} else {
char[] digits = Integer.toString(n).toCharArray();
ArrayList<Integer> permutation = new ArrayList<>();
String numStr;
for (int i = 0; i < digits.length; i++) {
for (int j = 0; j < digits.length; j++) {
if (i != j) {
if (digits.length == 3) {
numStr =
String.valueOf(digits[i])
+ String.valueOf(digits[j])
+ String.valueOf(digits[3 - i - j]);
} else {
numStr = String.valueOf(digits[i]) + String.valueOf(digits[j]);
}
permutation.add(Integer.parseInt(numStr));
}
}
}
for (int x : permutation) {
if (!isPrime(x)) {
ret = false;
break;
}
}
}
return ret;
}
public static ArrayList<Integer> sieveOfEratosthenes(int limit) {
boolean[] mark = new boolean[limit + 1];
ArrayList<Integer> primes = new ArrayList<>();
Arrays.fill(mark, true);
mark[0] = mark[1] = false;
for (int i = 2; i * i <= limit; i++) {
if (mark[i]) {
for (int j = i * i; j <= limit; j += i) {
mark[j] = false;
}
}
}
for (int i = 2; i <= limit; i++) {
if (mark[i]) {
primes.add(i);
}
}
return primes;
}
public static void segmentedSieve(int left, int right, ArrayList<Integer> primes) {
boolean[] mark = new boolean[right - left + 1];
Arrays.fill(mark, true);
for (int i = 0; i < primes.size() && primes.get(i) <= Math.sqrt(right); i++) {
int base = (left / primes.get(i)) * primes.get(i);
if (base < left) {
base += primes.get(i);
}
for (int j = base; j <= right; j += primes.get(i)) {
if (j != primes.get(i)) {
mark[j - left] = false;
}
}
}
int result = 0;
for (int i = left + 1; i <= right; i++) {
if (mark[i - left]) {
// check anagramatic of i is prime or not.
if (checkAnagramaticPrime(i)) {
result = i;
break;
}
}
}
System.out.println(result);
}
public static int getLimits(int n) {
int limits = 1;
while (n > 0) {
limits *= 10;
n /= 10;
}
return limits;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
while (true) {
n = sc.nextInt();
if (n == 0) break;
int right = getLimits(n);
ArrayList<Integer> primes = sieveOfEratosthenes((int) Math.sqrt(right));
segmentedSieve(n, right, primes);
}
}
}