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 ...
随机推荐
- rar密码破解工具汇总
rar密码破解工具汇总 前言 假如酷爱在网络上找各种资源的你,经历千辛万苦终于找到了一个rar打包的文件,兴奋地慌忙点击,可打开才发现是加密的,相信这样的场景很多人都遇到过,今天就针对压缩文件密码的破 ...
- Cloudam云端携手高校探索云计算在生命科学领域的应用
随着云计算服务和实践的成熟,越来越多的行业对于云计算的需求也日益增加.不同行业的需求与云计算融合,就需要更大的算力支撑.这也意味着,云计算的需求市场日渐扩大,Cloudam云端自主研发的云E算力平台应 ...
- tibco EMS 8.2.0安装
安装环境 序号 项目 值 1 OS版本 Red Hat Enterprise Linux Server release 7.1 (Maipo) 2 内核版本 3.10.0-229.el7.x86_64 ...
- Python工程师学习之旅
1.Python软件开发基础 1.Linux操作系统2.Docker基础3.Python基础语法4.Python字符串解析5.Python正则表达式6.Python文件操作7.Python 模块8.P ...
- springboot注解之@Import @Conditional @ImportResource @ConfigurationProperties @EnableConfigurationProperties
1.包结构 2.主程序类 1 @SpringBootApplication(scanBasePackages={"com.atguigu"}) 2 public class Mai ...
- python基础学习之元组和字典的功能方法
什么是元组?(tuple) emmmmmm,这个没必要深究吧,就是一排'元素',一行 格式: a = (1,2,3,4,5,6,7,8,9)用小括号表示的,极为元组. 其有序,且不可更改,可以对比st ...
- java常见面试题2:求出两个正整数的最大公约数
概念: 最大公约数:两个整数共有因子中最大的一个 方法一: 如果两个数相等,则最大公约数为它本身,两个数不等,则用两个数依次除 两个数中最小的一个到 1,直到找到同时能被两个数除尽的那个数 代码清单: ...
- weex参考文章
1官网:https://weex.apache.org/zh/guide/introduction.html 2.weexui https://alibaba.github.io/weex-ui/ ...
- Hznu_0j 1533 计算球体积(水)
题意:根据输入的半径值,计算球的体积: Input 输入数据有多组,每组占一行,每行包括一个实数,表示球的半径. Output 输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数. ...
- 攻防世界 reverse crazy
crazy 百越杯2018 查看main函数: int __cdecl main(int argc, const char **argv, const char **envp) { __int64 v ...