determine if x is the factorial of another number in swift

Here's a function in Swift that determines if a number x is the factorial of another number:

main.swift
func isFactorial(_ x: Int) -> Bool {
    var n = 1
    var factorial = 1
    
    while factorial < x {
        n += 1
        factorial *= n
    }
    
    return factorial == x
}
181 chars
12 lines

The function uses a while loop to continuously multiply consecutive natural numbers until the factorial is greater than or equal to the input x. Then, the function returns true if the resulting factorial is equal to x, otherwise it returns false.

gistlibby LogSnag