Last active
May 9, 2018 06:40
-
-
Save ashalva/031cfce76d304d65821b57eef21a5d77 to your computer and use it in GitHub Desktop.
Finding all prime factors for the number in swift.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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