In case you're interested, here's an alternative solution based on the Sieve of Eratosthenes:
UPPERLIMIT = 100
#=
Create an array of booleans called "is_prime" which is initially true
for all odd numbers, false for all even numbers
=#
is_prime = [(i % 2) > 0 for i in 1:UPPERLIMIT]
is_prime[2] = true # 2 is the exception, an even prime
#=
Now iterate through the array up to the square root of UPPERLIMIT.
factors which are bigger than sqrt(UPPERLIMIT) must have co-factors
that are smaller, so there's no need to search above that limit.
=#
for num in 3:2:floor(Int,sqrt(UPPERLIMIT))
#=
Every time we find a prime, wipe out all of its multiples.
We can start at 3*num since 2*num is already masked out
because it's even. Adding odd multiples would also yield
even outcomes, so they can be stepped over as well.
=#
if is_prime[num]
for i in 3*num:2*num:UPPERLIMIT
is_prime[i] = false
end
end
end
# Everything that hasn't been masked out to false is now prime!
for i in 2:UPPERLIMIT
if is_prime[i]
println(i)
end
end
Note that this is almost the same number of lines of actual code (after stripping out comments) as the brute force approach. Both produce identical results, but this version is substantially faster - 2.1s vs 67s to generate the 664579 primes up to 10_000_000 on my laptop.
ADDENDUM
I'm new to Julia myself, and have been learning as I played with this. That led to the version below, where I replaced conditional loops with && and foreach. I've removed the comments to highlight how little code is required:
UPPERLIMIT = 100
is_prime = [(i % 2) > 0 for i in 1:UPPERLIMIT]
is_prime[2] = true # 2 is the exception, an even prime
for num in 3:2:floor(Int,sqrt(UPPERLIMIT))
is_prime[num] && foreach(i -> is_prime[i] = false, num*num:2*num:UPPERLIMIT)
end
foreach(i -> is_prime[i] && println(i), 2:UPPERLIMIT)
This variant not only involves less code, but it runs even faster—1.41s vs 2.1s to generate primes up to 10M.