|
8 | 8 |
|
9 | 9 | import UIKit
|
10 | 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 | 11 | @UIApplicationMain
|
27 | 12 | class AppDelegate: UIResponder, UIApplicationDelegate {
|
28 | 13 |
|
29 | 14 | var window: UIWindow?
|
30 | 15 |
|
31 |
| - |
32 | 16 | func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
|
33 | 17 | // Override point for customization after application launch.
|
34 | 18 | performPrimesGeneration()
|
35 | 19 | return true
|
36 | 20 | }
|
37 | 21 |
|
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 |
| - |
60 | 22 | func performPrimesGeneration() {
|
61 | 23 |
|
62 | 24 | let primesTo = 1_000_000
|
63 | 25 |
|
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 |
79 | 27 |
|
80 | 28 | var startDate = NSDate().timeIntervalSince1970 * 1000
|
81 |
| - // print(eratosthenes_sieve(5000)) |
82 |
| - let era_sieve = eratosthenes_sieve(primesTo) |
| 29 | + let era_sieve = primeGenerator.eratosthenesPrimes(primesTo) |
83 | 30 | var endDate = NSDate().timeIntervalSince1970 * 1000
|
84 | 31 | print("Prime generation time for sieve of eratosthenes: \(endDate - startDate) ms.")
|
85 | 32 |
|
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 |
| - |
125 | 33 | startDate = NSDate().timeIntervalSince1970 * 1000
|
126 |
| - // print(atkins_sieve(5000)) |
127 |
| - let at_sieve = atkins_sieve(primesTo) |
| 34 | + let at_sieve = primeGenerator.atkinsPrimes(primesTo) |
128 | 35 | endDate = NSDate().timeIntervalSince1970 * 1000
|
129 | 36 | print("Prime generation time for atkins sieve: \(endDate - startDate) ms.")
|
130 | 37 | print("they are equal \(era_sieve == at_sieve)")
|
131 | 38 | }
|
132 | 39 |
|
133 |
| - |
134 | 40 | }
|
135 | 41 |
|
0 commit comments