Skip to content

Commit 0e81e07

Browse files
committed
Refactor
Create singleton class
1 parent fbba3db commit 0e81e07

File tree

4 files changed

+176
-97
lines changed

4 files changed

+176
-97
lines changed

Prime Numbers/PrimeGenerator.swift

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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](left.0.stride(through: right, by: left.1))
24+
}
25+
26+
class PrimeGenerator {
27+
28+
static let sharedInstance = PrimeGenerator()
29+
30+
func eratosthenesPrimes(max: Int) -> [Int] {
31+
let m = Int(sqrt(ceil(Double(max))))
32+
let set = NSMutableSet(array: 3..2..max)
33+
set.addObject(2)
34+
for i in (2..1..m) {
35+
if (set.containsObject(i)) {
36+
for j in i^^2..i..max {
37+
set.removeObject(j)
38+
}
39+
}
40+
}
41+
return set.sortedArrayUsingDescriptors([NSSortDescriptor(key: "integerValue", ascending: true)]) as! [Int]
42+
}
43+
44+
func atkinsPrimes(max: Int) -> [Int] {
45+
var is_prime = [Bool](count: max + 1, repeatedValue: false)
46+
is_prime[2] = true
47+
is_prime[3] = true
48+
let limit = Int(ceil(sqrt(Double(max))))
49+
for x in 1...limit {
50+
for y in 1...limit {
51+
var num = 4 * x * x + y * y
52+
if (num <= max && (num % 12 == 1 || num % 12 == 5)) {
53+
is_prime[num] = true
54+
}
55+
num = 3 * x * x + y * y
56+
if (num <= max && num % 12 == 7) {
57+
is_prime[num] = true
58+
}
59+
if (x > y) {
60+
num = 3 * x * x - y * y
61+
if (num <= max && num % 12 == 11) {
62+
is_prime[num] = true
63+
}
64+
}
65+
}
66+
}
67+
if limit > 5 {
68+
for i in 5...limit {
69+
if is_prime[i] {
70+
for j in (i * i)..i..max {
71+
is_prime[j] = false
72+
}
73+
}
74+
}
75+
}
76+
var primesArray = [Int]()
77+
for (idx, val) in is_prime.enumerate() {
78+
if val == true { primesArray.append(idx) }
79+
}
80+
return primesArray
81+
}
82+
83+
}

Prime Numbers/Primes/Primes.xcodeproj/project.pbxproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
31900F4E1D0EF73700F8673F /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 31900F4C1D0EF73700F8673F /* LaunchScreen.storyboard */; };
1515
31900F591D0EF73700F8673F /* PrimesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31900F581D0EF73700F8673F /* PrimesTests.swift */; };
1616
31900F641D0EF73700F8673F /* PrimesUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31900F631D0EF73700F8673F /* PrimesUITests.swift */; };
17+
31900F761D0EF92E00F8673F /* PrimeGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31900F751D0EF92E00F8673F /* PrimeGenerator.swift */; };
1718
/* End PBXBuildFile section */
1819

1920
/* Begin PBXContainerItemProxy section */
@@ -47,6 +48,7 @@
4748
31900F5F1D0EF73700F8673F /* PrimesUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PrimesUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
4849
31900F631D0EF73700F8673F /* PrimesUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrimesUITests.swift; sourceTree = "<group>"; };
4950
31900F651D0EF73700F8673F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
51+
31900F751D0EF92E00F8673F /* PrimeGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrimeGenerator.swift; sourceTree = "<group>"; };
5052
/* End PBXFileReference section */
5153

5254
/* Begin PBXFrameworksBuildPhase section */
@@ -99,6 +101,7 @@
99101
children = (
100102
31900F431D0EF73700F8673F /* AppDelegate.swift */,
101103
31900F451D0EF73700F8673F /* ViewController.swift */,
104+
31900F751D0EF92E00F8673F /* PrimeGenerator.swift */,
102105
31900F471D0EF73700F8673F /* Main.storyboard */,
103106
31900F4A1D0EF73700F8673F /* Assets.xcassets */,
104107
31900F4C1D0EF73700F8673F /* LaunchScreen.storyboard */,
@@ -256,6 +259,7 @@
256259
isa = PBXSourcesBuildPhase;
257260
buildActionMask = 2147483647;
258261
files = (
262+
31900F761D0EF92E00F8673F /* PrimeGenerator.swift in Sources */,
259263
31900F461D0EF73700F8673F /* ViewController.swift in Sources */,
260264
31900F441D0EF73700F8673F /* AppDelegate.swift in Sources */,
261265
);
@@ -484,6 +488,7 @@
484488
31900F6A1D0EF73700F8673F /* Release */,
485489
);
486490
defaultConfigurationIsVisible = 0;
491+
defaultConfigurationName = Release;
487492
};
488493
31900F6B1D0EF73700F8673F /* Build configuration list for PBXNativeTarget "PrimesTests" */ = {
489494
isa = XCConfigurationList;
@@ -492,6 +497,7 @@
492497
31900F6D1D0EF73700F8673F /* Release */,
493498
);
494499
defaultConfigurationIsVisible = 0;
500+
defaultConfigurationName = Release;
495501
};
496502
31900F6E1D0EF73700F8673F /* Build configuration list for PBXNativeTarget "PrimesUITests" */ = {
497503
isa = XCConfigurationList;
@@ -500,6 +506,7 @@
500506
31900F701D0EF73700F8673F /* Release */,
501507
);
502508
defaultConfigurationIsVisible = 0;
509+
defaultConfigurationName = Release;
503510
};
504511
/* End XCConfigurationList section */
505512
};

Prime Numbers/Primes/Primes/AppDelegate.swift

Lines changed: 3 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -8,128 +8,34 @@
88

99
import UIKit
1010

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](left.0.stride(through: right, by: left.1))
24-
}
25-
2611
@UIApplicationMain
2712
class AppDelegate: UIResponder, UIApplicationDelegate {
2813

2914
var window: UIWindow?
3015

31-
3216
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
3317
// Override point for customization after application launch.
3418
performPrimesGeneration()
3519
return true
3620
}
3721

38-
func applicationWillResignActive(application: UIApplication) {
39-
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
40-
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
41-
}
42-
43-
func applicationDidEnterBackground(application: UIApplication) {
44-
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
45-
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
46-
}
47-
48-
func applicationWillEnterForeground(application: UIApplication) {
49-
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
50-
}
51-
52-
func applicationDidBecomeActive(application: UIApplication) {
53-
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
54-
}
55-
56-
func applicationWillTerminate(application: UIApplication) {
57-
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
58-
}
59-
6022
func performPrimesGeneration() {
6123

6224
let primesTo = 1_000_000
6325

64-
func eratosthenes_sieve(max: Int) -> [Int] {
65-
assert(max > 1, "Prime numbers can only be above 1")
66-
print("Getting all primes under \(max)")
67-
let m = Int(sqrt(ceil(Double(max))))
68-
let set = NSMutableSet(array: 3..2..max)
69-
set.addObject(2)
70-
for i in (2..1..m) {
71-
if (set.containsObject(i)) {
72-
for j in i^^2..i..max {
73-
set.removeObject(j)
74-
}
75-
}
76-
}
77-
return set.sortedArrayUsingDescriptors([NSSortDescriptor(key: "integerValue", ascending: true)]) as! [Int]
78-
}
26+
let primeGenerator = PrimeGenerator.sharedInstance
7927

8028
var startDate = NSDate().timeIntervalSince1970 * 1000
81-
// print(eratosthenes_sieve(5000))
82-
let era_sieve = eratosthenes_sieve(primesTo)
29+
let era_sieve = primeGenerator.eratosthenesPrimes(primesTo)
8330
var endDate = NSDate().timeIntervalSince1970 * 1000
8431
print("Prime generation time for sieve of eratosthenes: \(endDate - startDate) ms.")
8532

86-
func atkins_sieve(max: Int) -> [Int] {
87-
var is_prime = [Bool](count: max + 1, repeatedValue: false)
88-
is_prime[2] = true
89-
is_prime[3] = true
90-
let limit = Int(ceil(sqrt(Double(max))))
91-
for x in 1...limit {
92-
for y in 1...limit {
93-
var num = 4 * x * x + y * y
94-
if (num <= max && (num % 12 == 1 || num % 12 == 5)) {
95-
is_prime[num] = true
96-
}
97-
num = 3 * x * x + y * y
98-
if (num <= max && num % 12 == 7) {
99-
is_prime[num] = true
100-
}
101-
if (x > y) {
102-
num = 3 * x * x - y * y
103-
if (num <= max && num % 12 == 11) {
104-
is_prime[num] = true
105-
}
106-
}
107-
}
108-
}
109-
if limit > 5 {
110-
for i in 5...limit {
111-
if is_prime[i] {
112-
for j in (i * i)..i..max {
113-
is_prime[j] = false
114-
}
115-
}
116-
}
117-
}
118-
var primesArray = [Int]()
119-
for (idx, val) in is_prime.enumerate() {
120-
if val == true { primesArray.append(idx) }
121-
}
122-
return primesArray
123-
}
124-
12533
startDate = NSDate().timeIntervalSince1970 * 1000
126-
// print(atkins_sieve(5000))
127-
let at_sieve = atkins_sieve(primesTo)
34+
let at_sieve = primeGenerator.atkinsPrimes(primesTo)
12835
endDate = NSDate().timeIntervalSince1970 * 1000
12936
print("Prime generation time for atkins sieve: \(endDate - startDate) ms.")
13037
print("they are equal \(era_sieve == at_sieve)")
13138
}
13239

133-
13440
}
13541

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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](left.0.stride(through: right, by: left.1))
24+
}
25+
26+
class PrimeGenerator {
27+
28+
static let sharedInstance = PrimeGenerator()
29+
30+
func eratosthenesPrimes(max: Int) -> [Int] {
31+
let m = Int(sqrt(ceil(Double(max))))
32+
let set = NSMutableSet(array: 3..2..max)
33+
set.addObject(2)
34+
for i in (2..1..m) {
35+
if (set.containsObject(i)) {
36+
for j in i^^2..i..max {
37+
set.removeObject(j)
38+
}
39+
}
40+
}
41+
return set.sortedArrayUsingDescriptors([NSSortDescriptor(key: "integerValue", ascending: true)]) as! [Int]
42+
}
43+
44+
func atkinsPrimes(max: Int) -> [Int] {
45+
var is_prime = [Bool](count: max + 1, repeatedValue: false)
46+
is_prime[2] = true
47+
is_prime[3] = true
48+
let limit = Int(ceil(sqrt(Double(max))))
49+
for x in 1...limit {
50+
for y in 1...limit {
51+
var num = 4 * x * x + y * y
52+
if (num <= max && (num % 12 == 1 || num % 12 == 5)) {
53+
is_prime[num] = true
54+
}
55+
num = 3 * x * x + y * y
56+
if (num <= max && num % 12 == 7) {
57+
is_prime[num] = true
58+
}
59+
if (x > y) {
60+
num = 3 * x * x - y * y
61+
if (num <= max && num % 12 == 11) {
62+
is_prime[num] = true
63+
}
64+
}
65+
}
66+
}
67+
if limit > 5 {
68+
for i in 5...limit {
69+
if is_prime[i] {
70+
for j in (i * i)..i..max {
71+
is_prime[j] = false
72+
}
73+
}
74+
}
75+
}
76+
var primesArray = [Int]()
77+
for (idx, val) in is_prime.enumerate() {
78+
if val == true { primesArray.append(idx) }
79+
}
80+
return primesArray
81+
}
82+
83+
}

0 commit comments

Comments
 (0)