# # prime-number-list.py # # version: 1.0 # date: October 2014 # Author: r.croteau66@gmail.com # # - This script generate a list of prime numbers using the # euclide algorithm. (see IsPrime()) # - This is a slow script for large list # - 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 test the number using the euclide algorithm. # I.E. We test the number agains every primes # already in the prime list. If no prime can divide # the number, we conclude it must be a prime itself. # def IsPrime(nb, primeLst): for prime in primeLst: if nb % prime == 0: return False return True # # This function print a list in a python format. # Ex: primeLst = [2, 3, 5, \ # 7, ...] # def PrintPrimeList(lst): lastindex = len(lst) - 1 line = "primeLst = [%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 = [2] for i in range(3, 1000, 2): if IsPrime(i, primeLst): 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