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的更多相关文章

  1. 866. Prime Palindrome

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  2. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  3. [Swift]LeetCode866. 回文素数 | Prime Palindrome

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  4. Prime Palindrome Golf

    Prime Palindrome Golf Do you know how to play Prime Palindrome Golf? You are given a number and your ...

  5. [LeetCode] 409. Longest Palindrome 最长回文

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  6. [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 ...

  7. leetcode bug & 9. Palindrome Number

    leetcode bug & 9. Palindrome Number bug shit bug "use strict"; /** * * @author xgqfrms ...

  8. 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. ...

  9. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

随机推荐

  1. PyTricks-使用namedtuple以及dataclass的方式定义类

    from collections import namedtuple from dataclasses import dataclass # 以前简单的类可以使用namedtuple实现. Car = ...

  2. Java中路径相关的获取方式

    [参考文章]:Java文件路径(getResource) [参考文章]:关于java:如何获取正在运行的JAR文件的路径? [参考文章]:关于Class.getResource和ClassLoader ...

  3. python中heapq对dict进行排序

    问题: 想从以下形式的dict中取value最大的2个key-value的key dict_num_num = {0: 0.07374631268436578, 1: 0.16307692307692 ...

  4. ubuntu18.04 systemctl

    systemd 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本.systemd 的特性有:支持并行化任务:同一时候採用 socket 式与 D-Bus 总线式激活服务 ...

  5. 移动端—— 兼容PC端,移动端的点击事件

    移动设备上不支持鼠标事件,好在webkit内核的移动浏览器支持 touch 事件,所以触摸事件是移动应用中所必须的.touchstart.touchmove.touchend事件可以类比于moused ...

  6. mysqldump定时任务生成备份文件内容为空解决方法

    1问题:写好了一个mysqldump备份脚本(如图)直接执行可以正常生成备份文件,但在用crontab运行时却生成内容为空 2原因分析:由于mysqldump存在于全局环境变量mysql的bin下面, ...

  7. DataFactory生产身份证号码==

    生产身份证号:定义数据类型为CHAR()类型的才能进行数据的组合

  8. c# 线程异步处理

    public class AsyncHelper { private static readonly TaskFactory _myTaskFactory = new TaskFactory(Canc ...

  9. 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_08-新增页面-前端-Api调用

    表单数据提交到后台 export const page_add = paramas => { return http.requestPost(apiUrl+'/cms/page/add',par ...

  10. 【python画图】

    1.热力图 import numpy as np import numpy.random import matplotlib.pyplot as plt # Generate some test da ...