Last active
November 3, 2017 14:10
-
-
Save wuerges/60b2ea296f2c90b28d7d7711a31306b1 to your computer and use it in GitHub Desktop.
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
import math | |
import timeit | |
n = 1000000 | |
def primes(): | |
mark = n * [False] | |
ps = [2] | |
mark[2] = True | |
for x in range(3, n, 2): | |
if not mark[x]: | |
ps.append(x) | |
for m in range(x * x, n, 2 * x): | |
mark[m] = True | |
return ps | |
def primo(nump): | |
count = 0 | |
for x in range(2, math.floor(math.sqrt(nump)) + 1): | |
if nump % x == 0: | |
count = 1 | |
return False | |
if count == 0: | |
return True | |
def primes_lerdo(): | |
ps = [] | |
for x in range(2, n): | |
if primo(x): | |
ps.append(x) | |
return ps | |
print("versão do crivo, O(n * log n * log log n):", timeit.timeit(primes, number=1)) | |
print("versão sem crivo, O(n ** 2):", timeit.timeit(primes_lerdo, number=1)) | |
#print(primes(200000) | |
#print(sum(ps)) | |
#print(ps[5]) | |
#print(ps[10000]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment