forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0866
No.0866.Prime Palindrome
- Loading branch information
Showing
6 changed files
with
375 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.