Skip to content

Instantly share code, notes, and snippets.

@willie
Forked from mattt/Sieve.swift
Last active October 15, 2018 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willie/c589832bc009350578acdbfda4dbcd2c to your computer and use it in GitHub Desktop.
Save willie/c589832bc009350578acdbfda4dbcd2c to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes with Accelerate
// Calculate prime numbers in a given range
// using Sieve of Eratosthenes
import Accelerate
var primes: [Int] = []
let range = 0...1000
var numbers = range.map(Float.init)
for n in range {
guard numbers[n] > 1 else { continue }
defer { primes.append(n) }
// Clear vector elements with given stride
// (i.e. set value to 0)
vDSP_vclr(&numbers[n],
numericCast(n),
numericCast((numbers.count - 1) / n))
}
print(primes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment