This method requires that you fill in the array with some known prime values. The more primes you add to the list the more accurate the checker will become.
I've ran this against the first 168 prime numbers without it skipping a beat. I do recommend that you grab your own list of prime numbers to test against it and see if anything returns "False" when it should return "True".
def isprime[n]:
if n==0 or n==1: return False
testlist = [2,3,5,7,9,11]
grand = 0
for test in testlist:
if n==test: return True
elif n%test==0: return False
else: grand += 1
if grand > 0: return True
else: return False
Using this in your favor can be done in this manner:
primearr = []
for x in xrange[1, 1000000]:
if isprime[x] == True:
primearr.append[x]
This code is fast enough for me to run the first 1000000 numbers in about a second or two. Now if you want to view the results then it's best that you throw them into a text document than reading them out [which can freeze or crash Python] like this:
with open['primelist.txt', 'a'] as f:
for prime in primearr:
f.write['{},'.format[prime]]
Then you can check the directory where you have Python installed to find the text document unless you specified a directory in the filename like this:
with open['C:\\Users\\yourname\\Desktop\\primelist.txt', 'a'] as f:
EDIT
I know this is a necro-edit but I feel that it's necessary because I did get it to successfully check the first 10,000,000 prime numbers with the same exact prime test list.
I got the first 10,000,000 primes from this source: //www.primos.mat.br/2T_en.html
primelist = []
with open['first-ten-million-primes', 'r'] as r:
primes = r.readlines[]
for prime in primes:
for num in prime.split['\t']:
primelist.append[int[num]]
testlist = [2,3,5,7,9,11]
success, failure = [], []
for prime in primelist:
grand = 0
for test in testlist:
if prime % test == 0 and prime != test: grand += 1
if grand > 0: failure.append[prime]
else: success.append[prime]
print['Successes: {}\r\nFailures: {}'.format[len[success], len[failure]]]
That output returned:
Successes: 10000000
Failures: 0
This was all on Python 3.6.5 x64.
I need a little help interpreting and completing this project. I double starred the part I'm on, any idea on how to do it? Additionally, I need some help with identifying what gets returned to what function. My code is linked below. The project is:
The Sieve of Eratosthenes is a famous algorithm that generates a
list of the prime numbers up to a specified limit. We maintain a
list of the prime numbers found so far and a candidate list of the
integers up to the specified limit that are not multiples of the
primes found so far. Initially, the list of primes is empty and
the list of candidates contains all the integers from 2 up to the
limit. If the limit is 20, we start with the following lists:
primes=[]
candidates=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
At each step, the next prime number is the first number on the
list of candidates. It is appended to the list of primes and
all of its multiples are removed from the list of candidates.
The algorithm stops when the list of candidates is empty. The
following table shows the lists after each step:
Primes Candidates
[] [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
[2] [3,5,7,9,11,13,15,17,19]
[2,3] [5,7,11,13,17,19]
[2,3,5] [7,11,13,17,19]
[2,3,5,7] [11,13,17,19]
[2,3,5,7,11] [13,17,19]
[2,3,5,7,11,13] [17,19]
[2,3,5,7,11,13,17] [19]
[2,3,5,7,11,13,17,19] []
Write a Python program that uses the Sieve of Eratosthenes to print
a list of the primes up to a limit read from the keyboard. Print
the list in columns, with 5 numbers printed on each line. Each
column should be 12 characters wide.
Your program must define the following functions, as well as any
additional functions that you find useful.
list = build[limit]
Return a list of all the whole numbers from 2 through limit.
For example, build[5] must return the list [2,3,4,5]. Your
program will call this function to build the initial list
of candidates.
newcands = sieve[number, oldcands]
**Return a new version of the candidates list, obtained by
removing all multiples of number from the old version of the
list. [Hint: Create a new list which is initially empty.
Loop over the old list appending to the new list those items
which are not multiples of number. A value is not a multiple
of number if value % number != 0.]**
putlist[lst]
Print the items on the specified list in 5 columns. Each column
must be right justified and 12 characters wide. Your program
will call this function to print the list of primes. [Hint:
Print one item on each iteration of a loop. Use the statement
print["{0:>12}".format[item], end=""]
to right-justify the item in 12 columns without advancing to a
new line. Hold the column number in a variable. Advance to a
new line and reset the variable when its value reaches 5.]
Hand in a listing of your program and the output from a successful
run that prints the prime numbers up to 500:
Limit? 500
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
101 103 107 109 113
127 131 137 139 149
151 157 163 167 173
179 181 191 193 197
199 211 223 227 229
233 239 241 251 257
263 269 271 277 281
283 293 307 311 313
317 331 337 347 349
353 359 367 373 379
383 389 397 401 409
419 421 431 433 439
443 449 457 461 463
467 479 487 491 499
My code: //pastebin.com/7iJ6E2cG or:
def build[limit]:
list1 = []
list2 = []
for number in range[limit + 1]:
if number not in range[2]:
list1.append[number]
print[list1]
for number in list1: #this is the part I'm stuck on, how do I correctly append primes to list2?
list2.append[number]
for number in list2:
if number % [number in list1] == 0:
list1.remove[number]
list = build[10]
#newcands = sieve[number, oldcands]