检查素数:任何数字是素数?

时间:2019-07-26 01:36:46

标签: python

您好,最亲爱的社区,

我被困在一个很简单的事情上,我想有时候你只是出于简单。无论如何,我写这个来检查素数:

num = int(input(""))

if num > 1:
    for i in range(2, num-1):
        if (num % i)  == 0:
            print(num, "is no Primenumber")
            break
        else:
            print(i, "PRIME!!!")

(原始代码无法打印任何内容,但是我遇到了麻烦,因此我将其重写以进行调试)

无论如何,它可以正确识别非质数,但是将其输出为质数,可以说7:

7
2 PRIME!!!
3 PRIME!!!
4 PRIME!!!
5 PRIME!!!

4 个答案:

答案 0 :(得分:1)

使用以下逻辑查找数字是否为质数。这适用于python,因为for循环还有一个else块,当执行过程中循环未中断时,该块将执行,这意味着数字为质数。

if num > 1: 

   # Iterate from 2 to n / 2  
   for i in range(2, num//2): 

       # If num is divisible by any number between  
       # 2 and n / 2, it is not prime  
       if (num % i) == 0: 
           print(num, "is not a prime number") 
           break
   else: 
       print(num, "is a prime number") 

else: 
   print(num, "is not a prime number") 

答案 1 :(得分:1)

如果您担心更大数量的性能,最好使用square root而不是number flooring division 2。任何大于平方根的因数也将具有小于1的因数。

这是一个常见的解决方案:

def check_for_prime(p_in):
    is_prime = True
    for n in range(2, int(p_in ** 0.5) + 1):
        if p_in % n == 0:
            is_prime = False
            break
    return is_prime

num = int(input(""))

if check_for_prime(num):
    print(f'{num} is prime')
else:
    print(f'{num} is not prime')

答案 2 :(得分:1)

不是最注重性能的,但是可以完成工作并且很简单。

def Markov_model(tf):
  m = ConcreteModel()
  m.time = ContinuousSet(bounds=(0,tf))

  #params
  m.markov = Param(m.time, initialize=0.0, mutable=True)
  m.c0 = Param(initialize=2.0)
  m.ip3 = Param(initialize=1.0)

  #ca concentration
  m.ca = Var(m.time, bounds=(0.01,5))

  def _open_probility_(m):
    return (pro_data[i] for i in range(160)) 
  m.open_probility = Set(initialize=_open_probility_, ordered=True)

  #variables
  m.c1 = Var(within=NonNegativeReals, bounds = (0.1, 1), initialize=0.185)
  m.v1 = Var(within=NonNegativeReals, bounds = (1, 10), initialize=6)
  m.v2 = Var(within=NonNegativeReals, bounds = (0.1, 1), initialize=0.11)
  m.v3 = Var(within=NonNegativeReals, bounds = (0.1, 1), initialize=0.9)
  m.k3 = Var(within=NonNegativeReals, bounds = (0.1, 1), initialize=0.1)
  m.d1 = Var(within=NonNegativeReals, bounds = (0.1, 1), initialize=0.13)
  m.d2 = Var(within=NonNegativeReals, bounds = (1, 10), initialize=1.049)
  m.d3 = Var(within=NonNegativeReals, bounds = (0.1, 1), initialize=0.9434)
  m.d5 = Var(within=NonNegativeReals, bounds = (0.01, 0.1), initialize=0.08234)
  m.a2 = Var(within=NonNegativeReals, bounds = (0.1, 1), initialize=0.2)

  #derivative
  m.dca_dt = DerivativeVar(m.ca, wrt=m.time)

  #var equation
  m.j_channel = Var(m.time)
  m.j_pump = Var(m.time)
  m.j_leak = Var(m.time)
  m.m_inf = Var(m.time)
  m.n_inf = Var(m.time)
  m.alpha_h = Var(m.time)
  m.beta_h = Var(m.time)
  m.ca_er = Var(m.time)

  m.ca[0].fix(0.6)
  m.open_probility[0].fix(0.5)

  def _m_inf(m, i):
    return m.m_inf[i] == m.ip3/(m.ip3 + m.d1)
  m.m_inf_con = Constraint(m.time, rule = _m_inf)

  def _n_inf(n, i):
    return m.n_inf[i] == m.ca[i]/(m.ca[i] + m.d5)
  m.n_inf_con = Constraint(m.time, rule = _n_inf)

  def _alpha(n, i):
    return m.alpha_h[i] == m.a2*m.d2*(m.ip3 + m.d1)/(m.ip3 + m.d3)
  m.alpha_con = Constraint(m.time, rule = _alpha)

  def _beta(n, i):
    return m.beta_h[i] == m.a2*m.ca[i]
  m.beta_con = Constraint(m.time, rule = _beta)

  def _ca_er(n,i):
    return m.ca_er[i] == (m.c0 - m.ca[i])/m.c1
  m.ca_er_con = Constraint(m.time, rule = _ca_er)

  def _j_leak(n,i):
    return m.j_leak[i] == m.c1*m.v2*(m.ca[i] - m.ca_er[i])
  m._j_leak_con = Constraint(m.time, rule = _j_leak)

  def _j_pump(n,i):
    return m.j_pump[i] == m.v3*m.ca[i]**2/(m.k3**2 + m.ca[i]**2)
  m._j_pump_con = Constraint(m.time, rule = _j_pump)


  def _j_channel(n, i):
    return m.j_channel[i] == m.c1*m.v1*(m.m_inf[i]**3)*(m.n_inf[i]**3)*(m.ca[i] - m.ca_er[i])*m.open_probility[i]
  m._j_channel_con = Constraint(m.time, rule = _j_channel)

  def _dca_dt(n,i):
    return m.dca_dt[i] == -m.j_leak[i] - m.j_pump[i] - m.j_channel[i]
  m._dca_dt_con = Constraint(m.time, rule=_dca_dt)

  return m

答案 3 :(得分:0)

没有第三方模块,您可以通过以下方式轻松执行它:

def is_prime(number):
    return all(number % n for n in range(2, number))

通过这种方式,您检查:

  • 指定数字以及2到该数字之间的任意数字之间的所有除数
  • 诀窍是考虑以下事实:如果任何number % n给出0,则意味着它是倍数,因此它不能是质数,您将立即得到结果