Julia Language Find smallest prime factor


Example

In some situations, one might want to return from a function before finishing an entire loop. The return statement can be used for this.

function primefactor(n)
    for i in 2:n
        if n % i == 0
            return i
        end
    end
    @assert false  # unreachable
end

Usage:

julia> primefactor(100)
2

julia> primefactor(97)
97

Loops can also be terminated early with the break statement, which terminates just the enclosing loop instead of the entire function.