gyjgyj0514 2020-01-19 22:53 采纳率: 0%
浏览 257

如何用if在python里求一个数是否为质数

is_prime that consumes a Natural number(n) and returns True if n is prime (its only positive divisors are 1 and n), and False otherwise.

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-01-20 09:06
    关注
    # -*- coding: UTF-8 -*-
    
    def is_prime(n):
        if n < 2: return False
        if n == 2: return True
        for i in range(2, n / 2 + 1):
            if n % i == 0: return False
        return True
    
    for i in range(100):
        if (is_prime(i)): print(i)
    

    如果问题得到解决,请点下采纳,谢谢合作

    评论

报告相同问题?

悬赏问题

  • ¥15 overleaf中论文编辑,报错`pages' is a missing field, not a string, for entry 4
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么