Sha256: a360263da30399c5a94d1527994e38254166195b4d8a0a0912e7de79ad23ad85

Contents?: true

Size: 666 Bytes

Versions: 71

Compression:

Stored size: 666 Bytes

Contents

import Darwin

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)))) {
            if number % i == 0 {
                return false
            }
        }

        return true
    }
}

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
trackler-2.0.6.15 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.14 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.13 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.12 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.11 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.10 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.9 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.8 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.7 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.6 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.5 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.4 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.3 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.2 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.1 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.6.0 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.5.18 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.5.17 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.5.16 tracks/swift/exercises/nth-prime/NthPrimeExample.swift
trackler-2.0.5.15 tracks/swift/exercises/nth-prime/NthPrimeExample.swift