Find prime numbers in Python as short as possible

Introduction

Looking at this article, I was impressed if Tetris could be made in just seven lines. But I can't javascript at all, so I don't understand the meaning. (I mean, I understand the syntax, but I don't know what I'm doing) However, I realized that short coating would be cool, and I thought that Python might be able to do that, so I will introduce it. Also, I don't have vocabulary, so please forgive me even if the sentence is strange.

Primality test algorithm normally

First, try building a primality test program normally. (Execution speed is not considered)

JudgeNum=int(input("number?"))
prime=True
for i in range(2,JudgeNum):
    if JudgeNum%i==0:
        prime=False
print(prime)

Execution result

number?5
True

This is the usual answer. Even if you show this to other people, it is a reaction like "Hmm". Let's shorten this a little for the time being

Short primality test algorithm

First

int(input("number?"))#Is
int(input())#Replace with
JudgeNum#Is
n#Replace with

Then use the comprehension notation.

prime=True
for i in range(2,n):
    if n%i==0:
        prime=False

This guy

prime=not[i for i in range(2,n) if n%2==0]

Is almost the same as In this comprehension, the number of cracks is added to the list.

In python, an empty list is evaluated as False, so by flipping it with not, the empty list (which means that it was not divisible by all numbers) is a prime number. (vocabulary) (I tried to flip it with ~ instead of not for a moment, but I couldn't because the flip is a list.)

Then you don't need the variable prime. So

print(not[i for i in range(2,n) if n%2==0])

Can be shortened with. Eventually

n=int(input())
print(not[i for i in range(2,n) if n%2==0])

I was able to shorten it to two lines.

Primality test algorithm for one line

If you've come this far, I'd like to have one line. But when you declare a variable, you have to start a new line. Is it necessary to have at least two lines because the primality test algorithm is almost impossible without using variables? I wish I could do it with commas like function arguments ... . . . ! Should I use a function! However, it is impossible with ordinary functions. So I decided to use an anonymous function (lambda). Just take n as an argument

(lambda n:print(not[i for i in range(2,n)if n%i==0]))(int(input()))

Now you can compress it to one line. Eh? Do you know that two lines have fewer characters?

Finally

This is a short coating that I worked hard on. If you have a code shorter than this, please let me know.

Recommended Posts

Find prime numbers in Python as short as possible
Prime numbers in Python
I searched for prime numbers in python
Project Euler # 10 "sum of prime numbers" in Python
Recursively find prime numbers
List find in Python
Prime number 2 in Python
Handle prime numbers in Python / Ruby / PHP / Golang (Go)
[Python 3] Prime factorization in 14 lines
Find the difference in Python
Determine prime numbers with python
Find permutations / combinations in Python
Let's find pi in Python
Handle complex numbers in Python
Testing with random numbers in Python
Use fabric as is in python (fabric3)
Infinite prime number generator in Python3
Law of large numbers in python
[Python] nCr mod Compute prime numbers
Project Euler # 3 "Maximum Prime Factors" in Python
Find files like find on linux in Python
Algorithm learned with Python 4th: Prime numbers
Write a short property definition in Python
Project Euler # 7 "1000 1st prime number" in Python
Find and check inverse matrix in Python
Find (deterministic finite) Cartesian automata in Python
Project Euler # 2 "Even Fibonacci Numbers" in Python
I tried to implement merge sort in Python with as few lines as possible
Techniques often used in python short coding (Notepad)
AtCoder: Python: Automate sample testing as much as possible.
Prime number enumeration and primality test in Python
Minimal implementation to do Union Find in Python
Find the divisor of the value entered in python
Prime factorization of integers entered in Python ver.1
Find the solution of the nth-order equation in python
Class inheritance practice in python as seen in sklearn
Project Euler # 13 "Sum of Large Numbers" in Python
Get files, functions, line numbers running in python
Prime factorization ver.2 of integers input in Python
How to output "Ketsumaimo" as standard output in Python
Use python in Docker container as Pycharm interpreter
[Python] Find the transposed matrix in a comprehension
python xlwings: Find the cell in the last row
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python