Numbers with prime digits whose sum is 13: Difference between revisions
Content added Content deleted
(→{{header|jq}}: add analytic solution) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 6: | Line 6: | ||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="11l">F primeDigitsSum13(=n) |
||
V sum = 0 |
V sum = 0 |
||
L n > 0 |
L n > 0 |
||
Line 23: | Line 23: | ||
c = 0 |
c = 0 |
||
print() |
print() |
||
print()</ |
print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 34: | Line 34: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">DEFINE PRIMECOUNT="4" |
||
BYTE ARRAY primedigits(PRIMECOUNT)=[2 3 5 7] |
BYTE ARRAY primedigits(PRIMECOUNT)=[2 3 5 7] |
||
Line 103: | Line 103: | ||
FI |
FI |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Numbers_with_prime_digits_whose_sum_is_13.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Numbers_with_prime_digits_whose_sum_is_13.png Screenshot from Atari 8-bit computer] |
||
Line 114: | Line 114: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Based on the Algol W sample. |
Based on the Algol W sample. |
||
< |
<syntaxhighlight lang="algol68">BEGIN |
||
# find numbers whose digits are prime and whose digit sum is 13 # |
# find numbers whose digits are prime and whose digit sum is 13 # |
||
# as noted by the Wren sample, the digits can only be 2, 3, 5, 7 # |
# as noted by the Wren sample, the digits can only be 2, 3, 5, 7 # |
||
Line 156: | Line 156: | ||
OD # d2 # |
OD # d2 # |
||
OD # d1 # |
OD # d1 # |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 167: | Line 167: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
Uses the observations about the digits and numbers in the Wren solution to generate the sequence. |
Uses the observations about the digits and numbers in the Wren solution to generate the sequence. |
||
< |
<syntaxhighlight lang="algolw">begin |
||
% find numbers whose digits are prime and whose digit sum is 13 % |
% find numbers whose digits are prime and whose digit sum is 13 % |
||
% as noted by the Wren sample, the digits can only be 2, 3, 5, 7 % |
% as noted by the Wren sample, the digits can only be 2, 3, 5, 7 % |
||
Line 201: | Line 201: | ||
end for_d2 |
end for_d2 |
||
end for_d1 |
end for_d1 |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 212: | Line 212: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">pDigits: [2 3 5 7] |
||
lst: map pDigits 'd -> @[d] |
lst: map pDigits 'd -> @[d] |
||
Line 234: | Line 234: | ||
loop split.every: 10 result 'a -> |
loop split.every: 10 result 'a -> |
||
print map a => [pad to :string & 6]</ |
print map a => [pad to :string & 6]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 245: | Line 245: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<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 |
||
BEGIN { |
BEGIN { |
||
Line 276: | Line 276: | ||
return(sum == 13) |
return(sum == 13) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 288: | Line 288: | ||
=={{header|C}}== |
=={{header|C}}== |
||
Brute force |
Brute force |
||
< |
<syntaxhighlight lang="c">#include <stdbool.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 327: | Line 327: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533 |
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533 |
||
Line 337: | Line 337: | ||
{{Trans|Phix}} |
{{Trans|Phix}} |
||
Same recursive method. |
Same recursive method. |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using static System.Console; |
using static System.Console; |
||
using LI = System.Collections.Generic.SortedSet<int>; |
using LI = System.Collections.Generic.SortedSet<int>; |
||
Line 351: | Line 351: | ||
static void Main(string[] args) { WriteLine(string.Join(" ", |
static void Main(string[] args) { WriteLine(string.Join(" ", |
||
unl(new LI {}, new LI { 2, 3, 5, 7 }, 13))); } |
unl(new LI {}, new LI { 2, 3, 5, 7 }, 13))); } |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="white-space:pre-wrap;">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 style="white-space:pre-wrap;">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 |
||
Line 357: | Line 357: | ||
=== Alternate === |
=== Alternate === |
||
Based in '''Nigel Galloway's''' suggestion from the discussion page. |
Based in '''Nigel Galloway's''' suggestion from the discussion page. |
||
< |
<syntaxhighlight lang="csharp">class Program { |
||
static void Main(string[] args) { int[] lst; int sum; |
static void Main(string[] args) { int[] lst; int sum; |
||
Line 367: | Line 367: | ||
else if (sum < 12) |
else if (sum < 12) |
||
w.Add((i.digs * 10 + j, sum)); } } |
w.Add((i.digs * 10 + j, sum)); } } |
||
}</ |
}</syntaxhighlight> |
||
Same output. |
Same output. |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|C#}}(the alternate version) |
{{trans|C#}}(the alternate version) |
||
< |
<syntaxhighlight lang="cpp">#include <cstdio> |
||
#include <vector> |
#include <vector> |
||
#include <bits/stdc++.h> |
#include <bits/stdc++.h> |
||
Line 385: | Line 385: | ||
printf("%d%d ", get<0>(i), x); |
printf("%d%d ", get<0>(i), x); |
||
else if (sum < 12) w.push_back({get<0>(i) * 10 + x, sum}); } |
else if (sum < 12) w.push_back({get<0>(i) * 10 + x, sum}); } |
||
return 0; }</ |
return 0; }</syntaxhighlight> |
||
Same output as C#. |
Same output as C#. |
||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="d">import std.stdio; |
||
bool primeDigitsSum13(int n) { |
bool primeDigitsSum13(int n) { |
||
Line 421: | Line 421: | ||
} |
} |
||
writeln; |
writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533 |
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533 |
||
Line 429: | Line 429: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// prime digits whose sum is 13. Nigel Galloway: October 21st., 2020 |
// prime digits whose sum is 13. Nigel Galloway: October 21st., 2020 |
||
let rec fN g=let g=[for n in [2;3;5;7] do for g in g->n::g]|>List.groupBy(fun n->match List.sum n with 13->'n' |n when n<12->'g' |_->'x')|>Map.ofSeq |
let rec fN g=let g=[for n in [2;3;5;7] do for g in g->n::g]|>List.groupBy(fun n->match List.sum n with 13->'n' |n when n<12->'g' |_->'x')|>Map.ofSeq |
||
[yield! (if g.ContainsKey 'n' then g.['n'] else []); yield! (if g.ContainsKey 'g' then fN g.['g'] else [])] |
[yield! (if g.ContainsKey 'n' then g.['n'] else []); yield! (if g.ContainsKey 'g' then fN g.['g'] else [])] |
||
fN [[]] |> Seq.iter(fun n->n|>List.iter(printf "%d");printf " ");printfn "" |
fN [[]] |> Seq.iter(fun n->n|>List.iter(printf "%d");printf " ");printfn "" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 443: | Line 443: | ||
===Filtering selections=== |
===Filtering selections=== |
||
Generate all selections of the prime digits in the only possible lengths whose sum can be 13, then filter for sums that equal 13. |
Generate all selections of the prime digits in the only possible lengths whose sum can be 13, then filter for sums that equal 13. |
||
< |
<syntaxhighlight lang="factor">USING: formatting io kernel math math.combinatorics |
||
math.functions math.ranges sequences sequences.extras ; |
math.functions math.ranges sequences sequences.extras ; |
||
Line 450: | Line 450: | ||
"Numbers whose digits are prime and sum to 13:" print |
"Numbers whose digits are prime and sum to 13:" print |
||
{ 2 3 5 7 } 3 6 [a,b] [ selections [ sum 13 = ] filter ] with |
{ 2 3 5 7 } 3 6 [a,b] [ selections [ sum 13 = ] filter ] with |
||
map-concat [ digits>number ] map "%[%d, %]\n" printf</ |
map-concat [ digits>number ] map "%[%d, %]\n" printf</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 458: | Line 458: | ||
===F# translation=== |
===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. |
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. |
||
< |
<syntaxhighlight lang="factor">USING: io kernel math prettyprint sequences sequences.extras ; |
||
{ } { { 2 } { 3 } { 5 } { 7 } } [ |
{ } { { 2 } { 3 } { 5 } { 7 } } [ |
||
{ 2 3 5 7 } [ suffix ] cartesian-map concat |
{ 2 3 5 7 } [ suffix ] cartesian-map concat |
||
[ sum 13 = ] partition [ append ] dip [ sum 11 > ] reject |
[ sum 13 = ] partition [ append ] dip [ sum 11 > ] reject |
||
] until-empty [ bl ] [ [ pprint ] each ] interleave nl</ |
] until-empty [ bl ] [ [ pprint ] each ] interleave nl</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 472: | Line 472: | ||
Ho hum. Another prime digits task. |
Ho hum. Another prime digits task. |
||
< |
<syntaxhighlight lang="freebasic"> |
||
function digit_is_prime( n as integer ) as boolean |
function digit_is_prime( n as integer ) as boolean |
||
select case n |
select case n |
||
Line 502: | Line 502: | ||
for i as uinteger = 1 to 322222 |
for i as uinteger = 1 to 322222 |
||
if all_digits_prime(i) andalso digit_sum_13(i) then print i, |
if all_digits_prime(i) andalso digit_sum_13(i) then print i, |
||
next i</ |
next i</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 517: | Line 517: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
Reuses code from some other tasks. |
Reuses code from some other tasks. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 590: | Line 590: | ||
fmt.Println("Those numbers whose digits are all prime and sum to 13 are:") |
fmt.Println("Those numbers whose digits are all prime and sum to 13 are:") |
||
fmt.Println(res2) |
fmt.Println(res2) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 599: | Line 599: | ||
===only counting=== |
===only counting=== |
||
See Julia [http://rosettacode.org/wiki/Numbers_with_prime_digits_whose_sum_is_13#Julia] |
See Julia [http://rosettacode.org/wiki/Numbers_with_prime_digits_whose_sum_is_13#Julia] |
||
<syntaxhighlight lang="go"> |
|||
<lang go> |
|||
package main |
package main |
||
Line 688: | Line 688: | ||
fmt.Println("The count of numbers whose digits are all prime and sum to",mySum,"is",gblCount) |
fmt.Println("The count of numbers whose digits are all prime and sum to",mySum,"is",gblCount) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 725: | Line 725: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
As an unfold, in the recursive pattern described by Nigel Galloway on the Talk page. |
As an unfold, in the recursive pattern described by Nigel Galloway on the Talk page. |
||
< |
<syntaxhighlight lang="haskell">import Data.List.Split (chunksOf) |
||
import Data.List (intercalate, transpose, unfoldr) |
import Data.List (intercalate, transpose, unfoldr) |
||
import Text.Printf |
import Text.Printf |
||
Line 774: | Line 774: | ||
unDigits :: [Int] -> Int |
unDigits :: [Int] -> Int |
||
unDigits = foldl ((+) . (10 *)) 0</ |
unDigits = foldl ((+) . (10 *)) 0</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>43 numbers with prime digits summing to 13: |
<pre>43 numbers with prime digits summing to 13: |
||
Line 786: | Line 786: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="java">public class PrimeDigits { |
||
private static boolean primeDigitsSum13(int n) { |
private static boolean primeDigitsSum13(int n) { |
||
int sum = 0; |
int sum = 0; |
||
Line 814: | Line 814: | ||
System.out.println(); |
System.out.println(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533 |
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533 |
||
Line 823: | Line 823: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
As an unfold, in the recursive pattern described by Nigel Galloway on the Talk page. |
As an unfold, in the recursive pattern described by Nigel Galloway on the Talk page. |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 1,003: | Line 1,003: | ||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>337,355,373,535,553,733 |
<pre>337,355,373,535,553,733 |
||
Line 1,027: | Line 1,027: | ||
====Simple Generate-and-Test Solution==== |
====Simple Generate-and-Test Solution==== |
||
< |
<syntaxhighlight lang="jq"># Output: a stream |
||
def simple: |
def simple: |
||
range(2; 7) as $n |
range(2; 7) as $n |
||
Line 1,035: | Line 1,035: | ||
| join("") | tonumber; |
| join("") | tonumber; |
||
count(simple)</ |
count(simple)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,041: | Line 1,041: | ||
</pre> |
</pre> |
||
====A Faster Solution==== |
====A Faster Solution==== |
||
< |
<syntaxhighlight lang="jq">def faster: |
||
def digits: [2, 3, 5, 7]; |
def digits: [2, 3, 5, 7]; |
||
def wide($max): |
def wide($max): |
||
Line 1,056: | Line 1,056: | ||
count(faster) |
count(faster) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,064: | Line 1,064: | ||
As indicated above, this third solution is analytical (combinatoric), |
As indicated above, this third solution is analytical (combinatoric), |
||
and requires gojq for accuracy for relatively large target sums. |
and requires gojq for accuracy for relatively large target sums. |
||
< |
<syntaxhighlight lang="jq"># Input should be a sorted array of distinct positive integers |
||
# Output is a stream of distinct arrays, each of which is sorted, and each sum of which is $sum |
# Output is a stream of distinct arrays, each of which is sorted, and each sum of which is $sum |
||
def sorted_combinations($sum): |
def sorted_combinations($sum): |
||
Line 1,096: | Line 1,096: | ||
number_of_interesting_numbers(13), |
number_of_interesting_numbers(13), |
||
number_of_interesting_numbers(199)</ |
number_of_interesting_numbers(199)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,104: | Line 1,104: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Combinatorics, Primes |
||
function primedigitsums(targetsum) |
function primedigitsums(targetsum) |
||
Line 1,151: | Line 1,151: | ||
foreach(countprimedigitsums, nextprimes(17, 40)) |
foreach(countprimedigitsums, nextprimes(17, 40)) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
There are 3 prime-digit-only numbers summing to 5 : [5, 32, 23] |
There are 3 prime-digit-only numbers summing to 5 : [5, 32, 23] |
||
Line 1,201: | Line 1,201: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="scala">fun primeDigitsSum13(n: Int): Boolean { |
||
var nn = n |
var nn = n |
||
var sum = 0 |
var sum = 0 |
||
Line 1,228: | Line 1,228: | ||
} |
} |
||
println() |
println() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533 |
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533 |
||
Line 1,237: | Line 1,237: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="lua">function prime_digits_sum_13(n) |
||
local sum = 0 |
local sum = 0 |
||
while n > 0 do |
while n > 0 do |
||
Line 1,262: | Line 1,262: | ||
end |
end |
||
end |
end |
||
print()</ |
print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533 |
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533 |
||
Line 1,270: | Line 1,270: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import math, sequtils, strutils |
||
type Digit = 0..9 |
type Digit = 0..9 |
||
Line 1,297: | Line 1,297: | ||
for i, n in result: |
for i, n in result: |
||
stdout.write ($n).align(6), if (i + 1) mod 9 == 0: '\n' else: ' ' |
stdout.write ($n).align(6), if (i + 1) mod 9 == 0: '\n' else: ' ' |
||
echo()</ |
echo()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,308: | Line 1,308: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{Works with|Free Pascal}} Only counting.<BR>Extreme fast in finding the sum of primesdigits = value.<BR>Limited by Uint64 |
{{Works with|Free Pascal}} Only counting.<BR>Extreme fast in finding the sum of primesdigits = value.<BR>Limited by Uint64 |
||
< |
<syntaxhighlight lang="pascal">program PrimSumUpTo13; |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
{$MODE DELPHI} |
{$MODE DELPHI} |
||
Line 1,451: | Line 1,451: | ||
writeln(num:6,gblCount:25,' '); |
writeln(num:6,gblCount:25,' '); |
||
until num > MAXNUM; |
until num > MAXNUM; |
||
END.</ |
END.</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> Sum Count of arrangements |
<pre> Sum Count of arrangements |
||
Line 1,487: | Line 1,487: | ||
</pre> |
</pre> |
||
===using gmp=== |
===using gmp=== |
||
< |
<syntaxhighlight lang="pascal">program PrimSumUpTo13_GMP; |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
{$OPTIMIZATION ON,ALL} |
{$OPTIMIZATION ON,ALL} |
||
Line 1,702: | Line 1,702: | ||
z_clear(gblSum); |
z_clear(gblSum); |
||
z_clear(gbldelta); |
z_clear(gbldelta); |
||
END.</ |
END.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,756: | Line 1,756: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; |
use strict; |
||
Line 1,775: | Line 1,775: | ||
} |
} |
||
} |
} |
||
print $numbers =~ s/.{1,80}\K /\n/gr;</ |
print $numbers =~ s/.{1,80}\K /\n/gr;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,784: | Line 1,784: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">unlucky</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">needed</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">={})</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">unlucky</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">needed</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">={})</span> |
||
Line 1,799: | Line 1,799: | ||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unlucky</span><span style="color: #0000FF;">({</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span><span style="color: #000000;">13</span><span style="color: #0000FF;">))</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unlucky</span><span style="color: #0000FF;">({</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span><span style="color: #000000;">13</span><span style="color: #0000FF;">))</span> |
||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span> |
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,809: | Line 1,809: | ||
===iterative=== |
===iterative=== |
||
Queue-based version of Nigel's recursive algorithm, same output. |
Queue-based version of Nigel's recursive algorithm, same output. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- uses latest apply() mods, rest is fine</span> |
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- uses latest apply() mods, rest is fine</span> |
||
Line 1,831: | Line 1,831: | ||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%6d"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span> |
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%6d"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span> |
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
I've archived a slightly more OTT version: [[Numbers_with_prime_digits_whose_sum_is_13/Phix]]. |
I've archived a slightly more OTT version: [[Numbers_with_prime_digits_whose_sum_is_13/Phix]]. |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
<syntaxhighlight lang="prolog"> |
|||
<lang Prolog> |
|||
digit_sum(N, M) :- digit_sum(N, 0, M). |
digit_sum(N, M) :- digit_sum(N, 0, M). |
||
digit_sum(0, A, B) :- !, A = B. |
digit_sum(0, A, B) :- !, A = B. |
||
Line 1,861: | Line 1,861: | ||
?- main. |
?- main. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,869: | Line 1,869: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>put join ', ', sort +*, unique flat |
||
< 2 2 2 2 2 3 3 3 5 5 7 >.combinations |
< 2 2 2 2 2 3 3 3 5 5 7 >.combinations |
||
.grep( *.sum == 13 ) |
.grep( *.sum == 13 ) |
||
.map( { .join => $_ } ) |
.map( { .join => $_ } ) |
||
.map: { .value.permutations».join }</ |
.map: { .value.permutations».join }</syntaxhighlight> |
||
{{Out}} |
{{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> |
<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|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm finds and displays all decimal numbers whose digits are prime and sum to 13. */ |
||
parse arg LO HI COLS . /*obtain optional arguments from the CL*/ |
parse arg LO HI COLS . /*obtain optional arguments from the CL*/ |
||
if LO=='' | LO=="," then LO= 337 /*Not specified? Then use the default.*/ |
if LO=='' | LO=="," then LO= 337 /*Not specified? Then use the default.*/ |
||
Line 1,912: | Line 1,912: | ||
exit 0 /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
commas: parse arg _; do jc=length(_)-3 to 1 by -3; _=insert(',', _, jc); end; return _</ |
commas: parse arg _; do jc=length(_)-3 to 1 by -3; _=insert(',', _, jc); end; return _</syntaxhighlight> |
||
{{out|output|text= when using the internal default inputs:}} |
{{out|output|text= when using the internal default inputs:}} |
||
<pre> |
<pre> |
||
Line 1,928: | Line 1,928: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
Line 1,962: | Line 1,962: | ||
next |
next |
||
? "[" + left(svect, len(svect) - 1) + "]" |
? "[" + left(svect, len(svect) - 1) + "]" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,971: | Line 1,971: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="ruby">def primeDigitsSum13(n) |
||
sum = 0 |
sum = 0 |
||
while n > 0 |
while n > 0 |
||
Line 1,997: | Line 1,997: | ||
end |
end |
||
print "\n" |
print "\n" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533 |
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533 |
||
Line 2,005: | Line 2,005: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func generate_from_prefix(sum, p, base, digits) { |
||
var seq = [p] |
var seq = [p] |
||
Line 2,024: | Line 2,024: | ||
} |
} |
||
say numbers_with_digitsum(13)</ |
say numbers_with_digitsum(13)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,033: | Line 2,033: | ||
{{Trans|Phix}} |
{{Trans|Phix}} |
||
Same recursive method. |
Same recursive method. |
||
< |
<syntaxhighlight lang="vbnet">Imports System |
||
Imports System.Console |
Imports System.Console |
||
Imports LI = System.Collections.Generic.SortedSet(Of Integer) |
Imports LI = System.Collections.Generic.SortedSet(Of Integer) |
||
Line 2,053: | Line 2,053: | ||
unl(new LI From {}, new LI From { 2, 3, 5, 7 }, 13))) |
unl(new LI From {}, new LI From { 2, 3, 5, 7 }, 13))) |
||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="white-space:pre-wrap;">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 style="white-space:pre-wrap;">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 |
||
Line 2,059: | Line 2,059: | ||
=== Alternate === |
=== Alternate === |
||
Thanks to '''Nigel Galloway's''' suggestion from the discussion page. |
Thanks to '''Nigel Galloway's''' suggestion from the discussion page. |
||
< |
<syntaxhighlight lang="vbnet">Imports Tu = System.Tuple(Of Integer, Integer) |
||
Module Module1 |
Module Module1 |
||
Line 2,074: | Line 2,074: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
Same output. |
Same output. |
||
Line 2,082: | Line 2,082: | ||
{{libheader|Wren-sort}} |
{{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. |
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="ecmascript">import "/math" for Nums |
||
import "/seq" for Lst |
import "/seq" for Lst |
||
import "/sort" for Sort |
import "/sort" for Sort |
||
Line 2,128: | Line 2,128: | ||
Sort.quick(res) |
Sort.quick(res) |
||
System.print("Those numbers whose digits are all prime and sum to 13 are:") |
System.print("Those numbers whose digits are all prime and sum to 13 are:") |
||
System.print(res)</ |
System.print(res)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,137: | Line 2,137: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
<syntaxhighlight lang="xpl0"> |
|||
<lang XPL0> |
|||
int N, M, S, D; |
int N, M, S, D; |
||
[for N:= 2 to 322222 do |
[for N:= 2 to 322222 do |
||
Line 2,152: | Line 2,152: | ||
until M=0; \all digits in N tested or digit not prime |
until M=0; \all digits in N tested or digit not prime |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |