├── .gitignore └── .gitignore └── Prime ├── Prime.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist └── Prime └── main.swift /.gitignore/.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | # Package.resolved 41 | .build/ 42 | 43 | # CocoaPods 44 | # 45 | # We recommend against adding the Pods directory to your .gitignore. However 46 | # you should judge for yourself, the pros and cons are mentioned at: 47 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 48 | # 49 | # Pods/ 50 | 51 | # Carthage 52 | # 53 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 54 | # Carthage/Checkouts 55 | 56 | Carthage/Build 57 | 58 | # fastlane 59 | # 60 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 61 | # screenshots whenever they are needed. 62 | # For more information about the recommended setup visit: 63 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 64 | 65 | fastlane/report.xml 66 | fastlane/Preview.html 67 | fastlane/screenshots/**/*.png 68 | fastlane/test_output 69 | -------------------------------------------------------------------------------- /Prime/Prime.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 55F77C2522DAF28A005F6861 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55F77C2422DAF28A005F6861 /* main.swift */; }; 11 | /* End PBXBuildFile section */ 12 | 13 | /* Begin PBXCopyFilesBuildPhase section */ 14 | 55F77C1F22DAF28A005F6861 /* CopyFiles */ = { 15 | isa = PBXCopyFilesBuildPhase; 16 | buildActionMask = 2147483647; 17 | dstPath = /usr/share/man/man1/; 18 | dstSubfolderSpec = 0; 19 | files = ( 20 | ); 21 | runOnlyForDeploymentPostprocessing = 1; 22 | }; 23 | /* End PBXCopyFilesBuildPhase section */ 24 | 25 | /* Begin PBXFileReference section */ 26 | 55F77C2122DAF28A005F6861 /* Prime */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Prime; sourceTree = BUILT_PRODUCTS_DIR; }; 27 | 55F77C2422DAF28A005F6861 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 28 | /* End PBXFileReference section */ 29 | 30 | /* Begin PBXFrameworksBuildPhase section */ 31 | 55F77C1E22DAF28A005F6861 /* Frameworks */ = { 32 | isa = PBXFrameworksBuildPhase; 33 | buildActionMask = 2147483647; 34 | files = ( 35 | ); 36 | runOnlyForDeploymentPostprocessing = 0; 37 | }; 38 | /* End PBXFrameworksBuildPhase section */ 39 | 40 | /* Begin PBXGroup section */ 41 | 55F77C1822DAF28A005F6861 = { 42 | isa = PBXGroup; 43 | children = ( 44 | 55F77C2322DAF28A005F6861 /* Prime */, 45 | 55F77C2222DAF28A005F6861 /* Products */, 46 | ); 47 | sourceTree = ""; 48 | }; 49 | 55F77C2222DAF28A005F6861 /* Products */ = { 50 | isa = PBXGroup; 51 | children = ( 52 | 55F77C2122DAF28A005F6861 /* Prime */, 53 | ); 54 | name = Products; 55 | sourceTree = ""; 56 | }; 57 | 55F77C2322DAF28A005F6861 /* Prime */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | 55F77C2422DAF28A005F6861 /* main.swift */, 61 | ); 62 | path = Prime; 63 | sourceTree = ""; 64 | }; 65 | /* End PBXGroup section */ 66 | 67 | /* Begin PBXNativeTarget section */ 68 | 55F77C2022DAF28A005F6861 /* Prime */ = { 69 | isa = PBXNativeTarget; 70 | buildConfigurationList = 55F77C2822DAF28A005F6861 /* Build configuration list for PBXNativeTarget "Prime" */; 71 | buildPhases = ( 72 | 55F77C1D22DAF28A005F6861 /* Sources */, 73 | 55F77C1E22DAF28A005F6861 /* Frameworks */, 74 | 55F77C1F22DAF28A005F6861 /* CopyFiles */, 75 | ); 76 | buildRules = ( 77 | ); 78 | dependencies = ( 79 | ); 80 | name = Prime; 81 | productName = Prime; 82 | productReference = 55F77C2122DAF28A005F6861 /* Prime */; 83 | productType = "com.apple.product-type.tool"; 84 | }; 85 | /* End PBXNativeTarget section */ 86 | 87 | /* Begin PBXProject section */ 88 | 55F77C1922DAF28A005F6861 /* Project object */ = { 89 | isa = PBXProject; 90 | attributes = { 91 | LastSwiftUpdateCheck = 1020; 92 | LastUpgradeCheck = 1020; 93 | ORGANIZATIONNAME = bearkode; 94 | TargetAttributes = { 95 | 55F77C2022DAF28A005F6861 = { 96 | CreatedOnToolsVersion = 10.2.1; 97 | }; 98 | }; 99 | }; 100 | buildConfigurationList = 55F77C1C22DAF28A005F6861 /* Build configuration list for PBXProject "Prime" */; 101 | compatibilityVersion = "Xcode 9.3"; 102 | developmentRegion = en; 103 | hasScannedForEncodings = 0; 104 | knownRegions = ( 105 | en, 106 | ); 107 | mainGroup = 55F77C1822DAF28A005F6861; 108 | productRefGroup = 55F77C2222DAF28A005F6861 /* Products */; 109 | projectDirPath = ""; 110 | projectRoot = ""; 111 | targets = ( 112 | 55F77C2022DAF28A005F6861 /* Prime */, 113 | ); 114 | }; 115 | /* End PBXProject section */ 116 | 117 | /* Begin PBXSourcesBuildPhase section */ 118 | 55F77C1D22DAF28A005F6861 /* Sources */ = { 119 | isa = PBXSourcesBuildPhase; 120 | buildActionMask = 2147483647; 121 | files = ( 122 | 55F77C2522DAF28A005F6861 /* main.swift in Sources */, 123 | ); 124 | runOnlyForDeploymentPostprocessing = 0; 125 | }; 126 | /* End PBXSourcesBuildPhase section */ 127 | 128 | /* Begin XCBuildConfiguration section */ 129 | 55F77C2622DAF28A005F6861 /* Debug */ = { 130 | isa = XCBuildConfiguration; 131 | buildSettings = { 132 | ALWAYS_SEARCH_USER_PATHS = NO; 133 | CLANG_ANALYZER_NONNULL = YES; 134 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 135 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 136 | CLANG_CXX_LIBRARY = "libc++"; 137 | CLANG_ENABLE_MODULES = YES; 138 | CLANG_ENABLE_OBJC_ARC = YES; 139 | CLANG_ENABLE_OBJC_WEAK = YES; 140 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 141 | CLANG_WARN_BOOL_CONVERSION = YES; 142 | CLANG_WARN_COMMA = YES; 143 | CLANG_WARN_CONSTANT_CONVERSION = YES; 144 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 145 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 146 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 147 | CLANG_WARN_EMPTY_BODY = YES; 148 | CLANG_WARN_ENUM_CONVERSION = YES; 149 | CLANG_WARN_INFINITE_RECURSION = YES; 150 | CLANG_WARN_INT_CONVERSION = YES; 151 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 152 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 153 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 154 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 155 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 156 | CLANG_WARN_STRICT_PROTOTYPES = YES; 157 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 158 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 159 | CLANG_WARN_UNREACHABLE_CODE = YES; 160 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 161 | CODE_SIGN_IDENTITY = "-"; 162 | COPY_PHASE_STRIP = NO; 163 | DEBUG_INFORMATION_FORMAT = dwarf; 164 | ENABLE_STRICT_OBJC_MSGSEND = YES; 165 | ENABLE_TESTABILITY = YES; 166 | GCC_C_LANGUAGE_STANDARD = gnu11; 167 | GCC_DYNAMIC_NO_PIC = NO; 168 | GCC_NO_COMMON_BLOCKS = YES; 169 | GCC_OPTIMIZATION_LEVEL = 0; 170 | GCC_PREPROCESSOR_DEFINITIONS = ( 171 | "DEBUG=1", 172 | "$(inherited)", 173 | ); 174 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 175 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 176 | GCC_WARN_UNDECLARED_SELECTOR = YES; 177 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 178 | GCC_WARN_UNUSED_FUNCTION = YES; 179 | GCC_WARN_UNUSED_VARIABLE = YES; 180 | MACOSX_DEPLOYMENT_TARGET = 10.14; 181 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 182 | MTL_FAST_MATH = YES; 183 | ONLY_ACTIVE_ARCH = YES; 184 | SDKROOT = macosx; 185 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 186 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 187 | }; 188 | name = Debug; 189 | }; 190 | 55F77C2722DAF28A005F6861 /* Release */ = { 191 | isa = XCBuildConfiguration; 192 | buildSettings = { 193 | ALWAYS_SEARCH_USER_PATHS = NO; 194 | CLANG_ANALYZER_NONNULL = YES; 195 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 196 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 197 | CLANG_CXX_LIBRARY = "libc++"; 198 | CLANG_ENABLE_MODULES = YES; 199 | CLANG_ENABLE_OBJC_ARC = YES; 200 | CLANG_ENABLE_OBJC_WEAK = YES; 201 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 202 | CLANG_WARN_BOOL_CONVERSION = YES; 203 | CLANG_WARN_COMMA = YES; 204 | CLANG_WARN_CONSTANT_CONVERSION = YES; 205 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 206 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 207 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 208 | CLANG_WARN_EMPTY_BODY = YES; 209 | CLANG_WARN_ENUM_CONVERSION = YES; 210 | CLANG_WARN_INFINITE_RECURSION = YES; 211 | CLANG_WARN_INT_CONVERSION = YES; 212 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 213 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 214 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 215 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 216 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 217 | CLANG_WARN_STRICT_PROTOTYPES = YES; 218 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 219 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 220 | CLANG_WARN_UNREACHABLE_CODE = YES; 221 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 222 | CODE_SIGN_IDENTITY = "-"; 223 | COPY_PHASE_STRIP = NO; 224 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 225 | ENABLE_NS_ASSERTIONS = NO; 226 | ENABLE_STRICT_OBJC_MSGSEND = YES; 227 | GCC_C_LANGUAGE_STANDARD = gnu11; 228 | GCC_NO_COMMON_BLOCKS = YES; 229 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 230 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 231 | GCC_WARN_UNDECLARED_SELECTOR = YES; 232 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 233 | GCC_WARN_UNUSED_FUNCTION = YES; 234 | GCC_WARN_UNUSED_VARIABLE = YES; 235 | MACOSX_DEPLOYMENT_TARGET = 10.14; 236 | MTL_ENABLE_DEBUG_INFO = NO; 237 | MTL_FAST_MATH = YES; 238 | SDKROOT = macosx; 239 | SWIFT_COMPILATION_MODE = wholemodule; 240 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 241 | }; 242 | name = Release; 243 | }; 244 | 55F77C2922DAF28A005F6861 /* Debug */ = { 245 | isa = XCBuildConfiguration; 246 | buildSettings = { 247 | CODE_SIGN_STYLE = Automatic; 248 | PRODUCT_NAME = "$(TARGET_NAME)"; 249 | SWIFT_VERSION = 5.0; 250 | }; 251 | name = Debug; 252 | }; 253 | 55F77C2A22DAF28A005F6861 /* Release */ = { 254 | isa = XCBuildConfiguration; 255 | buildSettings = { 256 | CODE_SIGN_STYLE = Automatic; 257 | PRODUCT_NAME = "$(TARGET_NAME)"; 258 | SWIFT_VERSION = 5.0; 259 | }; 260 | name = Release; 261 | }; 262 | /* End XCBuildConfiguration section */ 263 | 264 | /* Begin XCConfigurationList section */ 265 | 55F77C1C22DAF28A005F6861 /* Build configuration list for PBXProject "Prime" */ = { 266 | isa = XCConfigurationList; 267 | buildConfigurations = ( 268 | 55F77C2622DAF28A005F6861 /* Debug */, 269 | 55F77C2722DAF28A005F6861 /* Release */, 270 | ); 271 | defaultConfigurationIsVisible = 0; 272 | defaultConfigurationName = Release; 273 | }; 274 | 55F77C2822DAF28A005F6861 /* Build configuration list for PBXNativeTarget "Prime" */ = { 275 | isa = XCConfigurationList; 276 | buildConfigurations = ( 277 | 55F77C2922DAF28A005F6861 /* Debug */, 278 | 55F77C2A22DAF28A005F6861 /* Release */, 279 | ); 280 | defaultConfigurationIsVisible = 0; 281 | defaultConfigurationName = Release; 282 | }; 283 | /* End XCConfigurationList section */ 284 | }; 285 | rootObject = 55F77C1922DAF28A005F6861 /* Project object */; 286 | } 287 | -------------------------------------------------------------------------------- /Prime/Prime.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Prime/Prime.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Prime/Prime/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Prime 4 | // 5 | // Created by bearkode on 14/07/2019. 6 | // Copyright © 2019 bearkode. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | 12 | class PrimeFinder { 13 | 14 | init(max: Int) { 15 | self.max = max 16 | } 17 | 18 | func find() -> [Int] { 19 | self.prepare() 20 | self.checkOddNumbers() 21 | 22 | return self.primes 23 | } 24 | 25 | // MARK: - private 26 | 27 | private let max: Int 28 | private var primes: [Int] = [] 29 | private var multiples: [Int] = [] 30 | 31 | private func prepare() { 32 | self.primes = [2] 33 | self.multiples = [2] 34 | } 35 | 36 | private func checkOddNumbers() { 37 | var candidate = 3 38 | 39 | while self.primes.count < self.max { 40 | if self.isPrime(candidate) { 41 | self.primes.append(candidate) 42 | } 43 | candidate += 2 44 | } 45 | } 46 | 47 | private func isPrime(_ candidate: Int) -> Bool { 48 | if self.isLeastRelevantMultipleOfNextLargerPrimeFactor(candidate: candidate) { 49 | self.multiples.append(candidate) 50 | return false 51 | } 52 | 53 | return self.isNotMultipleOfAnyPreviousPrimeFactors(candidate: candidate) 54 | } 55 | 56 | private func isNotMultipleOfAnyPreviousPrimeFactors(candidate: Int) -> Bool { 57 | for n in (0.. Bool { 67 | let nextLargerPrimeFactor = self.nextLargerPrimeFactor 68 | let leastRelevantMultiple = nextLargerPrimeFactor * nextLargerPrimeFactor 69 | return candidate == leastRelevantMultiple 70 | } 71 | 72 | private var nextLargerPrimeFactor: Int { 73 | guard self.primes.count > self.multiples.count else { 74 | return 0 75 | } 76 | 77 | return self.primes[self.multiples.count] 78 | } 79 | 80 | private func isMultipleOfPrimeFactor(candidate: Int, nth: Int) -> Bool { 81 | return candidate == self.smallestOddMultiple(notLessThan: candidate, nth: nth) 82 | } 83 | 84 | private func smallestOddMultiple(notLessThan candidate: Int, nth n: Int) -> Int { 85 | var multiple = self.multiples[n] 86 | 87 | while multiple < candidate { 88 | multiple += (self.primes[n] * 2) 89 | self.multiples[n] = multiple 90 | } 91 | 92 | return multiple 93 | } 94 | 95 | } 96 | 97 | 98 | class NumberPrinter { 99 | 100 | let rowsPerPage: Int 101 | let columnCount: Int 102 | 103 | init(rowsPerPage: Int = 7, columnCount: Int = 5) { 104 | self.rowsPerPage = rowsPerPage 105 | self.columnCount = columnCount 106 | } 107 | 108 | func printNumbers(_ numbers: [Int]) { 109 | self.numbers = numbers 110 | self.pageNumber = 1 111 | self.pageOffset = 0 112 | 113 | while self.pageOffset < numbers.count { 114 | self.printPage() 115 | self.pageNumber = self.pageNumber + 1 116 | self.pageOffset = self.pageOffset + self.rowsPerPage * self.columnCount 117 | } 118 | } 119 | 120 | private var numbers: [Int] = [] 121 | private var pageNumber: Int = 1 122 | private var pageOffset: Int = 0 123 | 124 | private func printPage() { 125 | self.printHeader() 126 | 127 | for rowOffset in self.pageOffset..<(self.pageOffset + self.rowsPerPage) { 128 | self.printRow(rowOffset: rowOffset) 129 | } 130 | } 131 | 132 | private func printHeader() { 133 | print("The First \(self.numbers.count) Prime Numbers --- page \(self.pageNumber)") 134 | } 135 | 136 | private func printRow(rowOffset: Int) { 137 | for column in 0..