Skip to content

Instantly share code, notes, and snippets.

@BrandonShega
Created June 30, 2017 13:47
Show Gist options
  • Save BrandonShega/88da8e9b664349459a5a244dd40adf1d to your computer and use it in GitHub Desktop.
Save BrandonShega/88da8e9b664349459a5a244dd40adf1d to your computer and use it in GitHub Desktop.
Prime Number Sequence
struct PrimeIterator: IteratorProtocol {
var newValue = 2
var foundPrimes: [Int] = []
mutating func next() -> Int? {
var nextPrime = newValue
while foundPrimes.contains(where: { nextPrime % $0 == 0 }) {
nextPrime += 1
}
newValue = nextPrime
foundPrimes.append(nextPrime)
return nextPrime
}
}
struct PrimeSequence: Sequence {
func makeIterator() -> PrimeIterator {
return PrimeIterator()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment