File tree Expand file tree Collapse file tree 6 files changed +375
-2
lines changed
solution/0800-0899/0866.Prime Palindrome Expand file tree Collapse file tree 6 files changed +375
-2
lines changed Original file line number Diff line number Diff line change 59
59
<!-- 这里可写当前语言的特殊实现逻辑 -->
60
60
61
61
``` python
62
-
62
+ class Solution :
63
+ def primePalindrome (self , n : int ) -> int :
64
+ def is_prime (x ):
65
+ if x < 2 :
66
+ return False
67
+ v = 2
68
+ while v * v <= x:
69
+ if x % v == 0 :
70
+ return False
71
+ v += 1
72
+ return True
73
+
74
+ def reverse (x ):
75
+ res = 0
76
+ while x:
77
+ res = res * 10 + x % 10
78
+ x //= 10
79
+ return res
80
+
81
+ while 1 :
82
+ if reverse(n) == n and is_prime(n):
83
+ return n
84
+ if 10 ** 7 < n < 10 ** 8 :
85
+ n = 10 ** 8
86
+ n += 1
63
87
```
64
88
65
89
### ** Java**
66
90
67
91
<!-- 这里可写当前语言的特殊实现逻辑 -->
68
92
69
93
``` java
94
+ class Solution {
95
+ public int primePalindrome (int n ) {
96
+ while (true ) {
97
+ if (reverse(n) == n && isPrime(n)) {
98
+ return n;
99
+ }
100
+ if (n > 10000000 && n < 100000000 ) {
101
+ n = 100000000 ;
102
+ }
103
+ ++ n;
104
+ }
105
+ }
106
+
107
+ private boolean isPrime (int x ) {
108
+ if (x < 2 ) {
109
+ return false ;
110
+ }
111
+ for (int v = 2 ; v * v <= x; ++ v) {
112
+ if (x % v == 0 ) {
113
+ return false ;
114
+ }
115
+ }
116
+ return true ;
117
+ }
118
+
119
+ private int reverse (int x ) {
120
+ int res = 0 ;
121
+ while (x != 0 ) {
122
+ res = res * 10 + x % 10 ;
123
+ x /= 10 ;
124
+ }
125
+ return res;
126
+ }
127
+ }
128
+ ```
129
+
130
+ ### ** C++**
131
+
132
+ ``` cpp
133
+ class Solution {
134
+ public:
135
+ int primePalindrome(int n) {
136
+ while (1)
137
+ {
138
+ if (reverse(n) == n && isPrime(n)) return n;
139
+ if (n > 10000000 && n < 100000000) n = 100000000;
140
+ ++n;
141
+ }
142
+ }
143
+
144
+ bool isPrime(int x) {
145
+ if (x < 2) return false;
146
+ for (int v = 2; v * v <= x; ++v)
147
+ if (x % v == 0)
148
+ return false;
149
+ return true;
150
+ }
151
+
152
+ int reverse (int x) {
153
+ int res = 0;
154
+ while (x)
155
+ {
156
+ res = res * 10 + x % 10;
157
+ x /= 10;
158
+ }
159
+ return res;
160
+ }
161
+ };
162
+ ```
70
163
164
+ ### **Go**
165
+
166
+ ```go
167
+ func primePalindrome(n int) int {
168
+ isPrime := func(x int) bool {
169
+ if x < 2 {
170
+ return false
171
+ }
172
+ for v := 2; v*v <= x; v++ {
173
+ if x%v == 0 {
174
+ return false
175
+ }
176
+ }
177
+ return true
178
+ }
179
+
180
+ reverse := func(x int) int {
181
+ res := 0
182
+ for x != 0 {
183
+ res = res*10 + x%10
184
+ x /= 10
185
+ }
186
+ return res
187
+ }
188
+ for {
189
+ if reverse(n) == n && isPrime(n) {
190
+ return n
191
+ }
192
+ if n > 10000000 && n < 100000000 {
193
+ n = 100000000
194
+ }
195
+ n++
196
+ }
197
+ }
71
198
```
72
199
73
200
### ** ...**
Original file line number Diff line number Diff line change 45
45
### ** Python3**
46
46
47
47
``` python
48
-
48
+ class Solution :
49
+ def primePalindrome (self , n : int ) -> int :
50
+ def is_prime (x ):
51
+ if x < 2 :
52
+ return False
53
+ v = 2
54
+ while v * v <= x:
55
+ if x % v == 0 :
56
+ return False
57
+ v += 1
58
+ return True
59
+
60
+ def reverse (x ):
61
+ res = 0
62
+ while x:
63
+ res = res * 10 + x % 10
64
+ x //= 10
65
+ return res
66
+
67
+ while 1 :
68
+ if reverse(n) == n and is_prime(n):
69
+ return n
70
+ if 10 ** 7 < n < 10 ** 8 :
71
+ n = 10 ** 8
72
+ n += 1
49
73
```
50
74
51
75
### ** Java**
52
76
53
77
``` java
78
+ class Solution {
79
+ public int primePalindrome (int n ) {
80
+ while (true ) {
81
+ if (reverse(n) == n && isPrime(n)) {
82
+ return n;
83
+ }
84
+ if (n > 10000000 && n < 100000000 ) {
85
+ n = 100000000 ;
86
+ }
87
+ ++ n;
88
+ }
89
+ }
90
+
91
+ private boolean isPrime (int x ) {
92
+ if (x < 2 ) {
93
+ return false ;
94
+ }
95
+ for (int v = 2 ; v * v <= x; ++ v) {
96
+ if (x % v == 0 ) {
97
+ return false ;
98
+ }
99
+ }
100
+ return true ;
101
+ }
102
+
103
+ private int reverse (int x ) {
104
+ int res = 0 ;
105
+ while (x != 0 ) {
106
+ res = res * 10 + x % 10 ;
107
+ x /= 10 ;
108
+ }
109
+ return res;
110
+ }
111
+ }
112
+ ```
113
+
114
+ ### ** C++**
115
+
116
+ ``` cpp
117
+ class Solution {
118
+ public:
119
+ int primePalindrome(int n) {
120
+ while (1)
121
+ {
122
+ if (reverse(n) == n && isPrime(n)) return n;
123
+ if (n > 10000000 && n < 100000000) n = 100000000;
124
+ ++n;
125
+ }
126
+ }
127
+
128
+ bool isPrime(int x) {
129
+ if (x < 2) return false;
130
+ for (int v = 2; v * v <= x; ++v)
131
+ if (x % v == 0)
132
+ return false;
133
+ return true;
134
+ }
135
+
136
+ int reverse (int x) {
137
+ int res = 0;
138
+ while (x)
139
+ {
140
+ res = res * 10 + x % 10;
141
+ x /= 10;
142
+ }
143
+ return res;
144
+ }
145
+ };
146
+ ```
54
147
148
+ ### **Go**
149
+
150
+ ```go
151
+ func primePalindrome(n int) int {
152
+ isPrime := func(x int) bool {
153
+ if x < 2 {
154
+ return false
155
+ }
156
+ for v := 2; v*v <= x; v++ {
157
+ if x%v == 0 {
158
+ return false
159
+ }
160
+ }
161
+ return true
162
+ }
163
+
164
+ reverse := func(x int) int {
165
+ res := 0
166
+ for x != 0 {
167
+ res = res*10 + x%10
168
+ x /= 10
169
+ }
170
+ return res
171
+ }
172
+ for {
173
+ if reverse(n) == n && isPrime(n) {
174
+ return n
175
+ }
176
+ if n > 10000000 && n < 100000000 {
177
+ n = 100000000
178
+ }
179
+ n++
180
+ }
181
+ }
55
182
```
56
183
57
184
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int primePalindrome (int n) {
4
+ while (1 )
5
+ {
6
+ if (reverse (n) == n && isPrime (n)) return n;
7
+ if (n > 10000000 && n < 100000000 ) n = 100000000 ;
8
+ ++n;
9
+ }
10
+ }
11
+
12
+ bool isPrime (int x) {
13
+ if (x < 2 ) return false ;
14
+ for (int v = 2 ; v * v <= x; ++v)
15
+ if (x % v == 0 )
16
+ return false ;
17
+ return true ;
18
+ }
19
+
20
+ int reverse (int x) {
21
+ int res = 0 ;
22
+ while (x)
23
+ {
24
+ res = res * 10 + x % 10 ;
25
+ x /= 10 ;
26
+ }
27
+ return res;
28
+ }
29
+ };
Original file line number Diff line number Diff line change
1
+ func primePalindrome (n int ) int {
2
+ isPrime := func (x int ) bool {
3
+ if x < 2 {
4
+ return false
5
+ }
6
+ for v := 2 ; v * v <= x ; v ++ {
7
+ if x % v == 0 {
8
+ return false
9
+ }
10
+ }
11
+ return true
12
+ }
13
+
14
+ reverse := func (x int ) int {
15
+ res := 0
16
+ for x != 0 {
17
+ res = res * 10 + x % 10
18
+ x /= 10
19
+ }
20
+ return res
21
+ }
22
+ for {
23
+ if reverse (n ) == n && isPrime (n ) {
24
+ return n
25
+ }
26
+ if n > 10000000 && n < 100000000 {
27
+ n = 100000000
28
+ }
29
+ n ++
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int primePalindrome (int n ) {
3
+ while (true ) {
4
+ if (reverse (n ) == n && isPrime (n )) {
5
+ return n ;
6
+ }
7
+ if (n > 10000000 && n < 100000000 ) {
8
+ n = 100000000 ;
9
+ }
10
+ ++n ;
11
+ }
12
+ }
13
+
14
+ private boolean isPrime (int x ) {
15
+ if (x < 2 ) {
16
+ return false ;
17
+ }
18
+ for (int v = 2 ; v * v <= x ; ++v ) {
19
+ if (x % v == 0 ) {
20
+ return false ;
21
+ }
22
+ }
23
+ return true ;
24
+ }
25
+
26
+ private int reverse (int x ) {
27
+ int res = 0 ;
28
+ while (x != 0 ) {
29
+ res = res * 10 + x % 10 ;
30
+ x /= 10 ;
31
+ }
32
+ return res ;
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments