Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
31
Task/Prime-triangle/Julia/prime-triangle-1.julia
Normal file
31
Task/Prime-triangle/Julia/prime-triangle-1.julia
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
using Combinatorics, Primes
|
||||
|
||||
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]
|
||||
for r in 2:nrows
|
||||
@Threads.threads for e in collect(permutations(2:2:r))
|
||||
p = zeros(Int, r - 1)
|
||||
for o in permutations(3:2:r)
|
||||
i = 0
|
||||
for (x, y) in zip(e, o)
|
||||
p[i += 1] = x
|
||||
p[i += 1] = y
|
||||
end
|
||||
length(e) > length(o) && (p[i += 1] = e[end])
|
||||
if pmask[p[i] + r + 1] && pmask[p[begin] + 1] && all(j -> pmask[p[j] + p[j + 1]], 1:r-2)
|
||||
lock(spinlock)
|
||||
if counts[r] == 0
|
||||
rowstrings[r] = " 1" * prod([lpad(n, 3) for n in p]) * lpad(r + 1, 3) * "\n"
|
||||
end
|
||||
counts[r] += 1
|
||||
unlock(spinlock)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
println(" 1 2\n" * prod(rowstrings), "\n", counts)
|
||||
end
|
||||
|
||||
@time primetriangle(16)
|
||||
32
Task/Prime-triangle/Julia/prime-triangle-2.julia
Normal file
32
Task/Prime-triangle/Julia/prime-triangle-2.julia
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
using Primes
|
||||
|
||||
function solverow(row, pos, avail)
|
||||
results, nresults = Int[], 0
|
||||
for (i, tf) in enumerate(avail)
|
||||
if tf && isprime(row[pos - 1] + i + 1)
|
||||
if pos >= length(row) - 1 && isprime(row[end] + i + 1)
|
||||
row[pos] = i + 1
|
||||
return (copy(row), 1)
|
||||
else
|
||||
row[pos] = i + 1
|
||||
newav = copy(avail)
|
||||
newav[i] = false
|
||||
newresults, n = solverow(copy(row), pos + 1, newav)
|
||||
nresults += n
|
||||
results = isempty(results) && !isempty(newresults) ? newresults : results
|
||||
end
|
||||
end
|
||||
end
|
||||
return results, nresults
|
||||
end
|
||||
|
||||
function primetriangle(nrows::Integer)
|
||||
nrows < 2 && error("number of rows requested must be > 1")
|
||||
counts, rowstrings = [1; zeros(Int, nrows - 1)], ["" for _ in 1:nrows]
|
||||
for r in 2:nrows
|
||||
p, n = solverow(collect(1:r+1), 2, trues(r - 1))
|
||||
rowstrings[r] = prod([lpad(n, 3) for n in p]) * "\n"
|
||||
counts[r] = n
|
||||
end
|
||||
println(" 1 2\n" * prod(rowstrings), "\n", counts)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue