# # prime-number-list-two.py # # version: 1.0 # date: November 2014 # Author: r.croteau66@gmail.com # # - This script generate a list of prime numbers # - The list of primes is generated be elimination # - The output can be include in a python list # # output: primeLst = [2, 3, 5, 7, ...] # ############################### ### ### F U N C T I O N S ### # # This function elimenate all multiple of a prime in the # potentialPrimeList[] # def EliminateMultiple(prime, pplst, listsize): mult = prime * 2 while mult < listsize: pplst[mult] = 0 mult += prime # # This function print a list in a python format. # Ex: primeLst = [2, 3, 5, \ # 7, ...] # def PrintPrimeList(lst): lastindex = len(lst) - 1 line = "primeTup = (%5d, " % 2 # start the first output line for i in range(1, lastindex-1): # print all other primes followed by a comma line += "%5d, " % lst[i] if (i % 10 == 0): # print list in a table format line += "\\" print line line = " " line += "%5d )\n" % (lst[lastindex]) # print the very last prime and close the braket print line return ################################## ### ### M A I N ### primeLst = [] size = 1000 potentialPrimeLst = [1] * size # A list of possible prime: 1 -> is prime , 0 -> not prime for i in range(2, size): if potentialPrimeLst[i] == 1: EliminateMultiple(i, potentialPrimeLst, size) primeLst.append(i) PrintPrimeList(primeLst)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question