Primes - allocate descendants to their ancestors: Difference between revisions
Content deleted Content added
Line 1,414:
using Printf
function
aprimes = primes(maxsum)
ancestors = [Vector{Int}() for _ in 1:maxsum]
Line 1,427:
foreach(p -> pop!(descendants[p]), [aprimes..., 4])
total = 0
format(v::Vector{Int}) = join([dot(n) for n in v], ", ")
dot(n::Int) = replace(string(n), r"(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))" => ".")
for s in 1:maxsum
ance = [ancestors[s]..., s]
desc = sort!(descendants[s]); total += dlen = length(desc)
foreach(d-> ancestors[d] = ance,
desc[1:(i-> i==nothing ? 0 : i)(findlast(e-> e<=maxsum, desc))]
)
(s in [0:20..., 46, 74, 99]) || continue
ance = ancestors[s]; alen = length(ance)
@printf("%3d: %1d ancestors %-17s %
, s
, alen, alen==0 ? "" : ance
, dot(dlen), dlen==0 ? ""
)
end
Line 1,446 ⟶ 1,448:
end
{{output}}
<pre>
1: 0 ancestors 0 descendants
2: 0 ancestors 0 descendants
3: 0 ancestors 0 descendants
4: 0 ancestors 0 descendants
5: 0 ancestors 1 descendants [6]
6: 1 ancestors [5] 2 descendants [8, 9]
7: 0 ancestors 2 descendants [10, 12]
8: 2 ancestors [5, 6] 3 descendants [15, 16, 18]
9: 2 ancestors [5, 6] 4 descendants [14, 20, 24, 27]
10: 1 ancestors [7] 5 descendants [21, 25, 30, 32, 36]
11: 0 ancestors 5 descendants [28, 40, 45, 48, 54]
12: 1 ancestors [7] 7 descendants [35, 42, 50, 60, 64, 72, 81]
13: 0 ancestors 8 descendants [22, 56, 63, 75, 80, 90, 96, 108]
14: 3 ancestors [5, 6, 9] 10 descendants [33, 49, 70, 84, 100, 120, 128, 135, 144, 162]
15: 3 ancestors [5, 6, 8] 12 descendants [26, 44, 105, 112, 125, ..., 192, 216, 243]
16: 3 ancestors [5, 6, 8] 14 descendants [39, 55, 66, 98, 140, ..., 270, 288, 324]
17: 0 ancestors 16 descendants [52, 88, 99, 147, 175, ..., 405, 432, 486]
18: 3 ancestors [5, 6, 8] 19 descendants [65, 77, 78, 110, 132, ..., 576, 648, 729]
19: 0 ancestors 22 descendants [34, 104, 117, 165, 176, ..., 810, 864, 972]
20: 3 ancestors [5, 6, 9] 26 descendants [51, 91, 130, 154, 156, ...,
46: 3 ancestors [7, 10, 25] 557 descendants [129, 205, 246, 493, 518, ...,
74: 5 ancestors [5, 6, 8, 16, 39]
99: 1 ancestors [17]
Total descendants:
</pre>
|