866. Prime Palindrome
Find the smallest prime palindrome greater than or equal to
N
.Recall that a number is prime if it's only divisors are 1 and itself, and it is greater than 1.
For example, 2,3,5,7,11 and 13 are primes.
Recall that a number is a palindrome if it reads the same from left to right as it does from right to left.
For example, 12321 is a palindrome.
Example 1:
Input: 6
Output: 7Example 2:
Input: 8
Output: 11Example 3:
Input: 13
Output: 101
Note:
1 <= N <= 10^8
- The answer is guaranteed to exist and be less than
2 * 10^8
.
Approach #1: Math. [Java]
class Solution {
public int primePalindrome(int N) {
if (8 <= N && N <= 11) return 11;
for (int x = 1; x < 100000; ++x) {
String s = Integer.toString(x), r = new StringBuilder(s).reverse().toString().substring(1);
int y = Integer.parseInt(s + r);
if (y >= N && isPrime(y)) return y;
}
return -1;
} boolean isPrime(int x) {
if (x < 2 || x % 2 == 0) return x == 2;
for (int i = 3; i * i <= x; i += 2) {
if (x % i == 0) return false;
}
return true;
}
}
Analysis:
All palindrome with even digits is multiple of 11.
We can prove as follow:
11 % 11 == 0
1111 % 11 == 0
111111 % 11 == 0
11111111 % 11 == 0
So:
1001 % 11 = (1111 - 11 * 10) % 11 == 0
100001 % 11 = (111111 - 1111 * 10) % 11 == 0
10000001 % 11 = (11111111 - 111111 * 10) % 11 == 0
For any palindrome with even digits:
abcddcba % 11
= (a * 10000001 + b * 100001 * 10 + c * 1001 * 100 + d * 11 * 1000) % 11
= 0
All palindrome with even digits is multiple of 11.
So among them, 11 is the only one prime
if (8 <= N <= 11) return 11
For other cases, we consider only palindrome with odd digits.
Time Complexity:
O(10000) to check all numbers 1 - 10000.
isPrime function is O(sqrt(x)) in worst case.
But only sqrt(N) worst cases for 1 <= x <= N
In general it's O(logx)
Reference:
https://leetcode.com/problems/prime-palindrome/discuss/146798/Search-Palindrome-with-Odd-Digits
866. Prime Palindrome的更多相关文章
- LeetCode 866. Prime Palindrome
866. Prime Palindrome(回文素数) 题目: 求出大于或等于 N 的最小回文素数. 回顾一下,如果一个数大于 1,且其因数只有 1 和它自身,那么这个数是素数. 例如,2,3,5,7 ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- Prime Palindrome Golf
Prime Palindrome Golf Do you know how to play Prime Palindrome Golf? You are given a number and your ...
- [Swift]LeetCode866. 回文素数 | Prime Palindrome
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- 洛谷 P1217 [USACO1.5]回文质数 Prime Palindrome
嗯... 这道题对于蒟蒻的我来说实在是TQL... 先看一下题:(题目链接:https://www.luogu.org/problemnew/show/P1217) 然后说一下我的做题过程吧: 一看到 ...
- 洛谷P1217回文质数-Prime Palindrome回溯
P1217 [USACO1.5]回文质数 Prime Palindromes 题意:给定一个区间,输出其中的回文质数: 学习了洛谷大佬的回溯写法,感觉自己写回溯的能力不是很强: #include &l ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- USACO 1.5 Prime Palindromes
Prime Palindromes The number 151 is a prime palindrome because it is both a prime number and a palin ...
- 4190. Prime Palindromes 一亿以内的质数回文数
Description The number 151 is a prime palindrome because it is both a prime number and a palindrome ...
随机推荐
- 第44天学习打卡(JUC 线程和进程 并发和并行 Lock锁 生产者和消费者问题 如何判断锁(8锁问题) 集合类不安全)
什么是JUC 1.java.util工具包 包 分类 业务:普通的线程代码 Thread Runnable 没有返回值.效率相比Callable相对较低 2.线程和进程 进程:一个程序.QQ.exe, ...
- Git 命令将电脑上的文件上传到 Github
1.在电脑上安装 Windows 版 Git下载地址:https://git-scm.com/downloads2.使用 Git GUI 生成 SSH Key 3.将 SSH Key 添加到 Gith ...
- 使用gitlab构建基于docker的持续集成(二)
使用gitlab构建基于docker的持续集成(二) gitlab docker aspnetcore Centos配置gitlab镜像并且启动 Centos配置防火墙 windows上访问gitla ...
- 后端程序员之路 44、Redis结合protobuf
protobuf序列化速度不错,在往Redis里存对象时,用protobuf序列化可以节省内存,省去写序列化反序列化代码的工作. google protocol buffer 与 redis 结合使用 ...
- R语言barplot ,掌握本篇的内容,基本的条形图都可以画了
本篇主要想复现文章中的一张图,原图来源(Antibiotic resistome and its association with bacterial communities during sewag ...
- tcpdump后台抓包并输出给wireshark
# 先进到/tmp 目录执行,方便Filezila 传输 # 开启抓包 nohup tcpdump -i eth0 -s0 -nnA 'port 22' -w dump22.pcap & [1 ...
- Shell脚本控制docker容器启动顺序
1.遇到的问题 在分布式项目部署的过程中,经常要求服务器重启之后,应用(包括数据库)能够自动恢复使用.虽然使用docker update --restart=always containerid能够让 ...
- python3 中post处理json 数据
使用详情如下 import json import requests headers = { "User-Agent": "Mozilla/5.0 (Windows NT ...
- VS添加dll引用
直接添加(CADImport.dll) 手动添加 (sgcadexp.dll) 直接放到项目bin的目录下
- Beego框架学习---layout的使用
Beego框架学习---layout的使用 在管理系统中,管理菜单的界面是固定的,会变化的只是中间的部分.我就在想,是不是跟angular的"组件模块的router-outlet一样&quo ...