LeetCode 866. Prime Palindrome
866. Prime Palindrome(回文素数)
题目:
求出大于或等于 N
的最小回文素数。
回顾一下,如果一个数大于 1,且其因数只有 1 和它自身,那么这个数是素数。
例如,2,3,5,7,11 以及 13 是素数。
回顾一下,如果一个数从左往右读与从右往左读是一样的,那么这个数是回文数。
例如,12321 是回文数。
思路:
思路还是挺清晰的,从给入数字向上检索,如果既是回文数,又是素数,就直接输出,如果不满足条件,那么就增加数字,继续判断。
这里有一个小问题,就是所有偶数位的回文数,都可以被11整除,至于证明。。。。。咱也不知道,咱也不敢问,所有如果发现这个数是偶数位,那么直接进一位,首数字和尾数字全为1,继续判断。
代码:
public static int primePalindrome(int N)
{
if (N <= 2)
return 2;
else if(N <= 3)
return 3;
else if(N <= 5)
return 5;
else if(N <= 7)
return 7;
else if(N <= 11)
return 11; for (int i = N; ; )
{
if(isHui(i) && isPrime(i))
return i; if((i + "").length() % 2 == 0)
i = (int)(Math.pow(10, (i + "").length()) + 1);
else
i++; }
} public static boolean isPrime(int i)
{
for (int j = 2; j <= Math.sqrt(i); j++)
if (i % j == 0)
return false;
return true;
} public static boolean isHui(int s)
{
String str = s+"";
int len = str.length();
for (int j = 0; j < len/2; j++)
if (str.charAt(j) != str.charAt(len-j-1))
return false;
return true;
}
LeetCode 866. Prime Palindrome的更多相关文章
- 866. Prime Palindrome
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- [Swift]LeetCode866. 回文素数 | 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 ...
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] 680. Valid Palindrome II 验证回文字符串 II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- leetcode bug & 9. Palindrome Number
leetcode bug & 9. Palindrome Number bug shit bug "use strict"; /** * * @author xgqfrms ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- tomcat单机多实例
catalina.home指向公用信息的位置,就是bin和lib的父目录. catalina.base指向每个Tomcat目录私有信息的位置,就是conf.logs.temp.webapps和work ...
- kafka 基本原理简介
Kafka是啥?用Kafka官方的话来说就是: Kafka is used for building real-time data pipelines and streaming apps. It i ...
- Mac Mysql 5.6.4修改初始化密码
Mac Mysql 修改初始化密码 第一步: 点击系统偏好设置->最下边点MySQL,在弹出页面中,关闭服务 第二步:进入终端输入:cd /usr/local/mysql/bin/回车后 登 ...
- <JavaScript> call()、apply()、bind() 的用法
其实是一个很简单的东西,认真看十分钟就从一脸懵B 到完全 理解! 先看明白下面: 例 1 obj.objAge; obj.myFun() // 小张年龄 undefined 例 2 shows() ...
- HTTP的响应协议
响应行介绍,响应状态码 1XX: 客户端请求服务器,但是请求未完成,服务器什么事也没干 2XX: 表示响应成功,代表性的状态码就是200 3XX: 请求重定向,代表性的状态码302 4XX: 客户端发 ...
- Jenkins 自动化部署上线
转载于互联网 jenkins自动化部署项目,通过jenkins 部署来节省运维时间,不需要手动cp上线及版本发布 Jenkins 自动化部署上线 Jenkins Jenkins 自动化部署上线一.Je ...
- 【原创】smarty引擎下的导航按钮高亮实现
<?php$_nvaarr = array( array('name'=>'首页','url'=>'company.php?id='), array('name'=>'公司介绍 ...
- 阶段5 3.微服务项目【学成在线】_day04 页面静态化_09-freemarker基础-内建函数
可以理解为freemaker提供的一些函数和方法 controller里面记得把map的数据注释去掉,.放开代码 工程需要重启下 <br/> 学生的个数: ${stus?size} < ...
- Example config file /etc/vsftpd.conf
# Example config file /etc/vsftpd.conf # # The default compiled in settings are fairly paranoid. Thi ...
- MyBatis接口式编程
MyBatis使用接口连接数据库 之前学习了如何传统的使用MyBatis连接数据库,今天学习如何使用更方便快捷的方式练级数据库.使用接口连接数据库 https://www.cnblogs.com/li ...