Skip to content

Commit 216ce99

Browse files
committed
Add prime generation
1 parent 3d7953a commit 216ce99

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

Prime Numbers/PrimeSieve.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
infix operator ^^ { associativity left precedence 160 }
2+
func ^^ (radix: Int, power: Int) -> Int {
3+
return Int(pow(CGFloat(radix), CGFloat(power)))
4+
}
5+
6+
infix operator .. {associativity left precedence 60 }
7+
8+
func ..<T: Strideable>(left: T, right: T.Stride) -> (T, T.Stride) {
9+
return (left, right)
10+
}
11+
12+
func ..<T: Strideable>(left: (T, T.Stride), right: T) -> [T] {
13+
return [T](left.0.stride(through: right, by: left.1))
14+
}
15+
16+
17+
func primesTo(max: Int) -> [Int] {
18+
let m = Int(sqrt(ceil(Double(max))))
19+
let set = NSMutableSet(array: 3..2..max)
20+
set.addObject(2)
21+
for i in (2..1..m) {
22+
if (set.containsObject(i)) {
23+
for j in i^^2..i..max {
24+
set.removeObject(j)
25+
}
26+
}
27+
}
28+
return set.sortedArrayUsingDescriptors([NSSortDescriptor(key: "integerValue", ascending: true)]) as! [Int]
29+
}

Prime Numbers/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Sieve Primes
2+
3+
Goal: To quickly get an array of prime numbers with a specified limit using the Sieve of Eratosthenes.
4+
5+
The Sieve of Eratosthenes is an algorithm that finds all primes numbers up to any given limit.
6+
7+
Here's an implementation in Swift that should be easy to understand:
8+
9+
```
10+
func primesTo(max: Int) -> [Int] {
11+
let m = Int(sqrt(ceil(Double(max))))
12+
let set = NSMutableSet(array: 3..2..max)
13+
set.addObject(2)
14+
for i in (2..1..m) {
15+
if (set.containsObject(i)) {
16+
for j in i^^2..i..max {
17+
set.removeObject(j)
18+
}
19+
}
20+
}
21+
return set.sortedArrayUsingDescriptors([NSSortDescriptor(key: "integerValue", ascending: true)]) as! [Int]
22+
}
23+
```
24+
25+
Put this code in a playground and test it like so:
26+
27+
```
28+
let primesArray = primesTo(50)
29+
print(primesArray)
30+
// prints [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
31+
```
32+
33+
--*write some introduction to the Sieve of Eratosthenes*--
34+
35+
## See also
36+
37+
[Sieve of Eratosthenes on Wikipedia](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
38+
39+
*Written for Swift Algorithm Club by Pratikbhai Patel*
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//: Playground - noun: a place where people can play
2+
3+
import Foundation
4+
import UIKit
5+
6+
infix operator ^^ { associativity left precedence 160 }
7+
func ^^ (radix: Int, power: Int) -> Int {
8+
return Int(pow(CGFloat(radix), CGFloat(power)))
9+
}
10+
11+
infix operator .. {associativity left precedence 60 }
12+
13+
func ..<T: Strideable>(left: T, right: T.Stride) -> (T, T.Stride) {
14+
return (left, right)
15+
}
16+
17+
func ..<T: Strideable>(left: (T, T.Stride), right: T) -> [T] {
18+
return [T](left.0.stride(through: right, by: left.1))
19+
}
20+
21+
func primesTo(max: Int) -> [Int] {
22+
assert(max > 1, "Prime numbers can only be above 1")
23+
print("Getting all primes under \(max)")
24+
let m = Int(sqrt(ceil(Double(max))))
25+
print("Need to remove all multiples of numbers through \(m)")
26+
let set = NSMutableSet(array: 3..2..max)
27+
set.addObject(2)
28+
print(set)
29+
for i in (2..1..m) {
30+
if (set.containsObject(i)) {
31+
print("Removing all multiples of \(i)")
32+
for j in i^^2..i..max {
33+
print("removing j: \(j)")
34+
set.removeObject(j)
35+
}
36+
}
37+
}
38+
return set.sortedArrayUsingDescriptors([NSSortDescriptor(key: "integerValue", ascending: true)]) as! [Int]
39+
}
40+
41+
let startDate = NSDate().timeIntervalSince1970 * 1000
42+
print(primesTo(50))
43+
let endDate = NSDate().timeIntervalSince1970 * 1000
44+
print("Prime generation time: \(endDate - startDate) ms.")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Prime Numbers/SievePrimes.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)