Project Euler #3 (Largest prime factor) in Swift
Naming

Your naming is misleading, and effectively broken:


fromNumber is a bad name for that variable. Something like toFactor would be better.
largestPossibleFactor is very broken. The function does not return the largestPossibleFactor, it instead returns an approximate square value. Take the number 36, the largest possible factor (other than 36 itself), is 18, yet your function returns 6. What you should call the function is something like .... actually, I don't think it needs to be a function at all.... it's over-extracted, calling a 1-liner function who's code is easier to write than describe. It's buggy code, anyway....


Bug

Your range limit is the 'half-open' range limit: 2..&lt;Int(ceil(squareRoot))

The intention of this code is to identify where to stop the factor checking. This code will fail for values like ... 9. For 9 your code will not factor it at all, because it is a square of a prime, so your squareRoot will calculate the valeu 3, and you will do the ceil of that (3), and then exclude 3 in the range, so you will not factor the value 9 at all, and call it prime.

Instead, you should simply use the full range operator, and truncate the square-root:

for i in 2...Int(squareRoot)


Performance

A small optimization is possible if you are interested, by limiting the start of the range of the factor tests. Consider converting the recursive part of your function to include the start of the range as well as the number to factor. Something like:

func findLargestPrimeFactorFrom(let smallest: Int, let toFactor: Int) -&gt; Int {
    for factor in smallest...Int(sqrt(Double(toFactor))) {
        if toFactor.isMultipleOf(factor) {
            return findLargestPrimeFactor(factor, toFactor/factor)
        }
    }
    return toFactor
}


The reason it will be faster is because in your recursion, you will never need to check values smaller than factors you have already eliminated higher up the stack.