Skip to content

Instantly share code, notes, and snippets.

@superlopuh
Last active August 29, 2015 14:06
Show Gist options
  • Save superlopuh/5f172501651e8d6c4af0 to your computer and use it in GitHub Desktop.
Save superlopuh/5f172501651e8d6c4af0 to your computer and use it in GitHub Desktop.
import Cocoa
func smallestPrimeFactorOf(integer: Int, var withMinGuess minGuess: Int = 2) -> Int? {
let upperBound = Int(sqrt(Double(integer)))
while minGuess <= upperBound {
if integer % minGuess == 0 {
return minGuess
}
minGuess++
}
return nil
}
func lpf(integer: Int, minGuess: Int = 2) -> Int {
if let spf = smallestPrimeFactorOf(integer, withMinGuess: minGuess) {
return lpf(integer/spf, minGuess: spf)
}
return integer
}
print(lpf(600851475143))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment