Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0866
Browse files Browse the repository at this point in the history
No.0866.Prime Palindrome
  • Loading branch information
yanglbme committed Jul 9, 2022
1 parent e896f7e commit 2809659
Show file tree
Hide file tree
Showing 6 changed files with 375 additions and 2 deletions.
129 changes: 128 additions & 1 deletion solution/0800-0899/0866.Prime Palindrome/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,142 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def primePalindrome(self, n: int) -> int:
def is_prime(x):
if x < 2:
return False
v = 2
while v * v <= x:
if x % v == 0:
return False
v += 1
return True

def reverse(x):
res = 0
while x:
res = res * 10 + x % 10
x //= 10
return res

while 1:
if reverse(n) == n and is_prime(n):
return n
if 10**7 < n < 10**8:
n = 10**8
n += 1
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public int primePalindrome(int n) {
while (true) {
if (reverse(n) == n && isPrime(n)) {
return n;
}
if (n > 10000000 && n < 100000000) {
n = 100000000;
}
++n;
}
}

private boolean isPrime(int x) {
if (x < 2) {
return false;
}
for (int v = 2; v * v <= x; ++v) {
if (x % v == 0) {
return false;
}
}
return true;
}

private int reverse(int x) {
int res = 0;
while (x != 0) {
res = res * 10 + x % 10;
x /= 10;
}
return res;
}
}
```

### **C++**

```cpp
class Solution {
public:
int primePalindrome(int n) {
while (1)
{
if (reverse(n) == n && isPrime(n)) return n;
if (n > 10000000 && n < 100000000) n = 100000000;
++n;
}
}

bool isPrime(int x) {
if (x < 2) return false;
for (int v = 2; v * v <= x; ++v)
if (x % v == 0)
return false;
return true;
}

int reverse(int x) {
int res = 0;
while (x)
{
res = res * 10 + x % 10;
x /= 10;
}
return res;
}
};
```
### **Go**
```go
func primePalindrome(n int) int {
isPrime := func(x int) bool {
if x < 2 {
return false
}
for v := 2; v*v <= x; v++ {
if x%v == 0 {
return false
}
}
return true
}
reverse := func(x int) int {
res := 0
for x != 0 {
res = res*10 + x%10
x /= 10
}
return res
}
for {
if reverse(n) == n && isPrime(n) {
return n
}
if n > 10000000 && n < 100000000 {
n = 100000000
}
n++
}
}
```

### **...**
Expand Down
129 changes: 128 additions & 1 deletion solution/0800-0899/0866.Prime Palindrome/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,140 @@
### **Python3**

```python

class Solution:
def primePalindrome(self, n: int) -> int:
def is_prime(x):
if x < 2:
return False
v = 2
while v * v <= x:
if x % v == 0:
return False
v += 1
return True

def reverse(x):
res = 0
while x:
res = res * 10 + x % 10
x //= 10
return res

while 1:
if reverse(n) == n and is_prime(n):
return n
if 10**7 < n < 10**8:
n = 10**8
n += 1
```

### **Java**

```java
class Solution {
public int primePalindrome(int n) {
while (true) {
if (reverse(n) == n && isPrime(n)) {
return n;
}
if (n > 10000000 && n < 100000000) {
n = 100000000;
}
++n;
}
}

private boolean isPrime(int x) {
if (x < 2) {
return false;
}
for (int v = 2; v * v <= x; ++v) {
if (x % v == 0) {
return false;
}
}
return true;
}

private int reverse(int x) {
int res = 0;
while (x != 0) {
res = res * 10 + x % 10;
x /= 10;
}
return res;
}
}
```

### **C++**

```cpp
class Solution {
public:
int primePalindrome(int n) {
while (1)
{
if (reverse(n) == n && isPrime(n)) return n;
if (n > 10000000 && n < 100000000) n = 100000000;
++n;
}
}

bool isPrime(int x) {
if (x < 2) return false;
for (int v = 2; v * v <= x; ++v)
if (x % v == 0)
return false;
return true;
}

int reverse(int x) {
int res = 0;
while (x)
{
res = res * 10 + x % 10;
x /= 10;
}
return res;
}
};
```
### **Go**
```go
func primePalindrome(n int) int {
isPrime := func(x int) bool {
if x < 2 {
return false
}
for v := 2; v*v <= x; v++ {
if x%v == 0 {
return false
}
}
return true
}
reverse := func(x int) int {
res := 0
for x != 0 {
res = res*10 + x%10
x /= 10
}
return res
}
for {
if reverse(n) == n && isPrime(n) {
return n
}
if n > 10000000 && n < 100000000 {
n = 100000000
}
n++
}
}
```

### **...**
Expand Down
29 changes: 29 additions & 0 deletions solution/0800-0899/0866.Prime Palindrome/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
int primePalindrome(int n) {
while (1)
{
if (reverse(n) == n && isPrime(n)) return n;
if (n > 10000000 && n < 100000000) n = 100000000;
++n;
}
}

bool isPrime(int x) {
if (x < 2) return false;
for (int v = 2; v * v <= x; ++v)
if (x % v == 0)
return false;
return true;
}

int reverse(int x) {
int res = 0;
while (x)
{
res = res * 10 + x % 10;
x /= 10;
}
return res;
}
};
31 changes: 31 additions & 0 deletions solution/0800-0899/0866.Prime Palindrome/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
func primePalindrome(n int) int {
isPrime := func(x int) bool {
if x < 2 {
return false
}
for v := 2; v*v <= x; v++ {
if x%v == 0 {
return false
}
}
return true
}

reverse := func(x int) int {
res := 0
for x != 0 {
res = res*10 + x%10
x /= 10
}
return res
}
for {
if reverse(n) == n && isPrime(n) {
return n
}
if n > 10000000 && n < 100000000 {
n = 100000000
}
n++
}
}
34 changes: 34 additions & 0 deletions solution/0800-0899/0866.Prime Palindrome/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public int primePalindrome(int n) {
while (true) {
if (reverse(n) == n && isPrime(n)) {
return n;
}
if (n > 10000000 && n < 100000000) {
n = 100000000;
}
++n;
}
}

private boolean isPrime(int x) {
if (x < 2) {
return false;
}
for (int v = 2; v * v <= x; ++v) {
if (x % v == 0) {
return false;
}
}
return true;
}

private int reverse(int x) {
int res = 0;
while (x != 0) {
res = res * 10 + x % 10;
x /= 10;
}
return res;
}
}
Loading

0 comments on commit 2809659

Please sign in to comment.