Prime triangle: Difference between revisions

m
optimize a bit more
m (optimized slightly)
m (optimize a bit more)
Line 46:
function primetriangle(nrows::Integer)
nrows < 2 && error("number of rows requested must be > 1")
pmask, spinlock = primesmask(2 * (nrows + 1)), Threads.SpinLock()
counts, rowstrings = [1; zeros(Int, nrows - 1)], ["" for _ in 1:nrows]
@Threads.threads for r in shuffle(2:nrows)
@Threads.threads for e in collect(permutations(2:2:r))
p = zeros(Int, r - 1)
for e in permutations(2:2:r), op in= permutationszeros(3:2:Int, r - 1)
ifor =o 0in permutations(3:2:r)
for (x, y) in zip(e,i o)= 0
p[ifor +=(x, 1]y) =in xzip(e, o)
p[i += 1] = yx
end p[i += 1] = y
length(e) > length(o) && (p[i += 1] = e[end])
if pmask[p[i] + r +length(e) 1]> length(o) && pmask[(p[begin]i += 1] &&= all(j -> pmaske[p[jend] + p[j + 1]], 1:r-2)
if countspmask[p[i] + r + 1] ==&& 0pmask[p[begin] + 1] && all(j -> pmask[p[j] + p[j + 1]], 1:r-2)
rowstrings[r] = " 1" * prodlock([lpad(n, 3) for n in p]) * lpad(r + 1, 3spinlock) * "\n"
if counts[r] +== 10
rowstrings[r] = " 1" * prod([lpad(n, 3) for n in p]) * lpad(r + 1, 3) * "\n"
end
counts[r] += 1
unlock(spinlock)
end
counts[r] += 1
end
end
Line 89 ⟶ 93:
 
[1, 1, 1, 1, 1, 2, 4, 7, 24, 80, 216, 648, 1304, 3392, 13808, 59448]
62 36.956955933227 seconds (699.4910 M allocations: 55.571557 GiB, 2246.2571% gc time, 0.6737% compilation time)
</pre>
 
4,102

edits