Skip to content

Commit 8219f23

Browse files
committed
Rearrange, add stuff
1 parent fed1b28 commit 8219f23

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

Prime Numbers/README.md

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Sieve Primes
22

3+
## Sieve of Eratosthenes
34
Goal: To quickly get an array of prime numbers with a specified limit using the Sieve of Eratosthenes.
45

5-
The Sieve of Eratosthenes is an algorithm that finds all primes numbers up to any given limit.
6+
The Sieve of Eratosthenes is an algorithm that finds all primes numbers up to any given limit by iteratively marking composites of each prime starting with multiples of 2. [Seeing this algorithm][1] in action is the best way to understand it.
67

7-
Here's an implementation in Swift that should be easy to understand:
8+
9+
### Implementation
810

911
```
1012
func primesTo(max: Int) -> [Int] {
@@ -30,10 +32,71 @@ print(primesArray)
3032
// prints [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
3133
```
3234

33-
--*write some introduction to the Sieve of Eratosthenes*--
35+
## Sieve of Atkin
36+
37+
Goal: To quickly get an array of prime numbers with a specified limit using the Sieve of Atkin.
38+
39+
The Sieve of Atkin is an algorithm that finds all primes numbers up to any given limit.
40+
41+
--*write some introduction to the Sieve of Atkin*--
42+
43+
### Implementation
44+
45+
```
46+
func primesTo(max: Int) -> [Int] {
47+
var is_prime = [Bool](count: max + 1, repeatedValue: false)
48+
is_prime[2] = true
49+
is_prime[3] = true
50+
let limit = Int(ceil(sqrt(Double(max))))
51+
for x in 1...limit {
52+
for y in 1...limit {
53+
var num = 4 * x * x + y * y
54+
if (num <= max && (num % 12 == 1 || num % 12 == 5)) {
55+
is_prime[num] = true
56+
}
57+
num = 3 * x * x + y * y
58+
if (num <= max && num % 12 == 7) {
59+
is_prime[num] = true
60+
}
61+
if (x > y) {
62+
num = 3 * x * x - y * y
63+
if (num <= max && num % 12 == 11) {
64+
is_prime[num] = true
65+
}
66+
}
67+
}
68+
}
69+
if limit > 5 {
70+
for i in 5...limit {
71+
if is_prime[i] {
72+
for j in (i * i)..i..max {
73+
is_prime[j] = false
74+
}
75+
}
76+
}
77+
}
78+
var primesArray = [Int]()
79+
for (idx, val) in is_prime.enumerate() {
80+
if val == true { primesArray.append(idx) }
81+
}
82+
return primesArray
83+
}
84+
```
85+
Put this code in a playground and test it like so:
86+
87+
```
88+
let primesArray = primesTo(50)
89+
print(primesArray)
90+
// prints [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
91+
```
3492

3593
## See also
3694

95+
96+
[1]:https://en.wikipedia.org/wiki/File:Sieve_of_Eratosthenes_animation.gif#/media/File:Sieve_of_Eratosthenes_animation.gif
97+
3798
[Sieve of Eratosthenes on Wikipedia](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
3899

100+
[Sieve of Atkin on Wikipedia](https://en.wikipedia.org/wiki/Sieve_of_Atkin)
101+
39102
*Written for Swift Algorithm Club by Pratikbhai Patel*

0 commit comments

Comments
 (0)