Numbers with prime digits whose sum is 13: Difference between revisions
Numbers with prime digits whose sum is 13 (view source)
Revision as of 21:06, 11 April 2024
, 1 month agoAdded Easylang
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
(Added Easylang) |
||
(16 intermediate revisions by 7 users not shown) | |||
Line 5:
=={{header|11l}}==
{{trans|C}}
<syntaxhighlight lang="11l">F primeDigitsSum13(=n)
V sum = 0
Line 84 ⟶ 83:
BYTE ARRAY digits(MAXDIG)
BYTE count,pos
count=1 pos=0
Init(count)
Line 211 ⟶ 210:
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">pDigits: [2 3 5 7]
lst: map pDigits 'd -> @[d]
result: new []
Line 245 ⟶ 243:
=={{header|AWK}}==
===Counting and testing===
<syntaxhighlight lang="awk">
# syntax: GAWK -f NUMBERS_WITH_PRIME_DIGITS_WHOSE_SUM_IS_13.AWK
Line 284 ⟶ 283:
32233 32323 32332 33223 33232 33322 52222 222223 222232 222322
223222 232222 322222
</pre>
===Generate digit combinations directly===
<syntaxhighlight lang="awk">BEGIN {
o = 1
src[o++] = 13
do {
r = src[++i]
n = src[++i]
for (p = 2; p != 9; p += p % 2 + 1) {
if (p >= r) {
if (p == r) res = res " " n p
break
}
src[++o] = r - p
src[++o] = n p
}
} while (i != o)
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|bc}}==
<syntaxhighlight lang="bc">q[0] = 13
o = 2
while (i != o) {
r = q[i++]
n = q[i++]
for (p = 2; p != 9; p += p % 2 + 1) {
if (p >= r) {
if (p == r) n + p
break
}
q[o++] = r - p
q[o++] = (n + p) * 10
}
}</syntaxhighlight>
{{out}}
<pre style="max-height:12em">
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>
Line 427 ⟶ 508:
22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332
33223 33232 33322 52222 222223 222232 222322 223222 232222 322222</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
func digprimsum13 n .
while n > 0
d = n mod 10
if d < 2 or d = 4 or d = 6 or d >= 8
return 0
.
sum += d
n = n div 10
.
return if sum = 13
.
p = 2
while p <= 322222
if digprimsum13 p = 1
write p & " "
.
p += 1
.
</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|F_Sharp|F#}}==
Line 456 ⟶ 563:
{ 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|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
<syntaxhighlight lang="Delphi">
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N+0.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
procedure GetDigits(N: integer; var IA: TIntegerDynArray);
{Get an array of the integers in a number}
var T: integer;
begin
SetLength(IA,0);
repeat
begin
T:=N mod 10;
N:=N div 10;
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=T;
end
until N<1;
end;
function IsPrimeDigitSum13(N: integer): boolean;
{Return true N's digits are prime and total 13}
var IA: TIntegerDynArray;
var I,Sum: integer;
begin
Result:=False;
GetDigits(N,IA);
for I:=0 to High(IA) do
if not IsPrime(IA[I]) then exit;
Sum:=0;
for I:=0 to High(IA) do Sum:=Sum+IA[I];
Result:=Sum=13;
end;
procedure ShowPrimeDigitSum13(Memo: TMemo);
{Show numbers whose digits are prime and total 13}
var I,Cnt: integer;
var S: string;
begin
Cnt:=0;
for I:=1 to 999999 do
if IsPrimeDigitSum13(I) then
begin
Inc(Cnt);
S:=S+Format('%8D',[I]);
If (Cnt mod 5)=0 then S:=S+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
</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
Elapsed Time: 627.207 ms.
</pre>
===F# translation===
The following is based on Nigel Galloway's algorithm as described [http://rosettacode.org/wiki/Talk:Numbers_with_prime_digits_whose_sum_is_13#Nice_recursive_solution here] on the talk page. It's about 10x faster than the previous method.
Line 783 ⟶ 984:
32233 32323 32332 33223 33232 33322 52222 222223 222232 222322
223222 232222 322222</pre>
=={{header|J}}==
Galloway's algorithm, from the talk page:
<syntaxhighlight lang=J>ps13=: {{
seq=. 0#,D=. ,Q=. ":,.p:i.4
while. #Q do.
N=. ,/D,"0 1/Q
i=. +/"1"."0 N
Q=. (12>i)#N
seq=. seq,,".(13=i)#N
end.
}}0</syntaxhighlight>
Here, upper case names are character arrays representing digits, lower case names are numeric arrays.
<code>Q</code> (number suffixes) and <code>N</code> (candidate numbers) represent sequences of sequences of characters. (The top level sequence -- rows -- distinguishes different potential numbers and the secondary sequence -- columns -- distinguishes digits within potential numbers).
Meanwhile, <code>D</code> (prime digits), <code>i</code> (digit sums) and <code>seq</code> (numbers whose digit sum is 13) are simple sequences.
<syntaxhighlight lang=J> ps13
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</syntaxhighlight>
=={{header|Java}}==
Line 1,089 ⟶ 1,313:
reduce .[] as $i ([]; .[$i] += 1);
(length|factorial) / (histogram|product_of_factorials);
def number_of_interesting_numbers($total):
def digits: [2, 3, 5, 7];
Line 1,305 ⟶ 1,529:
23323 23332 25222 32233 32323 32332 33223 33232 33322
52222 222223 222232 222322 223222 232222 322222 </pre>
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let prime_digits_13 =
let digits = [2; 3; 5; 7] in
let rec next ds ns = function
| [] -> if ns = [] then [] else next digits [] (List.rev ns)
| (n, r) :: cs' as cs ->
match ds with
| d :: ds' when d < r -> next ds' (((n + d) * 10, r - d) :: ns) cs
| d :: ds' when d = r -> n + d :: next digits ns cs'
| _ -> next digits ns cs'
in next digits [] [0, 13]
let () =
List.map string_of_int prime_digits_13 |> String.concat " " |> print_endline</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|Pascal}}==
Line 1,784 ⟶ 2,025:
=={{header|Phix}}==
<!--
<syntaxhighlight lang="phix">
with javascript_semantics
function unlucky(sequence set, integer needed, string v="", sequence res={})
if needed=0 then
res = append(res,sprintf("%6s",v))
elsif needed>0 then
for i=length(set) to 1 by -1 do
res = unlucky(set,needed-set[i],(set[i]+'0')&v,res)
end for
end if
return res
end function
sequence r = sort(unlucky({2,3,5,7},13))
puts(1,join_by(r,1,11," "))
{{out}}
<pre>
Line 1,809 ⟶ 2,050:
===iterative===
Queue-based version of Nigel's recursive algorithm, same output.
<!--
<syntaxhighlight lang="phix">
with javascript_semantics
requires("0.8.2") -- uses latest apply() mods, rest is fine
constant dgts = {2,3,5,7}
function unlucky()
sequence res = {}, q = {{0,0}}
integer s, -- partial digit sum, <=11
v -- corresponding value
while length(q) do
{{s,v}, q} = {q[1], q[2..$]}
for i=1 to length(dgts) do
integer d = dgts[i], {ns,nv} = {s+d,v*10+d}
if ns<=11 then q &= {{ns,nv}}
elsif ns=13 then res &= nv end if
end for
end while
return res
end function
sequence r = unlucky()
r = apply(true,sprintf,{{"%6d"},r})
puts(1,join_by(r,1,11," "))
I've archived a slightly more OTT version: [[Numbers_with_prime_digits_whose_sum_is_13/Phix]].
Line 1,866 ⟶ 2,107:
Those numbers whose digits are all prime and sum to 13 are:
[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|Python}}==
With improvements to the ideas from the discussion page:
<syntaxhighlight lang="python">from collections import deque
def prime_digits_sum(r):
q = deque([(r, 0)])
while q:
r, n = q.popleft()
for d in 2, 3, 5, 7:
if d >= r:
if d == r: yield n + d
break
q.append((r - d, (n + d) * 10))
print(*prime_digits_sum(13))</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|Quackery}}==
<syntaxhighlight lang="Quackery"> [] ' [ [ ] ]
[ behead
' [ 2 3 5 7 ] witheach
[ dip dup join
0 over witheach +
dup 13 = iff
[ drop nested
dip rot join
unrot conclude ]
done
11 > iff drop done
nested swap dip join ]
drop dup [] = until ]
swap witheach
[ 0 swap witheach
[ swap 10 * + ]
number$ nested join ]
60 wrap$</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>
Line 1,967 ⟶ 2,255:
Unlucky numbers are:
[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|RPL}}==
Nice recursive solution from the discussion page:
{{works with|HP|49}}
« { } { "" }
'''DO''' { }
1 PICK3 SIZE '''FOR''' j
2 7 '''FOR''' d
OVER j GET d +
0
1 PICK3 SIZE '''FOR''' k
OVER k DUP SUB STR→ +
'''NEXT'''
'''CASE'''
DUP 13 == '''THEN''' DROP 4 ROLL SWAP + UNROT '''END'''
11 > '''THEN''' DROP '''END'''
+
'''END'''
d 2 ≠ 1 + '''STEP''' <span style="color:grey">@ generates 2 3 5 7 index sequence</span>
'''NEXT''' NIP
'''UNTIL''' DUP SIZE NOT '''END'''
DROP
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { 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>
Line 2,029 ⟶ 2,344:
[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|Tcl}}==
<syntaxhighlight lang="tcl">set res {}
set src [list {} 13]
while {[llength $src]} {
set src [lassign $src n r]
foreach d {2 3 5 7} {
if {$d >= $r} {
if {$d == $r} {lappend res "$n$d"}
break
}
lappend src "$n$d" [expr {$r - $d}]
}
}
puts $res</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|UNIX Shell}}==
<syntaxhighlight lang="sh">set -- '' 13
res=''
while [ $# -ne 0 ]
do
for d in 2 3 5 7
do
[ $d -ge $2 ] && {
[ $d -eq $2 ] && res=$res${res:+ }$1$d
break
}
set -- "$@" $1$d $(($2 - d))
done
shift 2
done
echo "$res"</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|Visual Basic .NET}}==
Line 2,082 ⟶ 2,433:
{{libheader|Wren-sort}}
As the only digits which are prime are [2, 3, 5, 7], it is clear that a number must have between 3 and 6 digits for them to sum to 13.
<syntaxhighlight lang="
import "./seq" for Lst
import "./sort" for Sort
var combrep // recursive
|