Numbers whose binary and ternary digit sums are prime: Difference between revisions

Added Easylang
(Add Factor)
(Added Easylang)
 
(74 intermediate revisions by 27 users not shown)
Line 1:
{{Draft task|Prime Numbers}}
[[Category:Prime Numbers]]
 
;Task:
;Task:Show numbers which binary and ternary digit sum are prime, where '''n < 200'''
Show positive integers &nbsp; '''n''' &nbsp; whose binary and ternary digits sum are prime, &nbsp; where &nbsp; '''n &nbsp; &lt; &nbsp; 200'''.
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
I a < 2 | a % 2 == 0
R 0B
L(i) (3 .. Int(sqrt(a))).step(2)
I a % i == 0
R 0B
R 1B
 
F digit_sum(=n, b)
V result = 0
L n != 0
result += n % b
n I/= b
R result
 
V count = 0
L(n) 2..199
I is_prime(digit_sum(n, 2)) &
is_prime(digit_sum(n, 3))
count++
print(‘#3’.format(n), end' I count % 16 == 0 {"\n"} E ‘ ’)
print()
print(‘Found ’count‘ numbers.’)</syntaxhighlight>
 
{{out}}
<pre>
5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35
36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87
91 93 97 103 107 109 115 117 121 127 129 131 133 137 143 145
151 155 157 162 167 171 173 179 181 185 191 193 199
Found 61 numbers.
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE Func IsPrime(INT i BYTE base BYTE ARRAY primes)
BYTE sum,d
 
sum=0
WHILE i#0
DO
d=i MOD base
sum==+d
i==/base
OD
RETURN (primes(sum))
 
PROC Main()
DEFINE MAX="199"
BYTE ARRAY primes(MAX+1)
INT i,count=[0]
 
Put(125) PutE() ;clear the screen
Sieve(primes,MAX+1)
FOR i=1 TO MAX
DO
IF IsPrime(i,2,primes)=1 AND IsPrime(i,3,primes)=1 THEN
PrintI(i) Put(32)
count==+1
FI
OD
PrintF("%E%EThere are %I numbers",count)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Numbers_which_binary_and_ternary_digit_sum_are_prime.png Screenshot from Atari 8-bit computer]
<pre>
5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87 91 93 97
103 107 109 115 117 121 127 129 131 133 137 143 14 5 151 155 157 162 167 171 173 179 181 185 191 193 199
 
There are 61 numbers
</pre>
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<syntaxhighlight lang="algol68">BEGIN # find numbers whose digit sums in binary and ternary are prime #
# returns the digit sum of n in base b #
PRIO DIGITSUM = 9;
OP DIGITSUM = ( INT n, b )INT:
BEGIN
INT d sum := 0;
INT v := ABS n;
WHILE v > 0 DO
d sum +:= v MOD b;
v OVERAB b
OD;
d sum
END # DIGITSUM # ;
INT max number = 200;
PR read "primes.incl.a68" PR
[]BOOL prime = PRIMESIEVE 200;
INT n count := 0;
FOR n TO UPB prime DO
INT d sum 2 = n DIGITSUM 2;
IF prime[ d sum 2 ] THEN
INT d sum 3 = n DIGITSUM 3;
IF prime[ d sum 3 ] THEN
# the base 2 and base 3 digit sums of n are both prime #
print( ( " ", whole( n, -3 ), IF prime[ n ] THEN "*" ELSE " " FI ) );
n count +:= 1;
IF n count MOD 14 = 0 THEN print( ( newline ) ) FI
FI
FI
OD;
print( ( newline ) );
print( ( "Found ", whole( n count, 0 ), " numbers whose binary and ternary digit sums are prime", newline ) );
print( ( " those that are themselves prime are suffixed with a ""*""", newline ) )
END</syntaxhighlight>
{{out}}
<pre>
5* 6 7* 10 11* 12 13* 17* 18 19* 21 25 28 31*
33 35 36 37* 41* 47* 49 55 59* 61* 65 67* 69 73*
79* 82 84 87 91 93 97* 103* 107* 109* 115 117 121 127*
129 131* 133 137* 143 145 151* 155 157* 162 167* 171 173* 179*
181* 185 191* 193* 199*
Found 61 numbers whose binary and ternary digit sums are prime
those that are themselves prime are suffixed with a "*"
</pre>
 
=={{header|ALGOL-M}}==
<syntaxhighlight lang="algolm">begin
integer function mod(a,b);
integer a,b;
mod := a-(a/b)*b;
 
integer function digitsum(n,base);
integer n,base;
digitsum := if n=0 then 0 else mod(n,base)+digitsum(n/base,base);
 
integer function isprime(n);
integer n;
begin
integer i;
isprime := 0;
if n < 2 then go to stop;
for i := 2 step 1 until n-1 do
begin
if mod(n,i) = 0 then go to stop;
end;
isprime := 1;
stop:
i := i;
end;
 
integer i,d2,d3,n;
n := 0;
for i := 0 step 1 until 199 do
begin
d2 := digitsum(i,2);
d3 := digitsum(i,3);
if isprime(d2) <> 0 and isprime(d3) <> 0 then
begin
if n/10 <> (n-1)/10 then write(i) else writeon(i);
n := n + 1;
end;
end;
end</syntaxhighlight>
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
21 25 28 31 33 35 36 37 41 47
49 55 59 61 65 67 69 73 79 82
84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155
157 162 167 171 173 179 181 185 191 193
199</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin % find numbers whose binary and ternary digit sums are prime %
% returns the digit sum of n in base b %
integer procedure digitSum( integer value n, base ) ;
begin
integer v, dSum;
v := abs n;
dSum := 0;
while v > 0 do begin
dSum := dSum + v rem base;
v := v div base
end while_v_gt_0 ;
dSum
end digitSum ;
integer MAX_PRIME, MAX_NUMBER;
MAX_PRIME := 199;
begin
logical array prime( 1 :: MAX_PRIME );
integer nCount;
% sieve the primes to MAX_PRIME %
prime( 1 ) := false; prime( 2 ) := true;
for i := 3 step 2 until MAX_PRIME do prime( i ) := true;
for i := 4 step 2 until MAX_PRIME do prime( i ) := false;
for i := 3 step 2 until truncate( sqrt( MAX_PRIME ) ) do begin
integer ii; ii := i + i;
if prime( i ) then for np := i * i step ii until MAX_PRIME do prime( np ) := false
end for_i ;
% find the numbers %
nCount := 0;
for i := 1 until MAX_PRIME do begin
if prime( digitSum( i, 2 ) ) and prime( digitSum( i, 3 ) ) then begin
% have another matching number %
writeon( i_w := 3, s_w := 0, " ", i );
nCount := nCount + 1;
if nCount rem 14 = 0 then write()
end if_have_a_suitable_number
end for_i ;
write( i_w := 1, s_w := 0, "Found ", nCount, " numbers with prime binary and ternary digit sums up to ", MAX_PRIME )
end
end.</syntaxhighlight>
{{out}}
<pre>
5 6 7 10 11 12 13 17 18 19 21 25 28 31
33 35 36 37 41 47 49 55 59 61 65 67 69 73
79 82 84 87 91 93 97 103 107 109 115 117 121 127
129 131 133 137 143 145 151 155 157 162 167 171 173 179
181 185 191 193 199
Found 61 numbers with prime binary and ternary digit sums up to 199
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">(⊢(/⍨)(∧/((2=0+.=⍳|⊢)¨2 3(+/⊥⍣¯1)¨⊢))¨) ⍳200</syntaxhighlight>
{{out}}
<pre>5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155 157 162 167 171 173 179 181 185 191 193 199</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">loop split.every: 10
select 1..199 'n [ and? prime? sum digits.base: 2 n
prime? sum digits.base: 3 n ] 'a ->
print map a => [pad to :string & 4]</syntaxhighlight>
 
{{out}}
 
<pre> 5 6 7 10 11 12 13 17 18 19
21 25 28 31 33 35 36 37 41 47
49 55 59 61 65 67 69 73 79 82
84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155
157 162 167 171 173 179 181 185 191 193
199</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f NUMBERS_WHICH_BINARY_AND_TERNARY_DIGIT_SUM_ARE_PRIME.AWK
# converted from C
BEGIN {
start = 0
stop = 199
for (i=start; i<=stop; i++) {
if (is_prime(sum_digits(i,2)) && is_prime(sum_digits(i,3))) {
printf("%4d%1s",i,++count%10?"":"\n")
}
}
printf("\nBinary and ternary digit sums are both prime %d-%d: %d\n",start,stop,count)
exit(0)
}
function sum_digits(n,base, sum) {
do {
sum += n % base
} while (n = int(n/base))
return(sum)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
5 6 7 10 11 12 13 17 18 19
21 25 28 31 33 35 36 37 41 47
49 55 59 61 65 67 69 73 79 82
84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155
157 162 167 171 173 179 181 185 191 193
199
Binary and ternary digit sums are both prime 0-199: 61
</pre>
 
=={{header|BASIC}}==
==={{header|Chipmunk Basic}}===
{{trans|GW-BASIC}}
<syntaxhighlight lang="basic">
100 rem Numbers which binary and ternary digit sum are prime
110 for n = 2 to 199
120 if is_prime(digit_sum(n,2)) <> 0 then
130 if is_prime(digit_sum(n,3)) <> 0 then print using "### ";n;
140 endif
150 next n
160 print
170 end
180 sub is_prime(p)
190 if p = 3 or p = 2 then
200 is_prime = 1
210 else
220 if p = 1 then
230 is_prime = 0
240 else
250 q = 1 : i = 1
260 i = i+1
270 if p mod i = 0 then q = 0
280 if i*i <= p and q <> 0 then 260
290 is_prime = q
300 endif
310 endif
320 end sub
330 sub digit_sum(n,bas)
340 p = 0 : xn = n
350 while xn <> 0
360 p = p+xn mod bas
370 xn = int(xn/bas)
380 wend
390 digit_sum = p
400 end sub
</syntaxhighlight>
{{out}}
<pre>
5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87 91 93 97 103 107 109 115 117 121 127 129 131 133 137 143 145 151 155 157 162 167 171 173 179 181 185 191 193 199
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">#include"isprime.bas"
 
function digsum( byval n as uinteger, b as const uinteger ) as uinteger
'finds the digit sum of n in base b
dim as uinteger sum = 0
while n
sum+=n mod b
n\=b
wend
return sum
end function
 
for n as uinteger = 1 to 200
if isprime(digsum(n,2)) and isprime(digsum(n,3)) then print n;" ";
next n : print</syntaxhighlight>
{{out}}<pre>5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87 91 93 97 103 107 109 115 117 121 127 129 131 133 137 143 145 151 155 157 162 167 171 173 179 181 185 191 193 199</pre>
 
==={{header|GW-BASIC}}===
{{works with|BASICA}}
<syntaxhighlight lang="gwbasic">10 REM Numbers which binary and ternary digit sum are prime
20 FOR N = 2 TO 199
30 B = 2
40 GOSUB 200: GOSUB 120
50 IF Q = 0 THEN GOTO 90
60 B = 3
70 GOSUB 200: GOSUB 120
80 IF Q = 1 THEN PRINT USING "### ";N;
90 NEXT N
100 PRINT
110 END
120 REM tests if a number is prime
130 IF P = 3 OR P = 2 THEN Q = 1: RETURN
140 IF P = 1 THEN Q = 0: RETURN
150 Q = 1: I = 1
160 I = I + 1
170 IF P MOD I = 0 THEN Q = 0
180 IF I * I <= P AND Q <> 0 THEN GOTO 160
190 RETURN
200 REM finds the digit sum of N in base B, returns P
210 P = 0: XN = N
220 WHILE XN <> 0
230 P = P + XN MOD B
240 XN = XN \ B
250 WEND
260 RETURN</syntaxhighlight>
{{out}}<pre> 5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87 91 93 97 103 107 109 115 117 121 127 129 131 133 137 143 145 151 155 157 162 167 171 173 179 181 185 191 193 199</pre>
 
'''Another solution'''
 
None of the digit sums are higher than 9, so the easiest thing to do is to hardcode which ones are prime.
{{works with|BASICA}}
<syntaxhighlight lang="gwbasic">10 REM Numbers which binary and ternary digit sum are prime
20 DEFINT I,J,K,P
30 DIM P(9): DATA 0,1,1,0,1,0,1,0,0
40 FOR I=1 TO 9: READ P(I): NEXT
50 FOR I=0 TO 199
60 J=0: K=I
70 IF K>0 THEN J=J+K MOD 2: K=K\2: GOTO 70 ELSE IF P(J)=0 THEN 100
80 J=0: K=I
90 IF K>0 THEN J=J+K MOD 3: K=K\3: GOTO 90 ELSE IF P(J) THEN PRINT I,
100 NEXT I
110 END</syntaxhighlight>
{{out}}
<pre> 5 6 7 10 11
12 13 17 18 19
21 25 28 31 33
35 36 37 41 47
49 55 59 61 65
67 69 73 79 82
84 87 91 93 97
103 107 109 115 117
121 127 129 131 133
137 143 145 151 155
157 162 167 171 173
179 181 185 191 193
199</pre>
 
==={{header|Tiny BASIC}}===
This isn't a very interesting problem. The most illustrative part of this solution is that it only uses four variables; several have multiple purposes. Efficiency is important when the language has only 26 variable names in total.
 
<syntaxhighlight lang="tinybasic"> REM B digital base input to sumdig, also output of primality routine
REM N input to sumdig routine
REM P input to primality routine, output of sumdig routine
REM T temp variable in sumdig routine, loop var in prime routine
LET N = 1
20 LET N = N + 1
LET B = 2
GOSUB 200
GOSUB 100
IF B = 0 THEN GOTO 30
LET B = 3
GOSUB 200
GOSUB 100
IF B = 1 THEN PRINT N
30 IF N < 200 THEN GOTO 20
END
 
100 REM PRIMALITY BY TRIAL DIVISION
LET B = 0
IF P = 1 THEN RETURN
LET B = 1
IF P = 2 THEN RETURN
LET T = 2
110 IF (P/T)*T = P THEN LET B = 0
IF B = 0 THEN RETURN
LET T = T + 1
IF T*T <= P THEN GOTO 110
RETURN
 
200 REM digital sum of N in base B
LET T = N
LET P = 0
210 IF T = 0 THEN RETURN
LET P = P + T - (T/B)*B
LET T = T/B
GOTO 210 </syntaxhighlight>
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
let digitsum(n, base) =
n=0 -> 0, n rem base + digitsum(n/base, base)
 
let isprime(n) = valof
$( if n<2 then resultis false
for i=2 to n-1 do
if n rem i = 0 then resultis false
resultis true
$)
 
let accept(n) =
isprime(digitsum(n,2)) & isprime(digitsum(n,3))
 
let start() be
$( let c = 0
for i=0 to 199 do
if accept(i) do
$( writef("%I4",i)
c := c + 1
if c rem 10 = 0 then wrch('*N')
$)
wrch('*N')
$)</syntaxhighlight>
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
21 25 28 31 33 35 36 37 41 47
49 55 59 61 65 67 69 73 79 82
84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155
157 162 167 171 173 179 181 185 191 193
199</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdint.h>
 
/* good enough for small numbers */
uint8_t prime(uint8_t n) {
uint8_t f;
if (n < 2) return 0;
for (f = 2; f < n; f++) {
if (n % f == 0) return 0;
}
return 1;
}
 
/* digit sum in given base */
uint8_t digit_sum(uint8_t n, uint8_t base) {
uint8_t s = 0;
do {s += n % base;} while (n /= base);
return s;
}
 
int main() {
uint8_t n, s = 0;
for (n = 0; n < 200; n++) {
if (prime(digit_sum(n,2)) && prime(digit_sum(n,3))) {
printf("%4d",n);
if (++s>=10) {
printf("\n");
s=0;
}
}
}
printf("\n");
return 0;
}</syntaxhighlight>
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
21 25 28 31 33 35 36 37 41 47
49 55 59 61 65 67 69 73 79 82
84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155
157 162 167 171 173 179 181 185 191 193
199</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">prime = proc (n: int) returns (bool)
if n<2 then return(false) end
for i: int in int$from_to(2, n-1) do
if n//i=0 then return(false) end
end
return(true)
end prime
 
digit_sum = proc (n, base: int) returns (int)
sum: int := 0
while n>0 do
sum := sum + n//base
n := n/base
end
return(sum)
end digit_sum
 
start_up = proc ()
po: stream := stream$primary_output()
n: int := 0
for i: int in int$from_to(2, 199) do
s2: int := digit_sum(i,2)
s3: int := digit_sum(i,3)
if prime(s2) cand prime(s3) then
stream$putright(po, int$unparse(i), 4)
n := n + 1
if n // 20 = 0 then stream$putl(po, "") end
end
end
end start_up</syntaxhighlight>
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47
49 55 59 61 65 67 69 73 79 82 84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155 157 162 167 171 173 179 181 185 191 193
199</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub prime(n: uint8): (p: uint8) is
p := 0;
if n >= 2 then
var f: uint8 := 2;
while f < n loop
if n % f == 0 then
return;
end if;
f := f + 1;
end loop;
p := 1;
end if;
end sub;
 
sub digit_sum(n: uint8, base: uint8): (sum: uint8) is
sum := 0;
while n > 0 loop
sum := sum + n % base;
n := n / base;
end loop;
end sub;
 
var n: uint8 := 0;
while n < 200 loop;
if prime(digit_sum(n,2)) != 0 and prime(digit_sum(n,3)) != 0 then
print_i8(n);
print_nl();
end if;
n := n + 1;
end loop;</syntaxhighlight>
{{out}}
<pre style='height:50ex;'>5
6
7
10
11
12
13
17
18
19
21
25
28
31
33
35
36
37
41
47
49
55
59
61
65
67
69
73
79
82
84
87
91
93
97
103
107
109
115
117
121
127
129
131
133
137
143
145
151
155
157
162
167
171
173
179
181
185
191
193
199</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;
 
 
function SumDigitsByBase(N,Base: integer): integer;
{Sum all digits of N in the specified B}
var I: integer;
begin
Result:=0;
repeat
begin
I:=N mod Base;
Result:=Result+I;
N:=N div Base;
end
until N = 0;
end;
 
function IsBinaryTernaryPrime(N: integer): boolean;
{Test if sums of binary and ternary digits is prime}
var Sum2,Sum3: integer;
begin
Result:=IsPrime(SumDigitsByBase(N,2)) and
IsPrime(SumDigitsByBase(N,3));
end;
 
procedure ShowBinaryTernaryPrimes(Memo: TMemo);
{Show the Binary-Ternary sum primes of first 200 values}
var I,Cnt: integer;
var S: string;
begin
S:=''; Cnt:=0;
for I:=0 to 200-1 do
if IsBinaryTernaryPrime(I) then
begin
Inc(Cnt);
S:=S+Format('%8D',[I]);
If (Cnt mod 5)=0 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('Count= '+IntToStr(Cnt));
end;
 
</syntaxhighlight>
{{out}}
<pre>
5 6 7 10 11
12 13 17 18 19
21 25 28 31 33
35 36 37 41 47
49 55 59 61 65
67 69 73 79 82
84 87 91 93 97
103 107 109 115 117
121 127 129 131 133
137 143 145 151 155
157 162 167 171 173
179 181 185 191 193
199
Count= 61
Elapsed Time: 3.322 ms.
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
if num < 2
return 0
.
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func digsum n b .
while n > 0
sum += n mod b
n = n div b
.
return sum
.
for i = 1 to 199
if isprim digsum i 2 = 1 and isprim digsum i 3 = 1
write i & " "
.
.
</syntaxhighlight>
{{out}}
<pre>
5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87 91 93 97 103 107 109 115 117 121 127 129 131 133 137 143 145 151 155 157 162 167 171 173 179 181 185 191 193 199
</pre>
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<syntaxhighlight lang="fsharp">
// binary and ternary digit sums are prime: Nigel Galloway. April 16th., 2021
let fN2,fN3=let rec fG n g=function l when l<n->l+g |l->fG n (g+l%n)(l/n) in (fG 2 0, fG 3 0)
{0..200}|>Seq.filter(fun n->isPrime(fN2 n) && isPrime(fN3 n))|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87 91 93 97 103 107 109 115 117 121 127 129 131 133 137 143 145 151 155 157 162 167 171 173 179 181 185 191 193 199
Real: 00:00:00.005
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: combinators combinators.short-circuit formatting io lists
lists.lazy math math.parser math.primes sequences ;
 
Line 27 ⟶ 823:
 
"Base 10 Base 2 (sum) Base 3 (sum)" print
l23primes [ 200 < ] lwhile [ 23prime. ] leach</langsyntaxhighlight>
{{out}}
<pre style="height:24em">
Line 92 ⟶ 888:
193 11000001 3 21011 5
199 11000111 5 21101 5
</pre>
 
=={{header|Fermat}}==
<syntaxhighlight lang="fermat">Function Digsum(n, b) =
digsum := 0;
while n>0 do
digsum := digsum + n|b;
n:=n\b;
od;
digsum.;
 
for p=1 to 200 do
if Isprime(Digsum(p,3)) and Isprime(Digsum(p,2)) then
!(p,' ');
nadd := nadd+1;
fi od;</syntaxhighlight>
{{out}}<pre>5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87 91 93 97 103 107 109 115 117 121 127 129 131 133 137 143 145 151 155 157 162 167 171 173 179 181 185 191 193 199</pre>
 
=={{header|FOCAL}}==
<syntaxhighlight lang="focal">01.10 S P(2)=1;S P(3)=1;S P(5)=1;S P(7)=1
01.20 S V=10
01.30 F N=0,199;D 3
01.40 T !
01.50 Q
 
02.10 S A=0
02.20 S M=N
02.30 S T=FITR(M/B)
02.40 S A=A+M-T*B
02.50 S M=T
02.60 I (-M)2.3
 
03.10 S B=2;D 2;S X=A
03.20 S B=3;D 2;S Y=A
03.30 I (-P(X)*P(Y))3.4;R
03.40 T %4,N
03.50 S V=V-1
03.60 I (-V)3.7;T !;S V=10
03.70 R</syntaxhighlight>
{{out}}
<pre>= 5= 6= 7= 10= 11= 12= 13= 17= 18= 19
= 21= 25= 28= 31= 33= 35= 36= 37= 41= 47
= 49= 55= 59= 61= 65= 67= 69= 73= 79= 82
= 84= 87= 91= 93= 97= 103= 107= 109= 115= 117
= 121= 127= 129= 131= 133= 137= 143= 145= 151= 155
= 157= 162= 167= 171= 173= 179= 181= 185= 191= 193
= 199</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Numbers_which_binary_and_ternary_digit_sum_are_prime}}
 
'''Solution'''
 
[[File:Fōrmulæ - Numbers which binary and ternary digit sum are prime 01.png]]
 
[[File:Fōrmulæ - Numbers which binary and ternary digit sum are prime 02.png]]
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"rcu"
)
 
func main() {
var numbers []int
for i := 2; i < 200; i++ {
bds := rcu.DigitSum(i, 2)
if rcu.IsPrime(bds) {
tds := rcu.DigitSum(i, 3)
if rcu.IsPrime(tds) {
numbers = append(numbers, i)
}
}
}
fmt.Println("Numbers < 200 whose binary and ternary digit sums are prime:")
for i, n := range numbers {
fmt.Printf("%4d", n)
if (i+1)%14 == 0 {
fmt.Println()
}
}
fmt.Printf("\n\n%d such numbers found\n", len(numbers))
}</syntaxhighlight>
 
{{out}}
<pre>
Numbers < 200 whose binary and ternary digit sums are prime:
5 6 7 10 11 12 13 17 18 19 21 25 28 31
33 35 36 37 41 47 49 55 59 61 65 67 69 73
79 82 84 87 91 93 97 103 107 109 115 117 121 127
129 131 133 137 143 145 151 155 157 162 167 171 173 179
181 185 191 193 199
 
61 such numbers found
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.Bifunctor (first)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (isPrime)
 
--------- BINARY AND TERNARY DIGIT SUMS BOTH PRIME -------
 
digitSumsPrime :: Int -> [Int] -> Bool
digitSumsPrime n = all (isPrime . digitSum n)
 
digitSum :: Int -> Int -> Int
digitSum n base = go n
where
go 0 = 0
go n = uncurry (+) (first go $ quotRem n base)
 
--------------------------- TEST -------------------------
main :: IO ()
main =
putStrLn $
show (length xs)
<> " matches in [1..199]\n\n"
<> table xs
where
xs =
[1 .. 199]
>>= \x -> [show x | digitSumsPrime x [2, 3]]
 
------------------------- DISPLAY -----------------------
 
table :: [String] -> String
table xs =
let w = length (last xs)
in unlines $
unwords
<$> chunksOf
10
(justifyRight w ' ' <$> xs)
 
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</syntaxhighlight>
<pre>61 matches in [1..199]
 
5 6 7 10 11 12 13 17 18 19
21 25 28 31 33 35 36 37 41 47
49 55 59 61 65 67 69 73 79 82
84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155
157 162 167 171 173 179 181 185 191 193
199</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">((1*./@p:2 3+/@(#.^:_1)"0])"0#]) i.200</syntaxhighlight>
{{out}}
<pre>5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87 91 93 97 103 107 109 115 117 121 127 129 131 133 137 143 145 151 155 157 162 167 171 173 179 181 185 191 193 199</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq'''
<syntaxhighlight lang=jq>
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def sum(s): reduce s as $_ (0; . + $_);
 
def is_prime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
elif ($n % 5 == 0) then $n == 5
elif ($n % 7 == 0) then $n == 7
elif ($n % 11 == 0) then $n == 11
elif ($n % 13 == 0) then $n == 13
elif ($n % 17 == 0) then $n == 17
elif ($n % 19 == 0) then $n == 19
else 23
| until( (. * .) > $n or ($n % . == 0); .+2)
| . * . > $n
end;
 
def digitSum($base):
def stream:
recurse(if . > 0 then ./$base|floor else empty end) | . % $base ;
sum(stream);
def digit_sums_are_prime($n):
[range(2;$n)
| digitSum(2) as $bds
| select($bds|is_prime)
| digitSum(3) as $tds
| select($tds|is_prime) ];
 
def task($n):
"Numbers < \($n) whose binary and ternary digit sums are prime:",
(digit_sums_are_prime($n)
| length as $length
| (_nwise(14) | map(lpad(4)) | join(" ")),
"\nFound \($length) such numbers." );
 
task(200)
</syntaxhighlight>
{{output}}
<pre>
Numbers < 200 whose binary and ternary digit sums are prime:
5 6 7 10 11 12 13 17 18 19 21 25 28 31
33 35 36 37 41 47 49 55 59 61 65 67 69 73
79 82 84 87 91 93 97 103 107 109 115 117 121 127
129 131 133 137 143 145 151 155 157 162 167 171 173 179
181 185 191 193 199
 
Found 61 such numbers.
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Primes
 
btsumsareprime(n) = isprime(sum(digits(n, base=2))) && isprime(sum(digits(n, base=3)))
 
foreach(p -> print(rpad(p[2], 4), p[1] % 20 == 0 ? "\n" : ""), enumerate(filter(btsumsareprime, 1:199)))
</syntaxhighlight>{{out}}
<pre>
5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47
49 55 59 61 65 67 69 73 79 82 84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155 157 162 167 171 173 179 181 185 191 193
199
</pre>
 
=={{header|MAD}}==
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(P)
ENTRY TO PRIME.
WHENEVER P.L.2, FUNCTION RETURN 0B
THROUGH TEST, FOR DV=2, 1, DV.G.SQRT.(P)
TEST WHENEVER P-P/DV*DV.E.0, FUNCTION RETURN 0B
FUNCTION RETURN 1B
END OF FUNCTION
INTERNAL FUNCTION(N,BASE)
ENTRY TO DGTSUM.
SUM = 0
DN = N
DIGIT NX = DN/BASE
SUM = SUM + DN-NX*BASE
DN = NX
WHENEVER DN.G.0, TRANSFER TO DIGIT
FUNCTION RETURN SUM
END OF FUNCTION
THROUGH NBR, FOR I=0, 1, I.GE.200
WHENEVER PRIME.(DGTSUM.(I,2)) .AND. PRIME.(DGTSUM.(I,3))
PRINT FORMAT FMT, I
END OF CONDITIONAL
NBR CONTINUE
 
VECTOR VALUES FMT = $I3*$
END OF PROGRAM </syntaxhighlight>
{{out}}
<pre style='height:50ex;'> 5
6
7
10
11
12
13
17
18
19
21
25
28
31
33
35
36
37
41
47
49
55
59
61
65
67
69
73
79
82
84
87
91
93
97
103
107
109
115
117
121
127
129
131
133
137
143
145
151
155
157
162
167
171
173
179
181
185
191
193
199</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Partition[
Select[
Range@
200, (PrimeQ[Total@IntegerDigits[#, 2]] &&
PrimeQ[Total@IntegerDigits[#, 3]]) &], UpTo[8]] // TableForm</syntaxhighlight>
 
{{out}}<pre>
5 6 7 10 11 12 13 17
18 19 21 25 28 31 33 35
36 37 41 47 49 55 59 61
65 67 69 73 79 82 84 87
91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145
151 155 157 162 167 171 173 179
181 185 191 193 199
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import strutils
 
func isPrime(n: Positive): bool =
if n == 1: return false
if n mod 2 == 0: return n == 2
if n mod 3 == 0: return n == 3
var d = 5
while d * d <= n:
if n mod d == 0: return false
inc d, 2
if n mod d == 0: return false
inc d, 4
return true
 
func digitSum(n, b: Natural): int =
var n = n
while n != 0:
result += n mod b
n = n div b
 
var count = 0
for n in 2..<200:
if digitSum(n, 2).isPrime and digitSum(n, 3).isPrime:
inc count
stdout.write ($n).align(3), if count mod 16 == 0: '\n' else: ' '
echo()
echo "Found ", count, " numbers."</syntaxhighlight>
 
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35
36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87
91 93 97 103 107 109 115 117 121 127 129 131 133 137 143 145
151 155 157 162 167 171 173 179 181 185 191 193 199
Found 61 numbers.</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
use List::Util 'sum';
use ntheory <is_prime todigitstring>;
 
sub test_digits { 0 != is_prime sum split '', todigitstring(shift, shift) }
 
my @p;
test_digits($_,2) and test_digits($_,3) and push @p, $_ for 1..199;
say my $result = @p . " matching numbers:\n" . (sprintf "@{['%4d' x @p]}", @p) =~ s/(.{40})/$1\n/gr;</syntaxhighlight>
{{out}}
<pre>61 matching numbers:
5 6 7 10 11 12 13 17 18 19
21 25 28 31 33 35 36 37 41 47
49 55 59 61 65 67 69 73 79 82
84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155
157 162 167 171 173 179 181 185 191 193
199</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">result</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</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>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">result</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">prime23</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)))</span>
<span style="color: #008080;">and</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">199</span><span style="color: #0000FF;">),</span><span style="color: #000000;">prime23</span><span style="color: #0000FF;">)</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;">"%d numbers found: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
61 numbers found: {5,6,7,10,11,"...",181,185,191,193,199}
</pre>
 
=={{Header|PL/I}}==
 
See [[#Polyglot:PL/I and PL/M]]
 
=={{header|PL/M}}==
<syntaxhighlight lang="pli">100H:
/* CP/M CALLS */
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
 
/* PRINT NUMBER */
PRINT$NUMBER: PROCEDURE (N);
DECLARE S (8) BYTE INITIAL ('.....',13,10,'$');
DECLARE (N, P) ADDRESS, C BASED P BYTE;
P = .S(5);
DIGIT:
P = P - 1;
C = N MOD 10 + '0';
N = N / 10;
IF N > 0 THEN GO TO DIGIT;
CALL PRINT(P);
END PRINT$NUMBER;
 
/* SIMPLE PRIMALITY TEST */
PRIME: PROCEDURE (N) BYTE;
DECLARE (N, I) BYTE;
IF N < 2 THEN RETURN 0;
DO I=2 TO N-1;
IF N MOD I = 0 THEN RETURN 0;
END;
RETURN 1;
END PRIME;
 
/* SUM OF DIGITS */
DIGIT$SUM: PROCEDURE (N, BASE) BYTE;
DECLARE (N, BASE, SUM) BYTE;
SUM = 0;
DO WHILE N > 0;
SUM = SUM + N MOD BASE;
N = N / BASE;
END;
RETURN SUM;
END DIGIT$SUM;
 
/* TEST NUMBERS 0 .. 199 */
DECLARE I BYTE;
DO I=0 TO 199;
IF PRIME(DIGIT$SUM(I,2)) AND PRIME(DIGIT$SUM(I,3)) THEN
CALL PRINT$NUMBER(I);
END;
 
CALL EXIT;
EOF</syntaxhighlight>
{{out}}
<pre style='height:50ex;'>5
6
7
10
11
12
13
17
18
19
21
25
28
31
33
35
36
37
41
47
49
55
59
61
65
67
69
73
79
82
84
87
91
93
97
103
107
109
115
117
121
127
129
131
133
137
143
145
151
155
157
162
167
171
173
179
181
185
191
193
199</pre>
 
See also [[#Polyglot:PL/I and PL/M]]
 
=={{header|Plain English}}==
<syntaxhighlight lang="plainenglish">To run:
Start up.
Loop.
If a counter is past 200, break.
If the counter has prime digit sums in binary and ternary, write the counter then " " on the console without advancing.
Repeat.
Wait for the escape key.
Shut down.
 
A sum is a number.
 
A base is a number.
 
To find a digit sum of a number given a base:
Privatize the number.
Loop.
Divide the number by the base giving a quotient and a remainder.
Add the remainder to the digit sum.
Put the quotient into the number.
If the number is 0, exit.
Repeat.
 
To decide if a number has prime digit sums in binary and ternary:
Find a digit sum of the number given 2.
If the digit sum is not prime, say no.
Find another digit sum of the number given 3.
If the other digit sum is not prime, say no.
Say yes.</syntaxhighlight>
{{out}}
<pre>
5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87 91 93 97 103 107 109 115 117 121 127 129 131 133 137 143 145 151 155 157 162 167 171 173 179 181 185 191 193 199
</pre>
 
=={{header|Polyglot:PL/I and PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
Should work with many PL/I implementations.
<br>
The PL/I include file "pg.inc" can be found on the [[Polyglot:PL/I and PL/M]] page.
Note the use of text in column 81 onwards to hide the PL/I specifics from the PL/M compiler.
<syntaxhighlight lang="pli">/* FIND NUMBERS WHOSE DIGIT SUM SQUARED AND CUBED IS PRIME */
prime_digit_sums_100H: procedure options (main);
 
/* PL/I DEFINITIONS */
%include 'pg.inc';
/* PL/M DEFINITIONS: CP/M BDOS SYSTEM CALL AND CONSOLE I/O ROUTINES, ETC. */ /*
DECLARE BINARY LITERALLY 'ADDRESS', CHARACTER LITERALLY 'BYTE';
DECLARE FIXED LITERALLY ' ', BIT LITERALLY 'BYTE';
DECLARE STATIC LITERALLY ' ', RETURNS LITERALLY ' ';
DECLARE FALSE LITERALLY '0', TRUE LITERALLY '1';
DECLARE HBOUND LITERALLY 'LAST', SADDR LITERALLY '.';
BDOSF: PROCEDURE( FN, ARG )BYTE;
DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PRCHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PRSTRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PRNL: PROCEDURE; CALL PRCHAR( 0DH ); CALL PRCHAR( 0AH ); END;
PRNUMBER: PROCEDURE( N );
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR( 6 ) BYTE, W BYTE;
N$STR( W := LAST( N$STR ) ) = '$';
N$STR( W := W - 1 ) = '0' + ( ( V := N ) MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL BDOS( 9, .N$STR( W ) );
END PRNUMBER;
MODF: PROCEDURE( A, B )ADDRESS;
DECLARE ( A, B )ADDRESS;
RETURN( A MOD B );
END MODF;
/* END LANGUAGE DEFINITIONS */
 
/* TASK */
 
DIGITSUM: PROCEDURE( N, BASE )RETURNS /* RETURNS THE DIGIT SUM OF N */ (
FIXED BINARY /* IN THE SPECIFIED BASE */ )
;
DECLARE ( N, BASE ) FIXED BINARY;
DECLARE ( SUM, V ) FIXED BINARY;
SUM = MODF( N, BASE );
V = N / BASE;
DO WHILE( V > 0 );
SUM = SUM + MODF( V, BASE );
V = V / BASE;
END;
RETURN( SUM );
END DIGITSUM ;
 
ISPRIME: PROCEDURE( N )RETURNS /* RETURNS TRUE IF N IS PRIME, */ (
BIT /* FALSE OTHERWISE */ )
;
DECLARE N FIXED BINARY;
DECLARE I FIXED BINARY;
DECLARE RESULT BIT;
IF N < 2 THEN RESULT = FALSE;
ELSE IF N = 2 THEN RESULT = TRUE;
ELSE IF MODF( N, 2 ) = 0 THEN RESULT = FALSE;
ELSE DO;
RESULT = TRUE;
I = 3;
DO WHILE( RESULT & /*
AND /* */ ( I * I ) <= N );
RESULT = MODF( N, I ) > 0;
I = I + 2;
END;
END;
RETURN( RESULT );
END ISPRIME ;
 
DECLARE ( I, PCOUNT ) FIXED BINARY;
 
PCOUNT = 0;
DO I = 1 TO 199;
IF ISPRIME( DIGITSUM( I, 2 ) ) THEN DO;
IF ISPRIME( DIGITSUM( I, 3 ) ) THEN DO;
CALL PRCHAR( ' ' );
IF I < 10 THEN CALL PRCHAR( ' ' );
IF I < 100 THEN CALL PRCHAR( ' ' );
CALL PRNUMBER( I );
PCOUNT = PCOUNT + 1;
IF PCOUNT > 9 THEN DO;
PCOUNT = 0;
CALL PRNL;
END;
END;
END;
END;
 
EOF: end prime_digit_sums_100H;</syntaxhighlight>
{{out}}
<pre>
5 6 7 10 11 12 13 17 18 19
21 25 28 31 33 35 36 37 41 47
49 55 59 61 65 67 69 73 79 82
84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155
157 162 167 171 173 179 181 185 191 193
199
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">'''Binary and Ternary digit sums both prime'''
 
 
# digitSumsPrime :: Int -> [Int] -> Bool
def digitSumsPrime(n):
'''True if the digits of n in each
given base have prime sums.
'''
def go(bases):
return all(
isPrime(digitSum(b)(n))
for b in bases
)
return go
 
 
# digitSum :: Int -> Int -> Int
def digitSum(base):
'''The sum of the digits of n in a given base.
'''
def go(n):
q, r = divmod(n, base)
return go(q) + r if n else 0
return go
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Matching integers in the range [1..199]'''
xs = [
str(n) for n in range(1, 200)
if digitSumsPrime(n)([2, 3])
]
print(f'{len(xs)} matches in [1..199]\n')
print(table(10)(xs))
 
 
# ----------------------- GENERIC ------------------------
 
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n, subdividing the
contents of xs. Where the length of xs is not evenly
divible, the final list will be shorter than n.
'''
def go(xs):
return (
xs[i:n + i] for i in range(0, len(xs), n)
) if 0 < n else None
return go
 
 
# isPrime :: Int -> Bool
def isPrime(n):
'''True if n is prime.'''
if n in (2, 3):
return True
if 2 > n or 0 == n % 2:
return False
if 9 > n:
return True
if 0 == n % 3:
return False
 
def p(x):
return 0 == n % x or 0 == n % (2 + x)
 
return not any(map(p, range(5, 1 + int(n ** 0.5), 6)))
 
 
# table :: Int -> [String] -> String
def table(n):
'''A list of strings formatted as
rows of n (right justified) columns.
'''
def go(xs):
w = len(xs[-1])
return '\n'.join(
' '.join(row) for row in chunksOf(n)([
s.rjust(w, ' ') for s in xs
])
)
return go
 
 
# MAIN ---
if __name__ == '__main__':
main()
</syntaxhighlight>
<pre>61 matches in [1..199]
 
5 6 7 10 11 12 13 17 18 19
21 25 28 31 33 35 36 37 41 47
49 55 59 61 65 67 69 73 79 82
84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155
157 162 167 171 173 179 181 185 191 193
199</pre>
 
=={{header|Quackery}}==
 
<code>digitsum</code> is defined at [[Sum digits of an integer#Quackery]].
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<syntaxhighlight lang="Quackery"> []
200 times
[ i^ 3 digitsum isprime while
i^ 2 digitsum isprime while
i^ join ]
echo</syntaxhighlight>
 
{{out}}
 
<pre>[ 5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47 49 55 59 61 65 67 69 73 79 82 84 87 91 93 97 103 107 109 115 117 121 127 129 131 133 137 143 145 151 155 157 162 167 171 173 179 181 185 191 193 199 ]</pre>
 
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" line>say (^200).grep(-> $n {all (2,3).map({$n.base($_).comb.sum.is-prime}) }).batch(10)».fmt('%3d').join: "\n";</syntaxhighlight>
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
21 25 28 31 33 35 36 37 41 47
49 55 59 61 65 67 69 73 79 82
84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155
157 162 167 171 173 179 181 185 191 193
199</pre>
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program finds and displays integers whose base 2 and base 3 digit sums are prime.*/
parse arg n cols . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 200 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
call genP /*build array of semaphores for primes.*/
w= 10 /*width of a number in any column. */
title= ' positive integers whose binary and ternary digit sums are prime, N < ' commas(n)
if cols>0 then say ' index │'center(title, 1 + cols*(w+1) ) /*maybe show title.*/
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') /*maybe show sep. */
found= 0; idx= 1 /*initialize # of finds and the index. */
$= /*a list of numbers found (so far). */
do j=1 for n-1 /*find #s whose B2 & B3 sums are prime.*/
b2= sumDig( tBase(j, 2) ); if \!.b2 then iterate /*convert to base2, sum digits.*/ /* ◄■■■■■■■■ a filter. */
b3= sumDig( tBase(j, 3) ); if \!.b3 then iterate /* " " base3 " " */ /* ◄■■■■■■■■ a filter. */
found= found + 1 /*bump the number of found integers. */
if cols<1 then iterate /*Only showing the summary? Then skip.*/
$= $ right( commas(j), w) /*add a commatized integer ───► $ list.*/
if found//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*j*/
 
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') /*show foot sep ? */
say
say 'Found ' commas(found) title /*show summary. */
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 ?
sumDig: procedure; parse arg x 1 s 2;do j=2 for length(x)-1;s=s+substr(x,j,1);end;return s
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: @= 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103
!.=0; do p=1 for words(@); _= word(@, p); !._= 1; end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
tBase: procedure; parse arg x,toBase; y=; $= 0123456789
do while x>=toBase; y= substr($, x//toBase+1, 1)y; x= x % toBase
end /*while*/
return substr($, x+1, 1)y</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
index │ positive integers whose binary and ternary digit sums are prime, N < 200
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 5 6 7 10 11 12 13 17 18 19
11 │ 21 25 28 31 33 35 36 37 41 47
21 │ 49 55 59 61 65 67 69 73 79 82
31 │ 84 87 91 93 97 103 107 109 115 117
41 │ 121 127 129 131 133 137 143 145 151 155
51 │ 157 162 167 171 173 179 181 185 191 193
61 │ 199
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────
 
Found 61 positive integers whose binary and ternary digit sums are prime, N < 200
</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
see "working..." + nl
see "Numbers < 200 whose binary and ternary digit sums are prime:" + nl
 
decList = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
baseList = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
 
num = 0
limit = 200
 
Line 117 ⟶ 1,781:
next
if isprime(sumBin) and isprime(sumTer)
num = num + 1
see "{" + n + "," + strBin + ":" + sumBin + "," + strTer + ":" + sumTer + "}" + nl
see "" + num + ". {" + n + "," + strBin + ":" + sumBin + "," + strTer + ":" + sumTer + "}" + nl
ok
next
 
see "Found " + num + " such numbers" + nl
see "done..." + nl
 
Line 138 ⟶ 1,804:
binList = substr(binList,nl,"")
return binList
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:24em">
<pre>
working...
Numbers < 200 whose binary and ternary digit sums are prime:
{5,101:2,12:3}
1. {65,110101:2,2012:23}
2. {76,111110:32,2120:32}
3. {107,1010111:23,10121:23}
4. {1110,10111010:32,102101:32}
5. {1211,11001011:23,110102:23}
6. {1312,11011100:32,111110:32}
7. {1713,100011101:23,122111:53}
8. {1817,1001010001:2,200122:25}
9. {1918,1001110010:32,201200:32}
10. {2119,1010110011:3,210201:3}
11. {2521,1100110101:3,221210:53}
12. {2825,1110011001:3,1001221:25}
13. {3128,1111111100:53,10111001:32}
14. {3331,10000111111:25,10201011:3}
15. {3533,100011100001:32,10221020:53}
16. {3635,100100100011:23,11001022:25}
17. {3736,100101100100:32,11011100:32}
18. {4137,101001100101:3,11121101:53}
19. {4741,101111101001:53,12021112:5}
20. {4947,110001101111:35,12111202:5}
21. {5549,110111110001:53,20011211:35}
22. {5955,111011110111:5,20122001:53}
23. {6159,111101111011:5,20212012:5}
24. {6561,1000001111101:25,21022021:5}
25. {6765,10000111000001:32,21112102:5}
26. {6967,10001011000011:3,21202111:5}
27. {7369,10010011000101:3,22012120:5}
28. {7973,10011111001001:53,22212201:75}
29. {8279,10100101001111:35,100012221:27}
30. {8482,10101001010010:3,1001010001:2}
31. {8784,10101111010100:53,1002010010:32}
32. {9187,10110111010111:5,1010110020:3}
33. {9391,10111011011011:5,1011010101:3}
34. {9793,11000011011101:35,1012110110:53}
35. {10397,11001111100001:53,1021110121:5}
36. {107103,11010111100111:5,1022210211:75}
37. {109107,11011011101011:5,1100110222:37}
38. {115109,11100111101101:5,1102111001:53}
39. {117115,11101011110011:5,1110011021:35}
40. {121117,11110011110101:5,1111111100:53}
41. {127121,11111111111001:75,1120111111:5}
42. {129127,100000011111111:27,1121011201:5}
43. {131129,1000001110000001:32,1121211210:75}
44. {133131,1000010110000011:3,1122111212:7}
45. {137133,1000100110000101:3,1200211221:57}
46. {143137,1000111110001001:53,1202212002:75}
47. {145143,1001000110001111:35,1210112022:57}
48. {151145,1001011110010001:53,1212112101:75}
49. {155151,1001101110010111:5,1220212121:7}
50. {157155,1001110110011011:5,1221112202:7}
51. {162157,1010001010011101:35,2000012211:27}
52. {167162,1010011110100010:53,2001220000:52}
53. {171167,1010101110100111:5,2010020012:35}
54. {173171,1010110110101011:5,2010220100:53}
55. {179173,1011001110101101:5,2012220102:75}
56. {181179,1011010110110011:5,2020120122:57}
57. {185181,1011100110110101:5,2021220201:75}
58. {191185,1011111110111001:75,2100220212:57}
59. {193191,1100000110111111:37,2101121002:5}
60. {199193,1100011111000001:53,2110121011:5}
61. {199,11000111:5,21101:5}
Found 61 such numbers
done...
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
require 'prime'
 
p (1..200).select{|n| [2, 3].all?{|base| n.digits(base).sum.prime?} }</syntaxhighlight>
{{out}}
<pre>
[5, 6, 7, 10, 11, 12, 13, 17, 18, 19, 21, 25, 28, 31, 33, 35, 36, 37, 41, 47, 49, 55, 59, 61, 65, 67, 69, 73, 79, 82, 84, 87, 91, 93, 97, 103, 107, 109, 115, 117, 121, 127, 129, 131, 133, 137, 143, 145, 151, 155, 157, 162, 167, 171, 173, 179, 181, 185, 191, 193, 199]
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">1..^200 -> grep {|n| [2,3].all { n.sumdigits(_).is_prime } }</syntaxhighlight>
{{out}}
<pre>
[5, 6, 7, 10, 11, 12, 13, 17, 18, 19, 21, 25, 28, 31, 33, 35, 36, 37, 41, 47, 49, 55, 59, 61, 65, 67, 69, 73, 79, 82, 84, 87, 91, 93, 97, 103, 107, 109, 115, 117, 121, 127, 129, 131, 133, 137, 143, 145, 151, 155, 157, 162, 167, 171, 173, 179, 181, 185, 191, 193, 199]
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Int
import "./fmt" for Fmt
 
var numbers = []
for (i in 2..199) {
var bds = Int.digitSum(i, 2)
if (Int.isPrime(bds)) {
var tds = Int.digitSum(i, 3)
if (Int.isPrime(tds)) numbers.add(i)
}
}
System.print("Numbers < 200 whose binary and ternary digit sums are prime:")
Fmt.tprint("$4d", numbers, 14)
System.print("\nFound %(numbers.count) such numbers.")</syntaxhighlight>
 
{{out}}
<pre>
Numbers < 200 whose binary and ternary digit sums are prime:
5 6 7 10 11 12 13 17 18 19 21 25 28 31
33 35 36 37 41 47 49 55 59 61 65 67 69 73
79 82 84 87 91 93 97 103 107 109 115 117 121 127
129 131 133 137 143 145 151 155 157 162 167 171 173 179
181 185 191 193 199
 
Found 61 such numbers.
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
for I:= 2 to sqrt(N) do
if rem(N/I) = 0 then return false;
return true;
];
 
func SumDigits(N, Base); \Return sum of digits in N for Base
int N, Base, Sum;
[Sum:= 0;
repeat N:= N/Base;
Sum:= Sum + rem(0);
until N=0;
return Sum;
];
 
int Count, N;
[Count:= 0;
for N:= 0 to 200-1 do
if IsPrime(SumDigits(N,2)) & IsPrime(SumDigits(N,3)) then
[IntOut(0, N);
Count:= Count+1;
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
];
CrLf(0);
IntOut(0, Count);
Text(0, " such numbers found below 200.
");
]</syntaxhighlight>
 
{{out}}
<pre>
5 6 7 10 11 12 13 17 18 19
21 25 28 31 33 35 36 37 41 47
49 55 59 61 65 67 69 73 79 82
84 87 91 93 97 103 107 109 115 117
121 127 129 131 133 137 143 145 151 155
157 162 167 171 173 179 181 185 191 193
199
61 such numbers found below 200.
</pre>
2,041

edits