返回介绍

solution / 0800-0899 / 0866.Prime Palindrome / README_EN

发布于 2024-06-17 01:03:33 字数 4007 浏览 0 评论 0 收藏 0

866. Prime Palindrome

中文文档

Description

Given an integer n, return _the smallest prime palindrome greater than or equal to _n.

An integer is prime if it has exactly two divisors: 1 and itself. Note that 1 is not a prime number.

  • For example, 2, 3, 5, 7, 11, and 13 are all primes.

An integer is a palindrome if it reads the same from left to right as it does from right to left.

  • For example, 101 and 12321 are palindromes.

The test cases are generated so that the answer always exists and is in the range [2, 2 * 108].

 

Example 1:

Input: n = 6
Output: 7

Example 2:

Input: n = 8
Output: 11

Example 3:

Input: n = 13
Output: 101

 

Constraints:

  • 1 <= n <= 108

Solutions

Solution 1

class Solution:
  def primePalindrome(self, n: int) -> int:
    def is_prime(x):
      if x < 2:
        return False
      v = 2
      while v * v <= x:
        if x % v == 0:
          return False
        v += 1
      return True

    def reverse(x):
      res = 0
      while x:
        res = res * 10 + x % 10
        x //= 10
      return res

    while 1:
      if reverse(n) == n and is_prime(n):
        return n
      if 10**7 < n < 10**8:
        n = 10**8
      n += 1
class Solution {
  public int primePalindrome(int n) {
    while (true) {
      if (reverse(n) == n && isPrime(n)) {
        return n;
      }
      if (n > 10000000 && n < 100000000) {
        n = 100000000;
      }
      ++n;
    }
  }

  private boolean isPrime(int x) {
    if (x < 2) {
      return false;
    }
    for (int v = 2; v * v <= x; ++v) {
      if (x % v == 0) {
        return false;
      }
    }
    return true;
  }

  private int reverse(int x) {
    int res = 0;
    while (x != 0) {
      res = res * 10 + x % 10;
      x /= 10;
    }
    return res;
  }
}
class Solution {
public:
  int primePalindrome(int n) {
    while (1) {
      if (reverse(n) == n && isPrime(n)) return n;
      if (n > 10000000 && n < 100000000) n = 100000000;
      ++n;
    }
  }

  bool isPrime(int x) {
    if (x < 2) return false;
    for (int v = 2; v * v <= x; ++v)
      if (x % v == 0)
        return false;
    return true;
  }

  int reverse(int x) {
    int res = 0;
    while (x) {
      res = res * 10 + x % 10;
      x /= 10;
    }
    return res;
  }
};
func primePalindrome(n int) int {
  isPrime := func(x int) bool {
    if x < 2 {
      return false
    }
    for v := 2; v*v <= x; v++ {
      if x%v == 0 {
        return false
      }
    }
    return true
  }

  reverse := func(x int) int {
    res := 0
    for x != 0 {
      res = res*10 + x%10
      x /= 10
    }
    return res
  }
  for {
    if reverse(n) == n && isPrime(n) {
      return n
    }
    if n > 10000000 && n < 100000000 {
      n = 100000000
    }
    n++
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文