Judge whether it is a prime number [Python]

In this article A program that determines whether the input value (n) is a prime number or not. I will write.

What is a prime number?

A prime number is a natural number greater than 1 that has only one positive divisor and itself. See: prime numbers-Wikipedia

Execution environment

How to find

(1) If it is 1 or less or 2, describe whether it is a prime number. (2) If it is 3 or more, loop from 2 to n and check if there is a divisible number. If it is divisible, it is not a prime number. ③ Call the function and display the result.

** There was an error in the case of an integer greater than or equal to 3, so I corrected it **

(1) If it is 1 or less or 2, describe whether it is a prime number.

If False, it is not a prime number. If True, a prime number.

prime_num.py


def is_prime(n):
    #1 or less is not a prime number
    if n <= 1:
        return False
    #2 is a prime number
    if n == 2:
        return True

(2) If it is 3 or more, loop from 2 to n and check if there is a divisible number.

If it is divisible, it is not a prime number

prime_num.py(2)


def is_prime(n):
    #1 or less is not a prime number
    if n <= 1:
        return False
    #2 is a prime number
    if n == 2:
        return True
    #Even numbers other than 2 are not prime numbers
    if n % 2 == 0:
        return False
    #For odd numbers of 3 or more, prime numbers unless divisible by all odd numbers up to the square root
    return all(n % i != 0 for i in range(3, int(n**0.5) + 1, 2))

③ Call the function and display the result

If False, it is not a prime number. If True, it is a prime number.

prime_num.py(3)


def is_prime(n):
    #1 or less is not a prime number
    if n <= 1:
        return False
    #2 is a prime number
    if n == 2:
        return True
    #Even numbers other than 2 are not prime numbers
    if n % 2 == 0:
        return False
    #For odd numbers of 3 or more, prime numbers unless divisible by all odd numbers up to the square root
    return all(n % i != 0 for i in range(3, int(n**0.5) + 1, 2))

number = int(input("Natural number →"))

if is_prime(number):
    print(f"{number}Is a prime number.")
else:
    print(f"{number}Is not a prime number.")

Execution result

number = 1
→ 1 is not a prime number.

number = 5
→ 5 is a prime number.

The result is reflected.

Summary

This time, I wrote a program to judge whether it is a prime number in Python.

In this program, the larger n is, the longer the calculation becomes. I would like to find more efficient code.

reference

prime number --Wikipedia

Recommended Posts

Judge whether it is a prime number [Python]
A program that determines whether a number entered in Python is a prime number
Prime number 2 in Python
Check if the string is a number in python
I made a prime number generation program in Python
I made a prime number generation program in Python 2
Python list is not a list
What is a python map?
What is Python? What is it used for?
Is it a character string operation?
Infinite prime number generator in Python3
Prime number generation program by Python
It's a prime number ... Counting prime numbers ...
Delete a particular character in Python if it is the last
[Python] Correlation is below a certain level ・ Maximum number of features
Prime number
Project Euler # 7 "1000 1st prime number" in Python
What is a dog? Python installation volume
[python] [meta] Is the type of python a type?
[Python] Complete preprocessing Memo as it is
About February 02, 2020 * This is a Python article.
Creating a Python document generation tool because it is difficult to use sphinx
Why Python slicing is represented by a colon (:)
Is sys.settrace, a python genius feature, another language?
Prime number enumeration and primality test in Python
Arbitrary bit number prime creation Python code RSA
Let's write a Python program and run it
Launch a shell while a Python script is running
Tell me what a conformal map is, Python!
Python is easy
[Shell art] Only when it is a multiple of 3 and a number with 3 becomes stupid
What is python
Python is instance
python note: What does it mean to set a seed with random number generation?
What is Python
[Python] A program that counts the number of valleys
Basics of Python learning ~ What is a string literal? ~
Divisor enumeration when prime factorization is already known (Python)
What is God? Make a simple chatbot with python
Smartly announce that it is a deprecated implementation --debtcollerctor
[Python] Make sure the received function is a user-defined function
[Python] Randomly generate a large number of English names
Judge whether it is my child from the picture of Shiba Inu by deep learning (1)
It is blocked by Proxy, a connection error occurs in Python or pip, and it is retried.
Lambda + Python is good at restricting access with a large number of IP address lists
Created a class to check whether it is a constituent of TOPIX Core30, Large70, Mid400, Small
[Python] What is a tuple? Explains how to use without tuples and how to use it with examples.