Determine prime numbers with python

Use Sympy functions (recommended)

from sympy import isprime
isprime(11)

Execution example


print(isprime(11))
print(isprime(12))

Execution result


True
False

However, if the argument is in float format (for some reason) it will always return False, so you need to convert it to an int.

Execution example


print(isprime(11))
print(isprime(11.0))

#Convert argument to int type
print(isprime(int(11.0)))

Execution result


True
False
True

When creating your own function

import math

def isprime(x):
    if x == 1:
        return False
    if x == 2:
        return True
    for i in range(2, int(x**0.5) + 1):
        if x % i == 0:
            return False
    return True

Execution example


print(isprime(11))
print(isprime(12))

Execution result


True
False

Miscellaneous notes

I got the impression that sympy has various functions related to prime numbers. For example, how many prime numbers are included in the range of (a, b), a function to find the nth largest prime number, and so on. We plan to summarize it in the future. Click here for details (https://www.geeksforgeeks.org/prime-functions-python-sympy/)

Recommended Posts

Determine prime numbers with python
Algorithm learned with Python 4th: Prime numbers
Prime numbers in Python
Testing with random numbers in Python
[Python] nCr mod Compute prime numbers
FizzBuzz with Python3
Scraping with Python
Statistics with python
Play handwritten numbers with python Part 2 (identify)
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
I tried to create a list of prime numbers with python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
I searched for prime numbers in python
Excel with Python
Microcomputer with Python
Cast with python
Generate two correlated pseudo-random numbers (with Python sample)
Determine the numbers in the image taken with the webcam
Find prime numbers in Python as short as possible
Note for formatting numbers with python format function
Generate n correlated pseudo-random numbers (with Python sample)
[Python] Determine the type of iris with SVM
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Prime numbers and divisors
Learning Python with ChemTHEATER 03
Sequential search with Python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Posting tweets with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer