I’m new to Julia and practicing writing functions…
The function below works great, if I call it from the command line like:
julia> prime(5)
[1, 2, 3, 5]
BUT if I try to set the output of the function to a variable, then it doesn’t work
julia> c = prime(5)
[1, 2, 3, 5]
julia> print(c)
nothing
What gives? See the prime function code below.
function prime(n::Integer)
# Find all prime numbers between 1 and n
num_all = collect(1:n);
is_prime = trues(n)
for i in num_all
d_iv = collect(2:i-1)
for ii in d_iv
if i%ii == 0
is_prime[i] = false
continue
end
end
end
c = num_all[is_prime]
return c
end