The remainder operator in Julia is the %
operator. This operator behaves similarly to the %
in languages such as C and C++. a % b
is the signed remainder left over after dividing a
by b
.
This operator is very useful for implementing certain algorithms, such as the following implementation of the Sieve of Eratosthenes.
iscoprime(P, i) = !any(x -> i % x == 0, P)
function sieve(n)
P = Int[]
for i in 2:n
if iscoprime(P, i)
push!(P, i)
end
end
P
end
Usage:
julia> sieve(20)
8-element Array{Int64,1}:
2
3
5
7
11
13
17
19