-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime.py
More file actions
executable file
·55 lines (51 loc) · 1.15 KB
/
prime.py
File metadata and controls
executable file
·55 lines (51 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
#direct translation from xr
#terribly unpythonic, obviously
limit = 10000
x = 2
numbers = [0, 0]
primes = []
num_primes = 0
while (x < limit):
numbers.append(x)
x = x + 1
p = 2
marked_all = 0
while (marked_all == 0):
#"p = " show; p string showln
#mark multiples of p
mp = p
while (mp < limit):
if (numbers[mp] > p):
numbers[mp] = 0
#"marking: " show; mp string showln
mp = mp + p
#find first number greater than p not marked
i = 0
inum = 0
marked = 0
while (i < limit and marked != 1):
inum = numbers[i]
#"is inum > p? (" show; inum string show; " > " show; p string show; ")" showln
if (inum > p):
marked = 1
p = inum
#"restarting with p = " show; p string showln
i = i + 1
if (marked == 0):
#"marked all" showln
marked_all = 1
#spit out primes
n = 0
num = 0
while (n < limit):
num = numbers[n]
if (num > 0):
primes.append(num)
num_primes = num_primes + 1
n = n + 1
#print primes
pc = 0
while (pc < num_primes):
print primes[pc]
pc = pc + 1