-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution866.cs
59 lines (51 loc) · 1.08 KB
/
Solution866.cs
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
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution866
{
/// <summary>
/// 866. Prime Palindrome - Medium
/// <a href="https://leetcode.com/problems/prime-palindrome">See the problem</a>
/// </summary>
public int PrimePalindrome(int n)
{
while (true)
{
if (n == Reverse(n) && IsPrime(n))
{
return n;
}
n++;
if (10000000 < n && n < 100000000)
{
n = 100000000;
}
}
}
private int Reverse(int n)
{
int result = 0;
while (n > 0)
{
result = result * 10 + n % 10;
n /= 10;
}
return result;
}
private bool IsPrime(int n)
{
if (n < 2)
{
return false;
}
int sqrt = (int)Math.Sqrt(n);
for (int i = 2; i <= sqrt; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
}