Created
May 11, 2017 20:10
-
-
Save waynew/843aec46c7ab45dec98289b7cf64b608 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#from math import floor | |
# | |
#LIMIT = 1000 | |
# | |
#prime_list = [] | |
#list_primes2 = [True for x in range(LIMIT)] | |
#list_primes2[0], list_primes2[1] = False, False | |
# | |
#for index, value in enumerate(list_primes2): | |
# if value: | |
# for x in range(index*2, LIMIT, index): | |
# list_primes2[x] = False | |
# | |
# prime_list.append(index) | |
# | |
#print(prime_list) | |
##for i, is_prime in enumerate(list_primes2): | |
## print(i, is_prime) | |
# | |
#from math import floor | |
# | |
#prime_list = [] | |
#list_primes2 = [True for x in range(1000)] | |
#list_primes2[0], list_primes2[1] = False, False | |
# | |
#for index, value in enumerate(list_primes2): | |
# if value: | |
# for x in range(floor(999/index)+1): | |
# list_primes2[index*x] = False | |
# | |
# prime_list.append(index) | |
# | |
#print(prime_list) | |
# | |
# Your problem is that you're either unclear on how the sieve is | |
# *supposed* to work, or how range really works. You can see that | |
# if you print out your list that you're using to track whether or | |
# not a number is prime: | |
from math import floor | |
LIMIT = 10 | |
prime_list = [] | |
# I've renamed it to `is_prime`, because that's what we're tracking | |
is_prime = [True for x in range(LIMIT)] | |
is_prime[0], is_prime[1] = False, False | |
for index, value in enumerate(is_prime): | |
if value: | |
for x in range(floor(LIMIT/index)): | |
print(f'Marking {index*x} as not prime') | |
is_prime[index*x] = False | |
print() | |
prime_list.append(index) | |
for num, is_prime in enumerate(is_prime): | |
print(num, 'is prime?', is_prime) | |
# Outputs: | |
# 0 is prime? False | |
# 1 is prime? False | |
# 2 is prime? False | |
# 3 is prime? False | |
# 4 is prime? False | |
# 5 is prime? False | |
# 6 is prime? False | |
# 7 is prime? True | |
# 8 is prime? False | |
# 9 is prime? True | |
# We can see why that's happening if we show what's going on inside this | |
# loop: | |
# for x in range(floor(LIMIT/index)): | |
# print(f'Marking {index*x} as not prime') | |
# is_prime[index*x] = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment