Created
July 4, 2015 12:42
-
-
Save soundlake/976ff65c774f747516b3 to your computer and use it in GitHub Desktop.
to print out prime numbers below certain integer.
This file contains hidden or 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
number = int(input('input a number: ')) | |
for candidate in range(2, number + 1): | |
is_candidate_a_prime = True | |
for i in range(2, candidate): | |
if candidate % i == 0: | |
is_candidate_a_prime = False | |
break | |
if is_candidate_a_prime: | |
print(candidate) |
This file contains hidden or 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
primes = [2, 3] | |
num = int(input('input a number: ')) | |
for candidate in range(primes[-1] + 2, num + 1, 2): | |
for prime in primes: | |
if prime > candidate ** 0.5: | |
primes.append(candidate) | |
break | |
if candidate % prime == 0: | |
break | |
for prime in primes: | |
if prime <= num: | |
print(prime) | |
else: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment