Primes - allocate descendants to their ancestors: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(22 intermediate revisions by 8 users not shown)
Line 45:
Total Descendants 546.986
</pre>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V maxsum = 99
 
F get_primes(max)
V lprimes = [2]
L(x) (3..max).step(2)
L(p) lprimes
I x % p == 0
L.break
L.was_no_break
lprimes.append(x)
R lprimes
 
V descendants = [[Int64]()] * (maxsum + 1)
V ancestors = [[Int64]()] * (maxsum + 1)
 
V primes = get_primes(maxsum)
 
L(p) primes
descendants[p].append(p)
L(s) 1 .< descendants.len - p
descendants[s + p] [+]= descendants[s].map(pr -> @p * pr)
 
L(p) primes [+] [4]
descendants[p].pop()
 
V total = 0
L(s) 1..maxsum
descendants[s].sort()
L(d) descendants[s]
I d > maxsum
L.break
ancestors[Int(d)] = ancestors[s] [+] [Int64(s)]
print([s]‘ Level: ’ancestors[s].len)
print(‘Ancestors: ’(I !ancestors[s].empty {String(ancestors[s])} E ‘None’))
print(‘Descendants: ’(I !descendants[s].empty {String(descendants[s].len)} E ‘None’))
I !descendants[s].empty
print(descendants[s])
print()
total += descendants[s].len
 
print(‘Total descendants ’total)</syntaxhighlight>
 
=={{header|AutoHotkey}}==
Line 50 ⟶ 95:
 
I seem that the use of an associative array is a little bit slower than the use of a simple array combined with the 'Sort' command, even if the 'Sort' command pumps 85% of the processing time.
<syntaxhighlight lang="autohotkey">#Warn
<lang AutoHotkey>#Warn
#SingleInstance force
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
Line 214 ⟶ 259:
return lPrimes
}</langsyntaxhighlight>
 
=={{header|C}}==
Line 224 ⟶ 269:
 
The InsertChild function is replaced by the AppendChild function which appends directly the child as the new last item in the list.
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Line 437 ⟶ 482:
 
return Index;
}</langsyntaxhighlight>
 
=== Optimized Approach ===
Line 445 ⟶ 490:
 
It is based on the same logic as the Python script.
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Line 687 ⟶ 732:
 
return Index;
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <vector>
Line 776 ⟶ 821:
std::cout << "Total descendants: " << total_descendants << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 810 ⟶ 855:
=={{header|Go}}==
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 893 ⟶ 938:
}
fmt.Println("\nTotal descendants", total)
}</langsyntaxhighlight>
 
{{out}}
Line 923 ⟶ 968:
Total descendants 546986
</pre>
 
=={{header|Haskell}}==
{{trans|Racket}}
 
<syntaxhighlight lang="haskell">{-# LANGUAGE DeriveFunctor #-}
import Data.Numbers.Primes (isPrime)
import Data.List
 
------------------------------------------------------------
-- memoization utilities
 
type Memo2 a = Memo (Memo a)
 
data Memo a = Node a (Memo a) (Memo a)
deriving Functor
 
memo :: Integral a => Memo p -> a -> p
memo (Node a l r) n
| n == 0 = a
| odd n = memo l (n `div` 2)
| otherwise = memo r (n `div` 2 - 1)
 
nats :: Integral a => Memo a
nats = Node 0 ((+1).(*2) <$> nats) ((*2).(+1) <$> nats)
 
memoize :: Integral a => (a -> b) -> (a -> b)
memoize f = memo (f <$> nats)
 
memoize2 :: (Integral a, Integral b) => (a -> b -> c) -> (a -> b -> c)
memoize2 f = memoize (memoize . f)
 
------------------------------------------------------------
 
partProd = memoize2 partProdM
where
partProdM x p
| p == 0 = []
| x == 0 = [1]
| x < 0 = []
| isPrime p = ((p *) <$> partProdM (x - p) p) ++
partProd x (p - 1)
| otherwise = partProd x (p - 1)
 
descendants = memoize descendantsM
where
descendantsM x =
if x == 4 then [] else sort (partProd x (x - 1))
 
ancestors = memoize ancestorsM
where
ancestorsM z = concat [ ancestors x ++ [x]
| x <- [z-1,z-2..1]
, z `elem` descendants x ]
 
main = do
mapM_ (putStrLn . task1) [1..15]
putStrLn (task2 46)
putStrLn (task2 99)
putStrLn task3
where
task1 n = show n ++
" ancestors:" ++ show (ancestors n) ++
" descendants:" ++ show (descendants n)
task2 n = show n ++ " has " ++
show (length (ancestors n)) ++ " ancestors, " ++
show (length (descendants n)) ++ " descendants."
task3 = "Total ancestors up to 99: " ++
show (sum $ length . ancestors <$> [1..99]) ++
"\nTotal descendants up to 99: " ++
show (sum $ length . descendants <$> [1..99])</syntaxhighlight>
 
<pre>λ> main
1 ancestors:[] descendants:[]
2 ancestors:[] descendants:[]
3 ancestors:[] descendants:[]
4 ancestors:[] descendants:[]
5 ancestors:[] descendants:[6]
6 ancestors:[5] descendants:[8,9]
7 ancestors:[] descendants:[10,12]
8 ancestors:[5,6] descendants:[15,16,18]
9 ancestors:[5,6] descendants:[14,20,24,27]
10 ancestors:[7] descendants:[21,25,30,32,36]
11 ancestors:[] descendants:[28,40,45,48,54]
12 ancestors:[7] descendants:[35,42,50,60,64,72,81]
13 ancestors:[] descendants:[22,56,63,75,80,90,96,108]
14 ancestors:[5,6,9] descendants:[33,49,70,84,100,120,128,135,144,162]
15 ancestors:[5,6,8] descendants:[26,44,105,112,125,126,150,160,180,192,216,243]
46 has 3 ancestors, 557 descendants.
99 has 1 ancestors, 38257 descendants.
Total ancestors up to 99: 179
Total descendants up to 99: 546986
(3.41 secs, 737,653,968 bytes)</pre>
 
=={{header|J}}==
Line 948 ⟶ 1,085:
===Implementation I===
 
<langsyntaxhighlight Jlang="j">require'strings files'
 
family=:3 :0 M.
Line 984 ⟶ 1,121:
)
task 99</langsyntaxhighlight>
 
If you want to inspect individual results, that's fairly straightforward.
Line 994 ⟶ 1,131:
=== Some examples ===
 
<langsyntaxhighlight Jlang="j"> #;descendants&.>1+i.99
546986
level 46
Line 1,009 ⟶ 1,146:
8 6 5
#descendants 18
19</langsyntaxhighlight>
 
===Implementation II===
Line 1,022 ⟶ 1,159:
Furthermore, the script produces the full report in a '.txt' file which can easily be compared with the output of some of the other languages. In Windows : <code>fc /N /L</code>
 
<langsyntaxhighlight Jlang="j">getdescendants=: 3 : 0
dd=: (<(>y{dd),y)y}dd
y getproducts"0 >:i.maxsum-y
Line 1,064 ⟶ 1,201:
 
file=: jpath '~user/temp/Ancestors.ijs.txt'
main 99</langsyntaxhighlight>
 
=={{header|Java}}==
{{Trans|C++}}
<langsyntaxhighlight lang="java">import java.io.*;
import java.util.*;
 
Line 1,170 ⟶ 1,307:
writer.write("\n");
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,201 ⟶ 1,338:
Total descendants: 546986
</pre>
=== Another version ===
<syntaxhighlight lang="java">import static java.lang.Math.sqrt;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public class PrimeAncestorsDescendants {
 
public static void main(String[] args) {
print(100);
}
 
public static void print(int limit) {
print(get(limit));
}
 
record PAD (int limit, List<List<Integer>> ancestors, List<List<Long>> descendants, int totalDescendants) {}
 
public static PAD get(int limit) {
List<List<Integer>> ancestors = new ArrayList<>(limit);
List<List<Long>> descendants = new ArrayList<>(limit);
for (int i=0; i<limit; i+=1) {
ancestors.add(new ArrayList<>());
descendants.add(new ArrayList<>());
}
List<Integer> primes = primesBelow(limit);
for (int p: primes) {
descendants.get(p).add((long) p);
for (int i=0, s=p; s<limit; s+=1, i+=1) {
for (long d: descendants.get(i)) {
descendants.get(s).add(p*d);
}
}
}
descendants.get(4).remove(0);
for (int p: primes) removeLast(descendants.get(p));
int totalDescendants = 0;
for (int i=1; i<limit; i+=1) {
List<Long> desc = sort(descendants.get(i));
totalDescendants += desc.size();
for (long d: desc) {
if (d >= limit) break;
ancestors.set((int) d, add(ancestors.get(i), i));
}
}
return new PAD(limit, ancestors, descendants, totalDescendants);
}
 
private static List<Integer> primesBelow(int limit) {
List<Integer> primes = new ArrayList<>();
boolean[] isComposite = new boolean[limit];
//int p=2; for (; p*p<limit; p+=1) {
int p=2; for (int sr=(int) sqrt(limit); p<sr; p+=1) {
if (isComposite[p]) continue;
primes.add(p);
for (int i=p*p; i<limit; i+=p) isComposite[i] = true;
}
for (; p<limit; p+=1) {
if (isComposite[p]) continue;
primes.add(p);
}
return primes;
}
 
private static List<Long> removeLast(List<Long> list) {
int size = list.size();
if (size > 0) list.remove(size-1);
return list;
}
private static <T extends Comparable<? super T>> List<T> sort(List<T> list) {
Collections.sort(list);
return list;
}
 
private static List<Integer> add(List<Integer> list, int n) {
list = new ArrayList<>(list);
list.add(n);
return list;
}
public static void print(PAD pad) {
for (int i=1; i<pad.limit; i+=1) {
if (i > 20 && i != 46 && i != 74 && i != 94 && i != 99) continue;
System.out.printf("%2d:", i);
printf(" %,d ancestors %-17s", pad.ancestors.get(i));
printf(" %,6d descendants %s\n", pad.descendants.get(i));
}
System.out.printf("\nTotal descendants: %,d\n", pad.totalDescendants);
}
private static <T extends Number> void printf(String fmt, List<T> list) {
System.out.printf(fmt, list.size(), format(list));
}
private static <T extends Number> String format(List<T> list) {
if (list.isEmpty()) return "";
StringBuilder sb = new StringBuilder("[");
if (list.size() <= 10) {
for (int i=0; i<list.size(); i+=1) sb.append(format(list, i));
}
else {
for (int i=0; i<5; i+=1) sb.append(format(list, i));
sb.append(", ...");
for (int i=list.size()-3; i<list.size(); i+=1) sb.append(format(list, i));
}
return sb.append("]").toString();
}
 
private static <T extends Number> String format(List<T> list, int i) {
return (i==0 ? "" : ", ") + String.format("%,d", list.get(i).longValue());
}
}
</syntaxhighlight>
{{out}}
<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, ..., 1.215, 1.296, 1.458]
46: 3 ancestors [7, 10, 25] 557 descendants [129, 205, 246, 493, 518, ..., 15.943.230, 17.006.112, 19.131.876]
74: 5 ancestors [5, 6, 8, 16, 39] 6.336 descendants [213, 469, 670, 793, 804, ..., 470.715.894.135, 502.096.953.744, 564.859.072.962]
94: 5 ancestors [5, 6, 9, 14, 49] 27.251 descendants [445, 534, 913, 1.633, 2.054, ..., 686.303.773.648.830, 732.057.358.558.752, 823.564.528.378.596]
99: 1 ancestors [17] 38.257 descendants [194, 1.869, 2.225, 2.670, 2.848, ..., 4.392.344.151.352.512, 4.941.387.170.271.576, 5.559.060.566.555.523]
 
Total descendants: 546.986
 
</pre>
 
=={{header|jq}}==
''Adapted from [[#Wren|Wren]]''
 
'''Works with jq and gojq, the C and Go implementations of jq'''
 
jaq, the Rust implementation of jq, does not currently have a `sqrt` function or support string
interpolation, but if the necessary tweaks are made, then the
following program will also run under jaq, with the same output.
<syntaxhighlight lang=jq>
def lpad($len): tostring | ($len - length) as $l | ([range(0;$l)|" "]|join("")) + .;
 
# Input: a positive integer
# Output: an array, $a, of length .+1 such that
# $a[$i] is $i if $i is prime, and false otherwise.
def primeSieve:
# erase(i) sets .[i*j] to false for integral j > 1
def erase($i):
if .[$i] then
reduce (range(2*$i; length; $i)) as $j (.; .[$j] = false)
else .
end;
(. + 1) as $n
| (($n|sqrt) / 2) as $s
| [null, null, range(2; $n)]
| reduce (2, 1 + (2 * range(1; $s|floor))) as $i (.; erase($i)) ;
 
def primes: primeSieve | map(select(.));
 
# Input: an array
def ellipsis($n):
if length <= $n then tostring
else "[" + (.[0:10] | map(tostring) | join(",")) + ", ...]"
end;
 
def task($maxSum):
($maxSum|primes) as $primes
| {maxSum: $maxSum,
descendants: [range(0; 1+$maxSum)|[]]}
| .ancestors = .descendants
| reduce $primes[] as $p (.;
.descendants[$p] += [$p]
| reduce range(1; .descendants|length - $p) as $s (.;
.descendants[$s + $p] += (.descendants[$s] | map($p * .) ) ) )
| reduce ($primes + [4])[] as $p (.; .descendants[$p] |= .[:-1] )
| .total = 0
| foreach (range(1; 1 + .maxSum),null) as $s (.;
if $s == null then .emit= "\nTotal descendants \(.total)"
else .emit = null
| .descendants[$s] |= sort
| .total += (.descendants[$s]|length)
| .ix=0
| (.descendants[$s]|length) as $ld
| until( .ix == $ld;
.descendants[$s][.ix] as $d
| if $d > .maxSum then .ix = $ld # early exit
else .ancestors[$d] = .ancestors[$s] + [$s]
| .ix += 1
end )
| if ($s < 21 or $s == 46 or $s == 74 or $s == .maxSum)
then .emit = "\($s|lpad(2)): \(.ancestors[$s]|length) ancestor[s]: \(.ancestors[$s]|lpad(18))"
| (.descendants[$s]|length) as $count
| .emit += " \($count|lpad(5)) descendant[s]: \(.descendants[$s]|ellipsis(10))"
else .
end
end)
| select(.emit).emit;
 
task(99)
</syntaxhighlight>
{{output}}
<pre>
1: 0 ancestor[s]: [] 0 descendant[s]: []
2: 0 ancestor[s]: [] 0 descendant[s]: []
3: 0 ancestor[s]: [] 0 descendant[s]: []
4: 0 ancestor[s]: [] 0 descendant[s]: []
5: 0 ancestor[s]: [] 1 descendant[s]: [6]
6: 1 ancestor[s]: [5] 2 descendant[s]: [8,9]
7: 0 ancestor[s]: [] 2 descendant[s]: [10,12]
8: 2 ancestor[s]: [5,6] 3 descendant[s]: [15,16,18]
9: 2 ancestor[s]: [5,6] 4 descendant[s]: [14,20,24,27]
10: 1 ancestor[s]: [7] 5 descendant[s]: [21,25,30,32,36]
11: 0 ancestor[s]: [] 5 descendant[s]: [28,40,45,48,54]
12: 1 ancestor[s]: [7] 7 descendant[s]: [35,42,50,60,64,72,81]
13: 0 ancestor[s]: [] 8 descendant[s]: [22,56,63,75,80,90,96,108]
14: 3 ancestor[s]: [5,6,9] 10 descendant[s]: [33,49,70,84,100,120,128,135,144,162]
15: 3 ancestor[s]: [5,6,8] 12 descendant[s]: [26,44,105,112,125,126,150,160,180,192, ...]
16: 3 ancestor[s]: [5,6,8] 14 descendant[s]: [39,55,66,98,140,168,189,200,225,240, ...]
17: 0 ancestor[s]: [] 16 descendant[s]: [52,88,99,147,175,210,224,250,252,300, ...]
18: 3 ancestor[s]: [5,6,8] 19 descendant[s]: [65,77,78,110,132,196,280,315,336,375, ...]
19: 0 ancestor[s]: [] 22 descendant[s]: [34,104,117,165,176,198,245,294,350,420, ...]
20: 3 ancestor[s]: [5,6,9] 26 descendant[s]: [51,91,130,154,156,220,264,297,392,441, ...]
46: 3 ancestor[s]: [7,10,25] 557 descendant[s]: [129,205,246,493,518,529,740,806,888,999, ...]
74: 5 ancestor[s]: [5,6,8,16,39] 6336 descendant[s]: [213,469,670,793,804,1333,1342,1369,1534,2014, ...]
99: 1 ancestor[s]: [17] 38257 descendant[s]: [194,1869,2225,2670,2848,3204,3237,4029,4565,5037, ...]
 
Total descendants 546986
</pre>
 
 
=={{header|Julia}}==
{{trans|Go}}
<langsyntaxhighlight lang="julia">using Primes
 
function ancestraldecendants(maxsum)
Line 1,235 ⟶ 1,620:
 
ancestraldecendants(99)
</syntaxhighlight>
</lang>{{output}}<pre>
{{output}}
<pre>
1: 0 Ancestor(s):Int64[] 0 Descendant(s): Int64[]
2: 0 Ancestor(s):Int64[] 0 Descendant(s): Int64[]
Line 1,262 ⟶ 1,649:
</pre>
=== A remake ===
<langsyntaxhighlight lang="julia">using Primes
using Printf
function ancestraldecendantsprimeAncestorsDecendants(maxsum)
aprimes = primes(maxsum)
ancestors = [Vector{Int}() for _ in 1:maxsum]
Line 1,272 ⟶ 1,660:
foreach(
s -> append!(descendants[s + p], [p * pr for pr in descendants[s]]),
2 : length(descendants) - p
)
end
foreach(p -> pop!(descendants[p]), vcat([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])
desc = sort!(descendants[s]); total += dlen = length(desc)
foreach(d-> ancestors[d] = ance,
total += dlen
#foreach desc[1:(di-> ancestors[d] i==nothing vcat(ancestors[s],? 0 [s]: i), desc[findall(x findlast(e-> xe<=maxsum, desc)])]
foreach(d-> ancestors[d] = vcat(ancestors[s], [s]),
desc[1:(i -> i==nothing ? dlen : i-1)(findfirst(e -> e>maxsum, desc))]
)
if !(s in vcat(collect([0:20)..., 46, 74, 99)]) continue|| endcontinue
ance = ancestors[s]; alen = length(ance)
@printf("%3d: %1d ancestors %-17s %6s descendants %s\n"
println(
lpad(s, 3), ":",s
, alen, alen==0 ? "" : ance
lpad("$(length(ance))", 2), " ancestors ",
rpad, dot(length(ancedlen), dlen== 0 ? "" : "$(ance)", 18),
: dlen<11 ? desc
lpad("$(length(desc))", 5), " descendants ",
: "[$(format(desc[1:5])), ..., $(format(desc[end-2:end]))]"
dlen == 0 ? ""
: dlen <= 10 ? "$(desc)"
: "[$(join(desc[1:5], ", ")), ..., $(join(desc[dlen-2:dlen], ", "))]"
)
end
print("Total\nTotal descendants: ", total)
end
primeAncestorsDecendants(99)</syntaxhighlight>
ancestraldecendants(99)</lang>
{{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, ..., 12151.215, 12961.296, 14581.458]
46: 3 ancestors [7, 10, 25] 557 descendants [129, 205, 246, 493, 518, ..., 1594323015.943.230, 1700611217.006.112, 1913187619.131.876]
74: 5 ancestors [5, 6, 8, 16, 39] 63366.336 descendants [213, 469, 670, 793, 804, ..., 470715894135470.715.894.135, 502096953744502.096.953.744, 564859072962564.859.072.962]
99: 1 ancestors [17] 3825738.257 descendants [194, 18691.869, 22252.225, 26702.670, 28482.848, ..., 43923441513525124.392.344.151.352.512, 49413871702715764.941.387.170.271.576, 55590605665555235.559.060.566.555.523]
 
Total descendants: 546.986
</pre>
 
=={{header|Kotlin}}==
{{trans|Python}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
const val MAXSUM = 99
Line 1,374 ⟶ 1,762:
 
println("\nTotal descendants $total")
}</langsyntaxhighlight>
 
{{out}}
Line 1,407 ⟶ 1,795:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight Nimlang="nim">import algorithm, strformat, strutils, sugar
 
const MaxSum = 99
Line 1,447 ⟶ 1,835:
inc total, dlength
 
echo "Total descendants: ", total</langsyntaxhighlight>
 
{{out}}
Line 1,492 ⟶ 1,880:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use List::Util qw(sum uniq);
use ntheory qw(nth_prime);
 
Line 1,536 ⟶ 1,924:
 
map { for my $k (keys %{$tree{$_}{descendants}}) { $total += $tree{$_}{descendants}{$k} } } 1..$max;
print "\nTotal descendants: $total\n";</langsyntaxhighlight>
{{out}}
<pre> 1, 0 Ancestors: [], 0 Descendants: []
Line 1,560 ⟶ 1,948:
=={{header|Phix}}==
{{trans|Go}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant maxSum = 99
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
 
<span style="color: #008080;">constant</span> <span style="color: #000000;">maxSum</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">99</span>
function getPrimes()
sequence primes = {2}
<span style="color: #008080;">function</span> <span style="color: #000000;">stringify</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
for x=3 to maxSum by 2 do
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
bool zero = false
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for i=1 to length(primes) do
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
if mod(x,primes[i]) == 0 then
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
zero = true
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
exit
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end if
end for
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
if not zero then
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
primes = append(primes, x)
<span style="color: #004080;">integer</span> <span style="color: #000000;">p</span>
end if
<span style="color: #004080;">sequence</span> <span style="color: #000000;">descendants</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">({},</span><span style="color: #000000;">maxSum</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
end for
<span style="color: #000000;">ancestors</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">({},</span><span style="color: #000000;">maxSum</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
return primes
<span style="color: #000000;">primes</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">maxSum</span><span style="color: #0000FF;">)</span>
end function
 
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
function stringify(sequence s)
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
for i=1 to length(s) do
<span style="color: #000000;">descendants</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">descendants</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
s[i] = sprintf("%d",s[i])
<span style="color: #008080;">for</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">descendants</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">p</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">descendants</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">+</span><span style="color: #000000;">p</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">descendants</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">+</span><span style="color: #000000;">p</span><span style="color: #0000FF;">])</span>
return s
<span style="color: #0000FF;">&</span> <span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">descendants</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">],</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
procedure main()
atom t0 = time()
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">4</span>
integer p
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
sequence descendants = repeat({},maxSum+1),
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
ancestors = repeat({},maxSum+1),
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">descendants</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">])!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
primes = getPrimes()
<span style="color: #000000;">descendants</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">descendants</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
for i=1 to length(primes) do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
p = primes[i]
descendants[p] = append(descendants[p], p)
<span style="color: #004080;">integer</span> <span style="color: #000000;">total</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
for s=1 to length(descendants)-p do
descendants[s+p] &= sq_mul(descendants[s], p)
<span style="color: #008080;">for</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">maxSum</span> <span style="color: #008080;">do</span>
end for
<span style="color: #004080;">sequence</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">descendants</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">]))</span>
end for
<span style="color: #000000;">total</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
p = 4
<span style="color: #004080;">atom</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
for i=0 to length(primes) do
<span style="color: #008080;">if</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">></span><span style="color: #000000;">maxSum</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if i>0 then p = primes[i] end if
<span style="color: #000000;">ancestors</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ancestors</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">])</span>
if length(descendants[p])!=0 then
<span style="color: #0000FF;">&</span><span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ancestors</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">]),</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
descendants[p] = descendants[p][1..$-1]
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;"><</span><span style="color: #000000;">26</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">46</span><span style="color: #0000FF;">,</span><span style="color: #000000;">74</span><span style="color: #0000FF;">,</span><span style="color: #000000;">99</span><span style="color: #0000FF;">})</span> <span style="color: #008080;">then</span>
end for
<span style="color: #004080;">sequence</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ancestors</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">]</span>
 
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
integer total = 0
<span style="color: #004080;">string</span> <span style="color: #000000;">sp</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"s"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">stringify</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
for s=1 to maxSum do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d: %d Ancestor%s: [%-14s"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">sp</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"]"</span><span style="color: #0000FF;">})</span>
sequence x = sort(descendants[s])
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">descendants</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">]))</span>
total += length(x)
<span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
for i=1 to length(x) do
<span style="color: #000000;">sp</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"s"</span><span style="color: #0000FF;">)</span>
atom d = x[i]
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;"><</span><span style="color: #000000;">10</span> <span style="color: #008080;">then</span>
if d>maxSum then exit end if
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">stringify</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
ancestors[d] &= append(ancestors[s], s)
<span style="color: #008080;">else</span>
end for
<span style="color: #000000;">d</span><span style="color: #0000FF;">[</span><span style="color: #000000;">4</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span>
if s<26 or find(s,{46,74,99}) then
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">stringify</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
sequence d = ancestors[s]
<span style="color: #000000;">d</span><span style="color: #0000FF;">[</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"..."</span>
integer l = length(d)
string sp<span style="color: iff(l=1?#008080;">end</span> <span style="color: #008080;"s")>if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%5d Descendant%s: [%s]\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">sp</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)})</span>
d = stringify(d)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"%2d: %d Ancestor%s: [%-14s", {s, l, sp, join(d)&"]"})
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
d = sort(descendants[s])
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nTotal descendants %d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">total</span><span style="color: #0000FF;">)</span>
l = length(d)
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- about 1s</span>
sp = iff(l=1?" ":"s")
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">33</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">4e9</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- over 16 days</span>
if l<10 then
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
d = stringify(d)
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
else
<!--</syntaxhighlight>-->
d[4..-4] = {0}
d = stringify(d)
d[4] = "..."
end if
printf(1,"%5d Descendant%s: [%s]\n", {l, sp, join(d)})
end if
end for
printf(1,"\nTotal descendants %d\n", total)
?elapsed(time()-t0) -- < 1s
?elapsed(5559060566555523/4_000_000_000) -- > 16 days
end procedure
main()</lang>
{{out}}
<pre>
Line 1,675 ⟶ 2,052:
 
Total descendants 546986
"01.7s2s"
"16 days, 2 hours, 2 minutes and 45s"
</pre>
Line 1,684 ⟶ 2,061:
=={{header|Python}}==
Python is very flexible, concise and effective with lists.
<langsyntaxhighlight lang="python">from __future__ import print_function
from itertools import takewhile
 
Line 1,727 ⟶ 2,104:
total += len(descendants[s])
 
print("Total descendants", total)</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 1,739 ⟶ 2,116:
We first define a macro to create memorized functions and a few auxiliary functions. In particular <code>(border list)</code> transforms a long list in a list with ellipsis.
 
<langsyntaxhighlight Racketlang="racket">#lang racket
 
(define-syntax-rule (define/mem (name args ...) body ...)
Line 1,756 ⟶ 2,133:
(define (add-tail list x)
(reverse (cons x (reverse list))))</langsyntaxhighlight>
 
The main part of the program uses the memorized functions.
 
<langsyntaxhighlight Racketlang="racket"> (define/mem (prime? x)
(if (= x 1)
#f
Line 1,802 ⟶ 2,179:
(define (total-descendants n)
(for/sum ([x (in-range 1 (add1 n))])
(length (descendants x))))</langsyntaxhighlight>
 
Now we display some results.
 
<langsyntaxhighlight Racketlang="racket">#;(for ([x (in-range 1 (add1 99))])
(show-info x))
(for ([x (in-range 1 (add1 15))])
Line 1,818 ⟶ 2,195:
(newline)
(printf "Total ancestors up to 99: ~a\n" (total-ancestors 99))
(printf "Total descendants up to 99: ~a\n" (total-descendants 99))</langsyntaxhighlight>
 
{{out}}
Line 1,849 ⟶ 2,226:
{{works with|Rakudo|2018.11}}
 
<syntaxhighlight lang="raku" perl6line>my $max = 99;
my @primes = (2 .. $max).race(:16batch).grep: *.is-prime;
my %tree;
(1..$max).race(:16batch).map: {
%tree{$_}<ancestor> = ();
%tree{$_}<descendants> = {};
Line 1,893 ⟶ 2,270:
 
say "\nTotal descendants: ",
sum (1..$max).race(:16batch).map({ +%tree{$_}<descendants> });</langsyntaxhighlight>
{{out}}
<pre> 1, 0 Ancestors: [], 0 Descendants: []
Line 1,918 ⟶ 2,295:
=={{header|Sidef}}==
{{trans|Go}}
<langsyntaxhighlight lang="ruby">var maxsum = 99
var primes = maxsum.primes
 
Line 1,944 ⟶ 2,321:
var idx = descendants[s].first_index {|x| x > maxsum }
 
for d in (descendants[s].slicefirst(0, idx+1)) {
ancestors[d] = (ancestors[s] + [s])
}
Line 1,955 ⟶ 2,332:
}
 
say "\nTotal descendants: #{total}"</langsyntaxhighlight>
{{out}}
<pre>
Line 1,987 ⟶ 2,364:
=={{header|Simula}}==
{{trans|Python}}
<langsyntaxhighlight lang="simula">COMMENT cim --memory-pool-size=512 allocate-descendants-to-their-ancestors.sim ;
BEGIN
Line 2,276 ⟶ 2,653:
OUTINT(TOTAL, 0);
OUTIMAGE;
END.</langsyntaxhighlight>
{{out}}
<pre>[1] LEVEL: 0
Line 2,380 ⟶ 2,757:
=={{header|Visual Basic .NET}}==
It is based on the same logic as the Python script.
<langsyntaxhighlight lang="vbnet">Imports System.Math
 
Module Module1
Line 2,597 ⟶ 2,974:
Return vbTrue
End Function
End Module</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 2,604 ⟶ 2,981:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./sort" for Sort
import "./fmt" for Fmt
 
var maxSum = 99
Line 2,639 ⟶ 3,016:
}
}
System.print("\nTotal descendants %(total)")</langsyntaxhighlight>
 
{{out}}
Line 2,674 ⟶ 3,051:
{{trans|Racket}}
Using [[Extensible prime generator#zkl]]
<langsyntaxhighlight lang="zkl">const maxsum=99;
primes:=Utils.Generator(Import("sieve.zkl").postponed_sieve)
Line 2,703 ⟶ 3,080:
}
println("Total ancestors: %,d".fmt(ta));
println("Total descendants: %,d".fmt(td));</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits