【每日一题1222】下一个素数

:woman_mage: 给定一个整数n(n>=0),请编写一个函数,找出这个数字之后的最近一个素数。

素数 的定义:只有两个正因数(1和自身)的自然数即为素数 。

示例:
输入:5,返回:7。
输入:12,返回:13。

题目难度:简单
题目来源:CodeWars:Next Prime

def solution(n: int) -> int:
    # your code

assert solution(0) == 2
assert solution(2) == 3
assert solution(5) == 7
assert solution(181) == 191
def solution(n: int) -> int:
    while True:
        n = n + 1
        for i in range(1, n):
            if n % i == 0 and i != 1:
                break
            if i == n - 1:
                return n


assert solution(0) == 2
assert solution(2) == 3
assert solution(5) == 7
assert solution(181) == 191

1 个赞
def solution(n: int) -> int:
    target = n+1
    while True:
        if 0 < target < 2:
            return 2
        for i in range(2, target):
            if target % i == 0:
                target += 1
                break
        else:
            return target

assert solution(0) == 2
assert solution(2) == 3
assert solution(5) == 7
assert solution(181) == 191
public static int nextPrime(int n) {
        while(true){
            n++;
            for(int i=1;i<n;i++){
                if(n%i==0 && i!=1) {
                    break;
                } else if(i+1==n) {
                    return n;
                }
            }
        }
    }
def solution(n: int) -> int:
    while True:
        n += 1
        num = 0
        for i in range(1, n + 1):
            if n % i == 0:
                num += 1
        if num == 2:
            break
    return n


assert solution(0) == 2
assert solution(2) == 3
assert solution(5) == 7
assert solution(181) == 191
def solution(n:int) -> int:
  for x in range(n+1,9999999):
    if x > 2:
      for i in range(2,x):
        if x%i== 0:
          break 
      else:
        return x 
    else:
      return 2



assert solution(0)==2
assert solution(2)==3
assert solution(5)==7
assert solution(181)==191










def solution(n: int) -> int:
    if n == 0 or n == 1:
        return 2
    else:
        x = n + 1
        while True:
            for i in range(2, x):
                if x % i == 0:
                    break
            else:
                return x
            x += 1


assert solution(0) == 2
assert solution(2) == 3
assert solution(5) == 7
assert solution(181) == 191

import math


def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True


def solution(n: int) -> int:
    while True:
        n += 1
        if is_prime(n):
            return n

assert solution(0) == 2
assert solution(2) == 3
assert solution(5) == 7
assert solution(181) == 191
def solution6(n):
    x=0
    if n>1:
        while x<2:
            m=n+1
            for i in range(1,m):
                if m%i==0:
                    x+=1
            if x==1:
               return m
            else:
                x=0
                n+=1
    elif n==0:
        return 2
    elif n==1:
        return "1不是素数"
    else:
        return "请输入自然数"

assert solution6(0) == 2
assert solution6(2) == 3
assert solution6(5) == 7
assert solution6(181) == 191
def solution(n: int) -> int:
    while True:
        s_list = []
        n += 1
        for i in range(1, n+1):
            if n % i == 0:
                s_list.append(i)
        if s_list == [1, n]:
            break
    return n


assert solution(0) == 2
assert solution(2) == 3
assert solution(5) == 7
assert solution(181) == 191
def solution(n: int) -> int:
    while True:
        n = n + 1
        for i in range(1, n):
            if n % i == 0 and i != 1:
                break
            if i == n - 1:
                return n

assert solution(0) == 2
assert solution(2) == 3
assert solution(5) == 7
assert solution(181) == 191
def solution(n: int) -> int:
    if n < 2:
        return 2
    n = n + 1
    while 1:
        i = 2
        k = 0
        # while 2 <= i <= n - 1:
        for i in range(2, n - 1):
            if n % i == 0:
                n = n + 1
                k = k + 1
                break
            else:
                i = i + 1
        if k == 0:
            return n

assert solution(0) == 2
assert solution(2) == 3
assert solution(5) == 7
assert solution(181) == 191

练习记录一下,可以2头寻找

def solution(n: int) -> int:
    if (n<2):
        return 2
    max_left, min_right = 0,0
    for i in reversed(range(2, n)):
        for j in range(2, i):
            if (i%j==0):
                break
        else:
            max_left = i
            break
    i = n + 1
    while(True):
        for j in range(2, i):
            if (i%j ==0):
                break
        else:
            min_right = i
            break
        i += 1
    if(max_left !=0 and min_right!=0):
        if(min_right - n > n - max_left):
            return max_left
        elif(min_right - n < n - max_left):
            return min_right
        else:
            return min_right
    elif(max_left ==0):
        return min_right
    else:
        return max_left