Skip to content

Commit 2dfcf10

Browse files
committed
Update and refactor with completion blocks and show results in textviews
1 parent 87ec25c commit 2dfcf10

File tree

4 files changed

+73
-32
lines changed

4 files changed

+73
-32
lines changed

Prime Numbers/Primes/Primes/AppDelegate.swift

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1313

1414
var window: UIWindow?
1515

16+
1617
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
1718
// Override point for customization after application launch.
18-
performPrimesGeneration()
1919
return true
2020
}
21-
22-
func performPrimesGeneration() {
23-
24-
let primesTo = 1_000_000
25-
26-
let primeGenerator = PrimeGenerator.sharedInstance
27-
28-
var startDate = Date()
29-
let era_sieve = primeGenerator.eratosthenesPrimes(primesTo)
30-
var endDate = Date()
31-
print("Prime generation time for sieve of eratosthenes: \(endDate.timeIntervalSince(startDate) * 1000) ms.")
32-
33-
startDate = Date()
34-
let at_sieve = primeGenerator.atkinsPrimes(primesTo)
35-
endDate = Date()
36-
print("Prime generation time for atkins sieve: \(endDate.timeIntervalSince(startDate) * 1000) ms.")
37-
print("they are equal \(era_sieve == at_sieve)")
38-
}
39-
4021
}
4122

Prime Numbers/Primes/Primes/Base.lproj/Main.storyboard

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,52 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11129.15" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
4+
<deployment identifier="iOS"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11103.10"/>
6+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
57
</dependencies>
68
<scenes>
79
<!--View Controller-->
810
<scene sceneID="tne-QT-ifu">
911
<objects>
10-
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
12+
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="Primes" customModuleProvider="target" sceneMemberID="viewController">
1113
<layoutGuides>
1214
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
1315
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
1416
</layoutGuides>
1517
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
16-
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
18+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1719
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
18-
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
20+
<subviews>
21+
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="bQv-Df-Olo">
22+
<subviews>
23+
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" text="Getting primes using the sieve of Eratosthenes" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="4V8-CM-o6m">
24+
<color key="backgroundColor" red="0.0" green="0.47843137250000001" blue="0.69411764710000001" alpha="1" colorSpace="calibratedRGB"/>
25+
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
26+
<fontDescription key="fontDescription" type="system" pointSize="14"/>
27+
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
28+
</textView>
29+
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" text="Getting primes using Atkin's sieve" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="FSt-V7-5y0">
30+
<color key="backgroundColor" red="0.25098040700000002" green="0.50196081400000003" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
31+
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
32+
<fontDescription key="fontDescription" type="system" pointSize="14"/>
33+
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
34+
</textView>
35+
</subviews>
36+
</stackView>
37+
</subviews>
38+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
39+
<constraints>
40+
<constraint firstItem="bQv-Df-Olo" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="20" id="7aR-Fu-fA4"/>
41+
<constraint firstItem="bQv-Df-Olo" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" constant="20" id="Rzv-Qz-6y8"/>
42+
<constraint firstAttribute="trailing" secondItem="bQv-Df-Olo" secondAttribute="trailing" constant="20" id="mrQ-nB-gHB"/>
43+
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="bQv-Df-Olo" secondAttribute="bottom" constant="20" id="yf4-4j-aVO"/>
44+
</constraints>
1945
</view>
46+
<connections>
47+
<outlet property="atSieveTextView" destination="FSt-V7-5y0" id="WbM-xZ-bks"/>
48+
<outlet property="eraSieveTextView" destination="4V8-CM-o6m" id="nja-8n-alY"/>
49+
</connections>
2050
</viewController>
2151
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
2252
</objects>

Prime Numbers/Primes/Primes/PrimeGenerator.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ class PrimeGenerator {
2727

2828
static let sharedInstance = PrimeGenerator()
2929

30-
func eratosthenesPrimes(_ max: Int) -> [Int] {
30+
31+
32+
33+
func eratosthenesPrimes(_ max: Int, completion:([Int]) -> ()){
3134
let m = Int(sqrt(ceil(Double(max))))
3235
let set = NSMutableSet(array: 3..2..max)
3336
set.add(2)
@@ -38,10 +41,10 @@ class PrimeGenerator {
3841
}
3942
}
4043
}
41-
return set.sortedArray(using: [SortDescriptor(key: "integerValue", ascending: true)]) as! [Int]
44+
completion(set.sortedArray(using: [SortDescriptor(key: "integerValue", ascending: true)]) as! [Int])
4245
}
4346

44-
func atkinsPrimes(_ max: Int) -> [Int] {
47+
func atkinsPrimes(_ max: Int, completion:([Int]) -> ()) {
4548
var is_prime = [Bool](repeating: false, count: max + 1)
4649
is_prime[2] = true
4750
is_prime[3] = true
@@ -77,7 +80,7 @@ class PrimeGenerator {
7780
for (idx, val) in is_prime.enumerated() {
7881
if val == true { primesArray.append(idx) }
7982
}
80-
return primesArray
83+
completion(primesArray)
8184
}
8285

8386
}

Prime Numbers/Primes/Primes/ViewController.swift

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,43 @@ import UIKit
1010

1111
class ViewController: UIViewController {
1212

13+
@IBOutlet weak var eraSieveTextView: UITextView!
14+
@IBOutlet weak var atSieveTextView: UITextView!
15+
1316
override func viewDidLoad() {
1417
super.viewDidLoad()
15-
// Do any additional setup after loading the view, typically from a nib.
18+
performPrimesGeneration()
1619
}
1720

1821
override func didReceiveMemoryWarning() {
1922
super.didReceiveMemoryWarning()
2023
// Dispose of any resources that can be recreated.
2124
}
22-
23-
25+
26+
func performPrimesGeneration() {
27+
28+
let primesTo = 1_000
29+
30+
let primeGenerator = PrimeGenerator.sharedInstance
31+
32+
var startDate = Date()
33+
var endDate = Date()
34+
var era_sieve = [Int]()
35+
primeGenerator.eratosthenesPrimes(primesTo) { (primesArray) in
36+
era_sieve = primesArray
37+
endDate = Date()
38+
print("Found \(era_sieve.count) primes in : \(endDate.timeIntervalSince(startDate) * 1000) ms.")
39+
self.eraSieveTextView.text = era_sieve.description
40+
}
41+
42+
startDate = Date()
43+
var at_sieve = [Int]()
44+
primeGenerator.atkinsPrimes(primesTo) { (primesArray) in
45+
at_sieve = primesArray
46+
endDate = Date()
47+
print("Found \(at_sieve.count) primes in : \(endDate.timeIntervalSince(startDate) * 1000) ms.")
48+
self.atSieveTextView.text = at_sieve.description
49+
}
50+
}
2451
}
2552

0 commit comments

Comments
 (0)