Sequence of primes by trial division: Difference between revisions

(add PicoLisp)
Line 1,144:
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
I've chosen to solve this task by creating a new iterator type, <tt>TDPrimes</tt>. <tt>TDPrimes</tt> contains the upper limit of the sequence. The iteration state is the list of computed primes, and the item returned with each iteration is the current prime. The core of the solution is the <tt>next</tt> method for <tt>TDPrimes</tt>, which computes the next prime by trial division of the previously determined primes contained in the iteration state.
<lang Julia>
type TDPrimes{T<:Integer}
plim::T
end
 
function<lang Base.startjulia>struct TDPrimes{T<:Integer}(pl::TDPrimes{T})
2ones(uplim::T, 1)
end
 
function Base.donestart{T<:Integer}(pl::TDPrimes{T},) = p::Array{2ones(T, 1})
Base.done{T<:Integer}(pl::TDPrimes{T}, p::Vector{T}) = p[end] > pl.plimuplim
function Base.next{T<:Integer}(pl::TDPrimes{T}, p::ArrayVector{T,1})
end
pr = npr = p[end]
 
ispr = false
function Base.next{T<:Integer}(pl::TDPrimes{T}, p::Array{T,1})
prwhile = p[end]!ispr
for i in (pr npr += 1):(pl.plim)
ispr = trueall(npr % d != 0 for d in p)
for j in p
if i%j == 0
ispr = false
break
end
end
if ispr
push!(p, i)
return (pr, p)
end
end
push!(p, typemax(T)npr)
return (pr, p)
end
 
println("Primes ≤ 100: ", join((p for p in TDPrimes(100)), ", "))</lang>
n = 100
print("The primes <= ", n, " are:\n ")
 
for i in TDPrimes(n)
print(i, " ")
end
println()
</lang>
 
{{out}}
<pre>Primes 100: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 </pre>
<pre>
The primes <= 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
</pre>
 
=={{header|jq}}==
Anonymous user