Prime number generation program by Python

Please prepare prime.txt without contents separately.

prime.py


import math
num = 3 #Number to start exploration
prime = [2] #Add 2 to prime numbers
prm_cnt = 1 #A variable that counts the found prime numbers
pfile = 'prime.txt' #File to write the found prime number

####################################################################
#Function Description:Last executed prime number list file read function
####################################################################
def load_prime():
    global prm_cnt
    global prime
    global num
    
    file_data = open(pfile, "r")#File reading
    print("!! load start !!\n")
    for line in file_data:
        prime.append(int(line))
    num = int(line)
    print("!! load end !!\n")
    file_data.close()
    prm_cnt = len(prime)

####################################################################
#Function Description:A function that flags a prime number when it is found
#Arg1:Number to judge whether it is a prime number Divide by the prime numbers that have appeared so far and check if there is a remainder
#Arg2:Number of prime numbers discovered so far
#Arg3:List of prime numbers can be referenced from outside the function
#Return:Primality test FLAG
####################################################################
def prime_func(num ,prm_lp, prm ):
    cnt = 0
    ret = "FALSE"
    for j in range(0,prm_lp):
        rem = num % prm[j]
        if rem == 0:
            cnt = 1#FALSE
            break
    if cnt == 0:
            prm_lp = prm_lp + 1#Count new prime numbers and increase the number of divisions for finding the next prime number by 1.
            prm.append(num)#Add new prime number to list
            ret = "TRUE"
    return ret

####################################################################
#Function Description:Prime number search
####################################################################
def calc_prime():
    global prime
    global prm_cnt
    global num
    pfile = 'prime.txt'
    E_OK = "TRUE"

    out_file = open(pfile,'a')
    while 1 :
        num = num + 2 #Search by 2 because there are only prime numbers in odd numbers
        ret = prime_func(
                num,      #Number to judge whether it is a prime number
                prm_cnt,  #Number of prime numbers
                prime     #List of prime numbers
            )
        if E_OK == ret: #Prime number discovery
            print(str(prime[len(prime)-1]) )
            prm_cnt = prm_cnt + 1
            out_file.write(str(num) + "\n")
     out_file.close()

####################################################################
#Function Description:main function
####################################################################
def Main():
    load_prime()
    calc_prime()

if __name__=="__main__":
    Main()

Recommended Posts

Prime number generation program by Python
I made a prime number generation program in Python
I made a prime number generation program in Python 2
Prime number 2 in Python
[python] Random number generation memorandum
Infinite prime number generator in Python3
Prime number
Random number generation summary by Numpy
A program that determines whether a number entered in Python is a prime number
Project Euler # 7 "1000 1st prime number" in Python
[Python] Shopping program
Derivation of multivariate t distribution and implementation of random number generation by python
Stock number ranking by Qiita tag with python
Arbitrary bit number prime creation Python code RSA
Judge whether it is a prime number [Python]
Python> Sort by number and sort by alphabet> Use sorted ()
Primality test by Python
Visualization memo by Python
"My Graph Generation Application" by Python (PySide + PyQtGraph) Part 2
MNIST image generation program creation by DCGAN (tensorflow tutorial)
Random string generation (Python)
07. Sentence generation by template
Beamformer response by python
JPEG image generation by specifying quality with Python + OpenCV
Prime numbers in Python
"My Graph Generation Application" by Python (PySide + PyQtGraph) Part 1
Axisymmetric stress analysis program by Python (square element) [revised edition]
[Python] A program that creates a two-dimensional array by combining integers
Mass generation of QR code with character display by Python
Speech recognition by Python MFCC
[Python 3] Prime factorization in 14 lines
EXE Web API by Python
Parameter setting by python configparser
Pin python managed by conda
Determine prime numbers with python
Homebrew Python --Youtube Search Program
ABC161D Lunlun Number with python3
Keyword extraction by MeCab (python)
Separate numbers by 3 digits (python)
Markov switching model by Python
Fibonacci and prime implementations (python)
Image processing by python (Pillow)
Python started by C programmers
Platform (OS) determination by Python
Sort by date in python
[Python of Hikari-] Chapter 07-02 Exception handling (continuous execution of the program by exception handling)
I made a prime number table output program in various languages
A script that returns 0, 1 attached to the first Python prime number