需要帮助理解 is_prime 函数的第 9 行;该功能有更简单的方法吗?

Need help understanding line # 9 of is_prime function; is there a simpler way for the function?

第 9 行(如果 n + 1 == x)与检查数字是否为质数有什么关系?

有没有更简单的方法来构建这个函数?

    def is_prime(x):
        if x == 2:
            return True
        elif x > 2:
            for n in range(2, x):
                if x % n == 0:
                    return False
                else:
                    if n + 1 == x:
                        return True
        else:
            return False

素数是只有 1 和 'self' 作为约数的整数。这是一个类似的解决方案,可能更容易理解。我们使用 pandas DataFrame 及其关联的 'apply' 函数。取消 'print df' 行并根据需要修改输出。玩得开心

"""
Created on Fri Nov 18 13:32:08 2016
@author: Soya
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas import DataFrame, Series


def isprime(x):
    vals = range(2,x/2)
    df = DataFrame([vals]).T
    df['1'] = df.apply(lambda y: x%y)
    print df
    print ''
    if df['1'].prod() != 0:
        print 'PRIME'

isprime(17)
   0  1
0  2  1
1  3  2
2  4  1
3  5  2
4  6  5
5  7  3

PRIME