博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python判断素数程序_使用面向对象方法检查素数的Python程序
阅读量:2528 次
发布时间:2019-05-11

本文共 2175 字,大约阅读时间需要 7 分钟。

python判断素数程序

This program will check whether a given number is Prime or Not, in this program we will divide the number from 2 to square root of that number, if the number is divided by any number in b/w then the number will not be a prime number.

该程序将检查给定数字是否为质 ,在此程序中,我们将数字从2除以该数的平方根,如果该数字除以b / w中的任何数字,则该数字将不是质数数。

We are implementing this program using the concept of classes and objects.

我们正在使用类和对象的概念来实现该程序。

Firstly we create the Class with Check name with 1 attributes ('number') and 2 methods, the methods are:

首先,我们使用Check名称创建具有1个属性( 'number' )和2个方法的Class,这些方法是:

  1. Constructor Method: This is created using __init__ inbuilt keyword. The constructor method is used to initialize the attributes of the class at the time of object creation.

    构造方法 :这是使用__init__内置关键字创建的。 构造函数方法用于在创建对象时初始化类的属性。

  2. Object Method: isPrime() is the object method, for creating object method we have to pass at least one parameter i.e. self keyword at the time of function creation.

    对象方法 : isPrime()是对象方法,要创建对象方法,我们必须在函数创建时传递至少一个参数,即self关键字。

Secondly, we have to create an object of this class using a class name with parenthesis then we have to call its method for our output.

其次,我们必须使用带有括号的类名来创建此类的对象,然后必须为其输出调用其方法。

Below is the implementation of the program,

下面是该程序的实现,

Python代码检查给定数字是否为质数 (Python code to check whether a given number is prime or not)

# Define a class for Checking prime numberclass Check :        # Constructor    def __init__(self,number) :        self.num = number           # define a method for checking number is prime or not     def isPrime(self) :                for i in range(2, int(num ** (1/2)) + 1) :                        # if any number is divisible by i             # then number is not prime            # so return False            if num % i == 0 :                return False                # if number is prime then return True        return True# Main code if __name__ == "__main__" :        # input number    num = 11        # make an object of Check class    check_prime = Check(num)        # method calling    print(check_prime.isPrime())        num = 14    check_prime = Check(num)    print(check_prime.isPrime())

Output

输出量

TrueFalse

翻译自:

python判断素数程序

转载地址:http://kjtzd.baihongyu.com/

你可能感兴趣的文章
Azure云服务托管恶意软件
查看>>
My安卓知识6--关于把项目从androidstudio工程转成eclipse工程并导成jar包
查看>>
旧的起点(开园说明)
查看>>
生产订单“生产线别”带入生产入库单
查看>>
crontab导致磁盘空间满问题的解决
查看>>
java基础 第十一章(多态、抽象类、接口、包装类、String)
查看>>
Hadoop 服务器配置的副本数量 管不了客户端
查看>>
欧建新之死
查看>>
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
mysql 日期时间运算函数(转)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>