Last active
August 29, 2015 14:06
-
-
Save superlopuh/5f172501651e8d6c4af0 to your computer and use it in GitHub Desktop.
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 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