Skip to content

Instantly share code, notes, and snippets.

@ashalva
Last active May 9, 2018 06:40
Show Gist options
  • Save ashalva/031cfce76d304d65821b57eef21a5d77 to your computer and use it in GitHub Desktop.
Save ashalva/031cfce76d304d65821b57eef21a5d77 to your computer and use it in GitHub Desktop.
Finding all prime factors for the number in swift.
import Foundation
func allPrimeFactors(number: Int) {
var n = number
while n % 2 == 0 {
print("2")
n /= 2
}
var i = 3
while i <= Int(sqrt(Double(n))) {
while n % i == 0 {
print("\(i)")
n /= i
}
i += 2
}
if n > 2 {
print("\(n)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment