Primality test with Python

What is a prime number?

A positive integer with no divisor outside of one and its number itself.

Well, to put it simply, it is a number that can only be divided by 1 and yourself. In other words, it is a number that "has only two divisors". Other numbers are called composite numbers.

So for the time being, I will write a program that displays prime numbers from 1 to 10.

n_list = range(2, 10)

for i in range(2, int(10 ** 0.5) + 1):
  n_list = [x for x in n_list if (x == i or x % i !=0)]

for j in n_list:
  print(j)  

#Execution result
2
3
5
7

Other writing style 1

Determine if what is assigned to N is a prime number

def calc_prime(N):
  for p in range(2, N):
    if N % p == 0:
        return str(N) + ' is composit'
  return str(N) + ' is PRIME!!'

calc_prime(7)

#Execution result
'7is PRIME!!'

Other writing style 2

A function that displays prime numbers up to N, where N is a natural number

def calc_prime(N):
  n_list = range(2, N)

  for i in range(2, int(N ** 0.5) + 1):
    n_list = [ x for x in n_list if (x == i or x % i !=0)]

  for j in n_list:
    print(j)

calc_prime(10)
#Execution result
2
3
5
7

Other writing style 3

Determine if the natural number assigned to n is a prime number.

n = 7
for p in range(2, n):
   if n % p == 0:
      print(str(n) + ' is composite.')
      break
else:
  print(str(n) + ' is PRIME!!')

#Execution result
7 is PRIME!!

Summary

This time I wrote programming to judge prime numbers. I'm not very familiar with it, but it seems that there are various ways to determine prime numbers, so if you are interested, please check it out. Also, I think there is a way to speed up the calculation, so if you are interested, please implement it.

Recommended Posts

Primality test with Python
Primality test with python
Primality test by Python
Algorithm in Python (primality test)
Unit test log output with python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Python Integrity Test
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
[Python] Super easy test with assert statement
Stress Test with Locust written in Python
Test Python non-functional programs with GitLab CI
WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
Generate Japanese test data with Python faker
Post Test 3 (Working with PosgreSQL in Python)
How to do portmanteau test with python
Integrating with setuptools / python setup.py test / pytest-runner
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Strengthen with code test ⑦
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Create test data like that with Python (Part 1)
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Strengthen with code test ⑨
Learning Python with ChemTHEATER 05-1
Strengthen with code test ③
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