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

m
→‎{{header|Wren}}: More minor changes.
(added AWK)
m (→‎{{header|Wren}}: More minor changes.)
(35 intermediate revisions by 17 users not shown)
Line 2:
 
;Task:
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}}
<lang algol68>BEGIN # find numbers whose digit sums in binary and ternary are prime #
<syntaxhighlight lang="algol68">BEGIN # find numbers whose digit sums in binary and ternary are prime #
# reurns a sieve of primes up to n #
PROC sieve = ( INT n )[]BOOL:
BEGIN
[ 1 : n ]BOOL p;
p[ 1 ] := FALSE; p[ 2 ] := TRUE;
FOR i FROM 3 BY 2 TO n DO p[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO n DO p[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( n ) DO
IF p[ i ] THEN FOR s FROM i * i BY i + i TO n DO p[ s ] := FALSE OD FI
OD;
p
END # prime list # ;
# returns the digit sum of n in base b #
PRIO DIGITSUM = 9;
Line 32 ⟶ 100:
END # DIGITSUM # ;
INT max number = 200;
PR read "primes.incl.a68" PR
[]BOOL prime = sieve( max number );
[]BOOL prime = PRIMESIEVE 200;
INT n count := 0;
FOR n TO maxUPB numberprime DO
INT d sum 2 = n DIGITSUM 2;
IF prime[ d sum 2 ] THEN
Line 49 ⟶ 118:
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</langsyntaxhighlight>
{{out}}
<pre>
Line 62 ⟶ 131:
 
=={{header|ALGOL-M}}==
<langsyntaxhighlight lang="algolm">begin
integer function mod(a,b);
integer a,b;
Line 98 ⟶ 167:
end;
end;
end</langsyntaxhighlight>
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
Line 109 ⟶ 178:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight 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 ) ;
Line 147 ⟶ 216:
write( i_w := 1, s_w := 0, "Found ", nCount, " numbers with prime binary and ternary digit sums up to ", MAX_PRIME )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 160 ⟶ 229:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">(⊢(/⍨)(∧/((2=0+.=⍳|⊢)¨2 3(+/⊥⍣¯1)¨⊢))¨) ⍳200</langsyntaxhighlight>
{{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
Line 167 ⟶ 236:
=={{header|Arturo}}==
 
<langsyntaxhighlight 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]</langsyntaxhighlight>
 
{{out}}
Line 182 ⟶ 251:
199</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NUMBERS_WHICH_BINARY_AND_TERNARY_DIGIT_SUM_ARE_PRIME.AWK
# converted from C
Line 213 ⟶ 282:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 227 ⟶ 296:
 
=={{header|BASIC}}==
==={{header|Chipmunk Basic}}===
None of the digit sums are higher than 9, so the easiest thing to do
{{trans|GW-BASIC}}
is to hardcode which ones are prime.
<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.
<lang BASIC>10 DEFINT I,J,K,P
{{works with|BASICA}}
20 DIM P(9): DATA 0,1,1,0,1,0,1,0,0
<syntaxhighlight lang="gwbasic">10 REM Numbers which binary and ternary digit sum are prime
30 FOR I=1 TO 9: READ P(I): NEXT
20 DEFINT I,J,K,P
40 FOR I=0 TO 199
30 DIM P(9): DATA 0,1,1,0,1,0,1,0,0
50 J=0: K=I
40 FOR I=1 TO 9: READ P(I): NEXT
60 IF K>0 THEN J=J+K MOD 2: K=K\2: GOTO 60 ELSE IF P(J)=0 THEN 90
7050 JFOR I=0: K=ITO 199
60 J=0: K=I
80 IF K>0 THEN J=J+K MOD 3: K=K\3: GOTO 80 ELSE IF P(J) THEN PRINT I,
70 IF K>0 THEN J=J+K MOD 2: K=K\2: GOTO 70 ELSE IF P(J)=0 THEN 100
90 NEXT I</lang>
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
Line 253 ⟶ 413:
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}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let digitsum(n, base) =
Line 279 ⟶ 481:
$)
wrch('*N')
$)</langsyntaxhighlight>
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
Line 290 ⟶ 492:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdint.h>
 
Line 323 ⟶ 525:
printf("\n");
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
Line 331 ⟶ 533:
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}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub prime(n: uint8): (p: uint8) is
Line 365 ⟶ 604:
end if;
n := n + 1;
end loop;</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>5
Line 428 ⟶ 667:
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|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight 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>
</lang>
{{out}}
<pre>
Line 444 ⟶ 771:
=={{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 463 ⟶ 790:
 
"Base 10 Base 2 (sum) Base 3 (sum)" print
l23primes [ 200 < ] lwhile [ 23prime. ] leach</langsyntaxhighlight>
{{out}}
<pre style="height:24em">
Line 529 ⟶ 856:
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}}==
<langsyntaxhighlight 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
Line 550 ⟶ 893:
03.50 S V=V-1
03.60 I (-V)3.7;T !;S V=10
03.70 R</langsyntaxhighlight>
{{out}}
<pre>= 5= 6= 7= 10= 11= 12= 13= 17= 18= 19
Line 559 ⟶ 902:
= 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}}==
<langsyntaxhighlight lang="haskell">import Data.Bifunctor (first)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (isPrime)
Line 600 ⟶ 996:
 
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</langsyntaxhighlight>
<pre>61 matches in [1..199]
 
Line 612 ⟶ 1,008:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j">((1*./@p:2 3+/@(#.^:_1)"0])"0#]) i.200</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight 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)))
</langsyntaxhighlight>{{out}}
<pre>
5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47
Line 631 ⟶ 1,085:
 
=={{header|MAD}}==
<langsyntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(P)
Line 659 ⟶ 1,113:
 
VECTOR VALUES FMT = $I3*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'> 5
Line 722 ⟶ 1,176:
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}}==
<langsyntaxhighlight Nimlang="nim">import strutils
 
func isPrime(n: Positive): bool =
Line 750 ⟶ 1,221:
stdout.write ($n).align(3), if count mod 16 == 0: '\n' else: ' '
echo()
echo "Found ", count, " numbers."</langsyntaxhighlight>
 
{{out}}
Line 761 ⟶ 1,232:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 771 ⟶ 1,242:
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;</langsyntaxhighlight>
{{out}}
<pre>61 matching numbers:
Line 781 ⟶ 1,252:
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}}==
<langsyntaxhighlight plmlang="pli">100H:
/* CP/M CALLS */
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
Line 831 ⟶ 1,331:
 
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>5
Line 894 ⟶ 1,394:
193
199</pre>
 
See also [[#Polyglot:PL/I and PL/M]]
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Loop.
Line 923 ⟶ 1,425:
Find another digit sum of the number given 3.
If the other digit sum is not prime, say no.
Say yes.</langsyntaxhighlight>
{{out}}
<pre>
Line 929 ⟶ 1,431:
</pre>
 
=={{header|PhixPolyglot:PL/I and PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<!--<lang Phix>(phixonline)-->
Should work with many PL/I implementations.
<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>
<br>
<span style="color: #004080;">string</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
The PL/I include file "pg.inc" can be found on the [[Polyglot:PL/I and PL/M]] page.
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
Note the use of text in column 81 onwards to hide the PL/I specifics from the PL/M compiler.
<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>
<syntaxhighlight lang="pli">/* FIND NUMBERS WHOSE DIGIT SUM SQUARED AND CUBED IS PRIME */
<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>
prime_digit_sums_100H: procedure options (main);
<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>
/* PL/I DEFINITIONS */
<span style="color: #008080;">return</span> <span style="color: #000000;">result</span>
%include 'pg.inc';
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
/* PL/M DEFINITIONS: CP/M BDOS SYSTEM CALL AND CONSOLE I/O ROUTINES, ETC. */ /*
DECLARE BINARY LITERALLY 'ADDRESS', CHARACTER LITERALLY 'BYTE';
<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>
DECLARE FIXED LITERALLY ' ', BIT LITERALLY 'BYTE';
<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>
DECLARE STATIC LITERALLY ' ', RETURNS LITERALLY ' ';
<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>
DECLARE FALSE LITERALLY '0', TRUE LITERALLY '1';
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
DECLARE HBOUND LITERALLY 'LAST', SADDR LITERALLY '.';
BDOSF: PROCEDURE( FN, ARG )BYTE;
<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>
DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
<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>
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
<!--</lang>-->
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
61 numbers found: {5,6,7,10,11,"...",181,185,191,193,199}
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}}==
<langsyntaxhighlight lang="python">'''Binary and Ternary digit sums both prime'''
 
 
Line 1,044 ⟶ 1,628:
if __name__ == '__main__':
main()
</syntaxhighlight>
</lang>
<pre>61 matches in [1..199]
 
Line 1,054 ⟶ 1,638:
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" perl6line>say (^200).grep(-> $n {all (2,3).map({$n.base($_).comb.sum.is-prime}) }).batch(10)».fmt('%3d').join: "\n";</langsyntaxhighlight>
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
Line 1,068 ⟶ 1,669:
 
=={{header|REXX}}==
<langsyntaxhighlight 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.*/
Line 1,080 ⟶ 1,681:
$= /*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.*/
Line 1,105 ⟶ 1,706:
do while x>=toBase; y= substr($, x//toBase+1, 1)y; x= x % toBase
end /*while*/
return substr($, x+1, 1)y</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,123 ⟶ 1,724:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,170 ⟶ 1,771:
binList = substr(binList,nl,"")
return binList
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:24em">
Line 1,238 ⟶ 1,839:
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>
 
Line 1,243 ⟶ 1,861:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Int
{{libheader|Wren-seq}}
<lang ecmascript>import "./mathfmt" for IntFmt
import "/fmt" for Fmt
import "/seq" for Lst
 
var numbers = []
Line 1,257 ⟶ 1,873:
}
System.print("Numbers < 200 whose binary and ternary digit sums are prime:")
Fmt.tprint("$4d", numbers, 14)
for (chunk in Lst.chunks(numbers, 14)) Fmt.print("$4d", chunk)
System.print("\nFound %(numbers.count) such numbers.")</langsyntaxhighlight>
 
{{out}}
Line 1,270 ⟶ 1,886:
 
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>
9,476

edits