Skip to content

Instantly share code, notes, and snippets.

@apatrascu
Created June 5, 2017 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apatrascu/7666d8847c9df9fbcdd0c5417c49bfda to your computer and use it in GitHub Desktop.
Save apatrascu/7666d8847c9df9fbcdd0c5417c49bfda to your computer and use it in GitHub Desktop.
Timer unit: 1e-06 s
Total time: 181.595 s
File: 03.primes-v1.py
Function: primes at line 3
Line # Hits Time Per Hit % Time Line Contents
==============================================================
3 @profile
4 def primes(n):
5 40 107 2.7 0.0 if n == 2:
6 return [2]
7 40 49 1.2 0.0 elif n < 2:
8 return []
9 40 44 1.1 0.0 s = []
10 39999960 34410114 0.9 18.9 for i in range(3, n+1):
11 39999920 29570173 0.7 16.3 if i % 2 != 0:
12 19999960 14976433 0.7 8.2 s.append(i)
13 40 329 8.2 0.0 mroot = n ** 0.5
14 40 82 2.0 0.0 half = (n + 1) / 2 - 1
15 40 46 1.1 0.0 i = 0
16 40 30 0.8 0.0 m = 3
17 20000 17305 0.9 0.0 while m <= mroot:
18 19960 16418 0.8 0.0 if s[i]:
19 6680 6798 1.0 0.0 j = (m * m - 3) / 2
20 6680 6646 1.0 0.0 s[j] = 0
21 32449400 22509523 0.7 12.4 while j < half:
22 32442720 26671867 0.8 14.7 s[j] = 0
23 32442720 22913591 0.7 12.6 j += m
24 19960 15078 0.8 0.0 i = i + 1
25 19960 16170 0.8 0.0 m = 2 * i + 3
26 40 87 2.2 0.0 l = [2]
27 20000000 14292643 0.7 7.9 for x in s:
28 19999960 13753547 0.7 7.6 if x:
29 3139880 2417421 0.8 1.3 l.append(x)
30 40 33 0.8 0.0 return l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment