Skip to content

Commit 0464cbb

Browse files
author
Chris Pilcher
committed
Merge branch 'master' of git://github.com/tikipatel/swift-algorithm-club into tikipatel-prime-numbers
2 parents 21b05b1 + 658599e commit 0464cbb

File tree

20 files changed

+1393
-0
lines changed

20 files changed

+1393
-0
lines changed

Prime Numbers/Atkins_Sieve.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
var is_prime = [Bool](count: max + 1, repeatedValue: false)
19+
is_prime[2] = true
20+
is_prime[3] = true
21+
let limit = Int(ceil(sqrt(Double(max))))
22+
for x in 1...limit {
23+
for y in 1...limit {
24+
var num = 4 * x * x + y * y
25+
if (num <= max && (num % 12 == 1 || num % 12 == 5)) {
26+
is_prime[num] = true
27+
}
28+
num = 3 * x * x + y * y
29+
if (num <= max && num % 12 == 7) {
30+
is_prime[num] = true
31+
}
32+
if (x > y) {
33+
num = 3 * x * x - y * y
34+
if (num <= max && num % 12 == 11) {
35+
is_prime[num] = true
36+
}
37+
}
38+
}
39+
}
40+
if limit > 5 {
41+
for i in 5...limit {
42+
if is_prime[i] {
43+
for j in (i * i)..i..max {
44+
is_prime[j] = false
45+
}
46+
}
47+
}
48+
}
49+
var primesArray = [Int]()
50+
for (idx, val) in is_prime.enumerate() {
51+
if val == true { primesArray.append(idx) }
52+
}
53+
return primesArray
54+
}
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/PrimeGenerator.swift

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// Operators.swift
3+
// Primes
4+
//
5+
// Created by Pratikbhai Patel on 6/13/16.
6+
// Copyright © 2016 Pratikbhai Patel. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
infix operator ^^ { associativity left precedence 160 }
12+
func ^^ (radix: Int, power: Int) -> Int {
13+
return Int(pow(CGFloat(radix), CGFloat(power)))
14+
}
15+
16+
infix operator .. {associativity left precedence 60 }
17+
18+
func ..<T: Strideable>(left: T, right: T.Stride) -> (T, T.Stride) {
19+
return (left, right)
20+
}
21+
22+
func ..<T: Strideable>(left: (T, T.Stride), right: T) -> [T] {
23+
return [T](stride(from: left.0, through: right, by: left.1))
24+
}
25+
26+
class PrimeGenerator {
27+
28+
static let sharedInstance = PrimeGenerator()
29+
private let eraQueue = DispatchQueue(label: "eraQueue")
30+
private let atQueue = DispatchQueue(label: "atQueue")
31+
32+
func eratosthenesPrimes(_ max: Int, completion:([Int]) -> ()){
33+
34+
eraQueue.async {
35+
let m = Int(sqrt(ceil(Double(max))))
36+
let set = NSMutableSet(array: 3..2..max)
37+
set.add(2)
38+
for i in (2..1..m) {
39+
if (set.contains(i)) {
40+
for j in i^^2..i..max {
41+
set.remove(j)
42+
}
43+
}
44+
}
45+
completion(set.sortedArray(using: [SortDescriptor(key: "integerValue", ascending: true)]) as! [Int])
46+
}
47+
}
48+
49+
func atkinsPrimes(_ max: Int, completion:([Int]) -> ()) {
50+
atQueue.async {
51+
var is_prime = [Bool](repeating: false, count: max + 1)
52+
is_prime[2] = true
53+
is_prime[3] = true
54+
let limit = Int(ceil(sqrt(Double(max))))
55+
for x in 1...limit {
56+
for y in 1...limit {
57+
var num = 4 * x * x + y * y
58+
if (num <= max && (num % 12 == 1 || num % 12 == 5)) {
59+
is_prime[num] = true
60+
}
61+
num = 3 * x * x + y * y
62+
if (num <= max && num % 12 == 7) {
63+
is_prime[num] = true
64+
}
65+
if (x > y) {
66+
num = 3 * x * x - y * y
67+
if (num <= max && num % 12 == 11) {
68+
is_prime[num] = true
69+
}
70+
}
71+
}
72+
}
73+
74+
if limit > 5 {
75+
for i in 5...limit {
76+
if is_prime[i] {
77+
for j in (i * i)..i..max {
78+
is_prime[j] = false
79+
}
80+
}
81+
}
82+
}
83+
var primesArray = [Int]()
84+
for (idx, val) in is_prime.enumerated() {
85+
if val == true { primesArray.append(idx) }
86+
}
87+
completion(primesArray)
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)