Prime numbers in Python

It may be surprisingly difficult to find a prime number and divide it by the prime number ... How do you do it with Python?

When I saw the gif of [Eratosthenes Sieve](https://ja.wikipedia.org/wiki/Eratosthenes Sieve) on Wikipedia, I thought it would be possible to do this, so I tried it. By example? With lambda binding.


do = lambda *x : x
switch = lambda x : x[-1]


primes = (
  lambda n : find_primes( range( 2, n + 1 ), ( n + 1 ) ** 0.5, [ ] )
)

find_primes = (
  lambda xs, end_point, ys :
    
    switch(
      xs[0] <= end_point and do( ys.append( xs[0] )
              				, find_primes( remove_fst_multiples( xs ) , end_point, ys )
            				)
      
      or   					do( ys.extend( xs ) 
              				, ys 
            				)
    )
)

remove_fst_multiples = (
  lambda xs : [ x for x in xs if x % xs[0]  ]
)

print primes(100)

Execution result:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

The algorithm is the same as Wikipedia.

primes is a function that just passes values in a nice way. The entity is find_primes.

find_primes is

remove_fst_multiples takes a list and returns a list excluding multiples of the first value of the list.

Try using itertools

I received a comment so I tried using it. However, I don't know what it is, so I googled it and replaced the search list with an iterator for the time being. Please see the comments and other announcements on how to use it properly.

from itertools import count, ifilter, takewhile

do = lambda *x : x
switch = lambda x : x[-1]


primes = ( lambda n : 
  find_primes( 
    takewhile(lambda x : x < n + 1, count(2))
  , ( n + 1 ) ** 0.5
  , [ ] 
  )
)

find_primes = (
  lambda xs, end_point, ys :
    (lambda a = next( xs ) :
      
      switch(
       a <= end_point and do( ys.append( a )
               			, find_primes( remove_multiples( xs, a ) , end_point, ys )
              			)
        
        or   			do( ys.append( a )
                		, ys.extend(list( xs) ) 
               			, ys 
             			)
      )
    )()
)

remove_multiples = (
  lambda xs, a : ifilter(lambda x : x % a , xs )
)

print primes(100)

Recommended Posts

Prime numbers in Python
Prime number 2 in Python
I searched for prime numbers in python
Project Euler # 10 "sum of prime numbers" in Python
Determine prime numbers with python
Find prime numbers in Python as short as possible
Handle complex numbers in Python
Handle prime numbers in Python / Ruby / PHP / Golang (Go)
Testing with random numbers in Python
Infinite prime number generator in Python3
Law of large numbers in python
[Python] nCr mod Compute prime numbers
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Project Euler # 3 "Maximum Prime Factors" in Python
Algorithm learned with Python 4th: Prime numbers
Project Euler # 7 "1000 1st prime number" in Python
Project Euler # 2 "Even Fibonacci Numbers" in Python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python