-
Notifications
You must be signed in to change notification settings - Fork 0
/
primesieve.arw
60 lines (56 loc) · 1.15 KB
/
primesieve.arw
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
56
57
58
59
60
function
/--> bool[] primeSieve(int max)
| bool[] primes[max]
| primes[0] = false
| primes[1] = false
|
| int i
| i = 2
| /-->
| | primes[i] = true
| | i = i + 1
| \--< i < max
|
| int firstPrime
| firstPrime = 1
| /-->
| | bool firstIsComposite
| | /-->
| | | firstPrime = firstPrime + 1
| | | firstIsComposite = false
| | | /--< firstPrime > max or firstPrime == max
| | | | firstIsComposite = not primes[firstPrime]
| | | \-->
| | \--< firstIsComposite
| | /--< firstPrime > max or firstPrime == max
| | | primes[firstPrime] = true
| | | int composite
| | | composite = firstPrime * 2
| | | /--< composite > max
| | | | /-->
| | | | | primes[composite] = false
| | | | | composite = composite + firstPrime
| | | | \--< composite < max
| | | \-->
| | \-->
| \--< firstPrime < max - 1
^ primes
main
int max
max = 1000
bool[] primes
primes = primeSieve(max)
print "Enter a number and I'll tell you whether or not it's prime!"
int testNum
testNum = input int
/--< testNum < 0 or testNum > max - 1
| /--< not primes[testNum]
| | print "Prime"
| \-->
| /--< primes[testNum]
| | print "Not prime"
| \-->
\-->
/--< not (testNum < 0 or testNum > max - 1)
| print "Out of bounds"
\-->