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

Dialects of BASIC moved to the BASIC section.
m (syntax highlighting fixup automation)
(Dialects of BASIC moved to the BASIC section.)
Line 296:
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
None of the digit sums are higher than 9, so the easiest thing to do
<syntaxhighlight lang="freebasic">#include"isprime.bas"
is to hardcode which ones are prime.
 
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">5 REM Numbers which binary and ternary digit sum are prime
10 FOR N = 2 TO 200
20 B = 2
30 GOSUB 220 : GOSUB 110
40 IF Q = 0 THEN GOTO 80
50 B = 3
60 GOSUB 220: GOSUB 110
70 IF Q = 1 THEN PRINT N;" ";
80 NEXT N
90 PRINT
100 END
110 REM tests if a number is prime
120 Q=0
130 IF P=3 THEN Q=1:RETURN
140 IF P=1 THEN Q=0:RETURN
150 IF P=2 THEN Q=1:RETURN
160 I=1
170 I=I+1
180 IF INT(P/I)*I = P THEN RETURN
190 IF I*I<=P THEN GOTO 170
200 Q = 1
210 RETURN
220 REM finds the digit sum of N in base B, returns P
230 P = 0
240 XN = N
250 IF XN = 0 THEN RETURN
260 P = P + XN MOD B
270 XN = XN\B
280 GOTO 250</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'''
<syntaxhighlight lang="basic">10 DEFINT I,J,K,P
None of the digit sums are higher than 9, so the easiest thing to do is to hardcode which ones are prime.
20 DIM P(9): DATA 0,1,1,0,1,0,1,0,0
{{works with|BASICA}}
30 FOR I=1 TO 9: READ P(I): NEXT
<syntaxhighlight lang="gwbasic">10 REM Numbers which binary and ternary digit sum are prime
40 FOR I=0 TO 199
20 DEFINT I,J,K,P
50 J=0: K=I
30 DIM P(9): DATA 0,1,1,0,1,0,1,0,0
60 IF K>0 THEN J=J+K MOD 2: K=K\2: GOTO 60 ELSE IF P(J)=0 THEN 90
40 FOR I=1 TO 9: READ P(I): NEXT
70 J=0: K=I
50 FOR I=0 TO 199
80 IF K>0 THEN J=J+K MOD 3: K=K\3: GOTO 80 ELSE IF P(J) THEN PRINT I,
60 J=0: K=I
90 NEXT I</syntaxhighlight>
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
Line 322 ⟶ 375:
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}}==
Line 689 ⟶ 784:
 
In '''[https://formulae.org/?example=Numbers_which_binary_and_ternary_digit_sum_are_prime this]''' page you can see the program(s) related to this task and their results.
 
=={{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|Go}}==
Line 750 ⟶ 827:
61 such numbers found
</pre>
 
=={{header|GW-BASIC}}==
<syntaxhighlight lang="gwbasic">10 FOR N = 2 TO 200
20 B = 2
30 GOSUB 220 : GOSUB 110
40 IF Q = 0 THEN GOTO 80
50 B = 3
60 GOSUB 220: GOSUB 110
70 IF Q = 1 THEN PRINT N;" ";
80 NEXT N
90 PRINT
100 END
110 REM tests if a number is prime
120 Q=0
130 IF P=3 THEN Q=1:RETURN
140 IF P=1 THEN Q=0:RETURN
150 IF P=2 THEN Q=1:RETURN
160 I=1
170 I=I+1
180 IF INT(P/I)*I = P THEN RETURN
190 IF I*I<=P THEN GOTO 170
200 Q = 1
210 RETURN
220 REM finds the digit sum of N in base B, returns P
230 P = 0
240 XN = N
250 IF XN = 0 THEN RETURN
260 P = P + XN MOD B
270 XN = XN\B
280 GOTO 250</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|Haskell}}==
Line 1,598 ⟶ 1,644:
[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|Wren}}==
512

edits