Numbers with prime digits whose sum is 13: Difference between revisions

Content added Content deleted
(→‎Tcl: add)
(→‎AWK: add faster alternative)
Line 243: Line 243:


=={{header|AWK}}==
=={{header|AWK}}==
===Counting and testing===
<syntaxhighlight lang="awk">
<syntaxhighlight lang="awk">
# syntax: GAWK -f NUMBERS_WITH_PRIME_DIGITS_WHOSE_SUM_IS_13.AWK
# syntax: GAWK -f NUMBERS_WITH_PRIME_DIGITS_WHOSE_SUM_IS_13.AWK
Line 283: Line 284:
223222 232222 322222
223222 232222 322222
</pre>
</pre>
===Generate digit combinations directly===
<syntaxhighlight lang="awk">BEGIN {
split("2 3 5 7", digits)
src[i = 0] = 13
o = 2
res = ""
while (i != o) {
r = src[i++]
n = src[i++]
for (d = 1; d != 5 && (p = digits[d]) <= r; ++d) {
if (p == r) {
res = res " " n p
} else {
src[o++] = r - p
src[o++] = n p
}
}
}
print substr(res, 2)
}
</syntaxhighlight>
{{out}}
<pre>337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222</pre>


=={{header|C}}==
=={{header|C}}==
Line 2,066: Line 2,090:
set src [list {} 13]
set src [list {} 13]
while {[llength $src]} {
while {[llength $src]} {
set dst {}
set src [lassign $src n r]
foreach {n r} $src {
foreach d {2 3 5 7} {
foreach d {2 3 5 7} {
if {$d > $r} break
if {$d > $r} break
if {$d == $r} {
if {$d == $r} {
lappend res "$n$d"
lappend res "$n$d"
} else {
} else {
lappend src "$n$d" [expr {$r - $d}]
lappend dst "$n$d" [expr {$r - $d}]
}
}
}
}
}
lset src $dst
}
}
puts $res</syntaxhighlight>
puts $res</syntaxhighlight>