-
Notifications
You must be signed in to change notification settings - Fork 0
/
866.prime-palindrome.go
149 lines (134 loc) · 2.41 KB
/
866.prime-palindrome.go
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
/*
* @lc app=leetcode id=866 lang=golang
* https://leetcode.com/problems/prime-palindrome/description/
* Medium (21.77%)
*/
func primePalindrome(n int) int {
switch {
case n <= 2:
return 2
case n <= 3:
return 3
case n <= 5:
return 5
case n <= 7:
return 7
case n <= 11:
return 11
case n <= 101:
return 101
}
ds := nextPotentialPalindromeNum(digits(n))
n = fromDigits(ds)
for !isPrime(n) {
ds = nextPotentialPalindromeNum(ds)
n = fromDigits(ds)
}
return n
}
func fromDigits(ds []int) int {
n := 0
for _, d := range ds {
n = 10*n + d
}
return n
}
func digits(n int) []int {
if n == 0 {
return []int{0}
}
var ds []int
for n > 0 {
ds = append(ds, n%10)
n /= 10
}
for i := 0; i < len(ds)/2; i++ {
ds[i], ds[len(ds)-1-i] = ds[len(ds)-1-i], ds[i]
}
return ds
}
// find the next potential palindrome number that greater or equal to n.
// assuming len(ds) >= 3, because in our solution above handles special case of
// n<=101
func nextPotentialPalindromeNum(ds []int) []int {
n := len(ds)
if n == 0 {
return nil
}
notPrimeByDigitRules := func(ds []int) bool {
if ds[0] == 5 || ds[len(ds)-1]%2 == 0 {
return true
}
sum := 0
s11 := 0
s101 := 0
for i, x := range ds {
sum += x
if i%2 == 0 {
s11 += x
} else {
s11 -= x
}
if i%4 <= 1 {
s101 += x
} else {
s101 -= x
}
}
return sum%3 == 0 || s11%11 == 0 || s101%101 == 0
}
if ds[0]%2 == 0 {
ds[0]++
ds[n-1] = ds[0]
for i := 1; i < n-1; i++ {
ds[i] = 0
}
if notPrimeByDigitRules(ds) {
return nextPotentialPalindromeNum(ds)
}
return ds
}
if n%2 != 0 {
for i := n / 2; i >= 0; i-- {
if ds[i] < 9 {
ds[i]++
ds[n-1-i] = ds[i]
sumDigit := ds[i]
if n%2 == 0 {
sumDigit += ds[i]
}
for j := i + 1; j <= n/2; j++ {
ds[j], ds[n-1-j] = 0, 0
}
previousDigit := ds[i]
same := true
for i--; i >= 0; i-- {
ds[n-1-i] = ds[i]
sumDigit += 2 * ds[i]
if ds[i] != previousDigit {
same = false
}
}
if same || notPrimeByDigitRules(ds) {
return nextPotentialPalindromeNum(ds)
}
return ds
}
}
}
// palindrome with even number of digits is divisible to 11
if n%2 == 0 {
ds = append([]int{1}, ds...)
} else {
ds = append([]int{1, 0}, ds...)
}
ds[n] = 1
for i := 1; i < n; i++ {
ds[i] = 0
}
if notPrimeByDigitRules(ds) {
return nextPotentialPalindromeNum(ds)
}
return ds
}