Sha256: 7d74d88cbb21d7016bc602985799cd4b8dd8eb3b7d592955f905f94f32b83d58

Contents?: true

Size: 695 Bytes

Versions: 188

Compression:

Stored size: 695 Bytes

Contents

#if os(Linux)
    import Glibc
#elseif os(OSX)
    import Darwin
#endif

struct Prime {
    static func nth(_ nth: Int) -> Int? {
        if nth < 1 {
            return nil
        }

        var primes = 0
        var i = 1

        while primes < nth {
            i += 1
            if isPrime(i) {
                primes += 1
            }
        }

        return i
    }

    private static func isPrime(_ number: Int) -> Bool {
        if number == 1 {
            return false
        } else if number == 2 {
            return true
        }

        for i in 2...Int(ceil(sqrt(Double(number)))) where number % i == 0 {
            return false
        }

        return true
    }
}

Version data entries

188 entries across 188 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.179 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.178 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.177 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.176 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.175 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.174 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.173 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.172 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.171 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.170 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.169 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.167 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.166 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.165 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.164 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.163 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.162 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.161 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift
trackler-2.2.1.160 tracks/swift/exercises/nth-prime/Sources/NthPrimeExample.swift