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

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 8:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
Line 32:
print(‘#3’.format(n), end' I count % 16 == 0 {"\n"} E ‘ ’)
print()
print(‘Found ’count‘ numbers.’)</langsyntaxhighlight>
 
{{out}}
Line 45:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE Func IsPrime(INT i BYTE base BYTE ARRAY primes)
Line 74:
OD
PrintF("%E%EThere are %I numbers",count)
RETURN</langsyntaxhighlight>
{{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]
Line 86:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight 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;
Line 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 131:
 
=={{header|ALGOL-M}}==
<langsyntaxhighlight lang="algolm">begin
integer function mod(a,b);
integer a,b;
Line 167:
end;
end;
end</langsyntaxhighlight>
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
Line 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 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 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 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 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 282:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 299:
is to hardcode which ones are prime.
 
<langsyntaxhighlight BASIClang="basic">10 DEFINT I,J,K,P
20 DIM P(9): DATA 0,1,1,0,1,0,1,0,0
30 FOR I=1 TO 9: READ P(I): NEXT
Line 307:
70 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,
90 NEXT I</langsyntaxhighlight>
{{out}}
<pre> 5 6 7 10 11
Line 324:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let digitsum(n, base) =
Line 348:
$)
wrch('*N')
$)</langsyntaxhighlight>
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
Line 359:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdint.h>
 
Line 392:
printf("\n");
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
Line 403:
 
=={{header|CLU}}==
<langsyntaxhighlight 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
Line 432:
end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19 21 25 28 31 33 35 36 37 41 47
Line 440:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub prime(n: uint8): (p: uint8) is
Line 471:
end if;
n := n + 1;
end loop;</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>5
Line 537:
=={{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 550:
=={{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 569:
 
"Base 10 Base 2 (sum) Base 3 (sum)" print
l23primes [ 200 < ] lwhile [ 23prime. ] leach</langsyntaxhighlight>
{{out}}
<pre style="height:24em">
Line 637:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Function Digsum(n, b) =
digsum := 0;
while n>0 do
Line 649:
!(p,' ');
nadd := nadd+1;
fi od;</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|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 672:
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 691:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include"isprime.bas"
 
function digsum( byval n as uinteger, b as const uinteger ) as uinteger
Line 705:
for n as uinteger = 1 to 200
if isprime(digsum(n,2)) and isprime(digsum(n,3)) then print n;" ";
next n : print</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>
 
Line 711:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 737:
}
fmt.Printf("\n\n%d such numbers found\n", len(numbers))
}</langsyntaxhighlight>
 
{{out}}
Line 752:
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="gwbasic">10 FOR N = 2 TO 200
20 B = 2
30 GOSUB 220 : GOSUB 110
Line 779:
260 P = P + XN MOD B
270 XN = XN\B
280 GOTO 250</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|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Bifunctor (first)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (isPrime)
Line 822:
 
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</langsyntaxhighlight>
<pre>61 matches in [1..199]
 
Line 834:
 
=={{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|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 853:
 
=={{header|MAD}}==
<langsyntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(P)
Line 881:
 
VECTOR VALUES FMT = $I3*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'> 5
Line 945:
199</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Partition[
Select[
Range@
200, (PrimeQ[Total@IntegerDigits[#, 2]] &&
PrimeQ[Total@IntegerDigits[#, 3]]) &], UpTo[8]] // TableForm</langsyntaxhighlight>
 
{{out}}<pre>
Line 963:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils
 
func isPrime(n: Positive): bool =
Line 989:
stdout.write ($n).align(3), if count mod 16 == 0: '\n' else: ' '
echo()
echo "Found ", count, " numbers."</langsyntaxhighlight>
 
{{out}}
Line 1,000:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,010:
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 1,022:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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>
Line 1,040:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,051:
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="pli">100H:
/* CP/M CALLS */
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
Line 1,099:
 
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>5
Line 1,166:
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Loop.
Line 1,193:
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 1,205:
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.
<langsyntaxhighlight lang="pli">/* FIND NUMBERS WHOSE DIGIT SUM SQUARED AND CUBED IS PRIME */
prime_digit_sums_100H: procedure options (main);
 
Line 1,294:
END;
 
EOF: end prime_digit_sums_100H;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,307:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''Binary and Ternary digit sums both prime'''
 
 
Line 1,396:
if __name__ == '__main__':
main()
</syntaxhighlight>
</lang>
<pre>61 matches in [1..199]
 
Line 1,409:
=={{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,420:
 
=={{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,457:
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,475:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,522:
binList = substr(binList,nl,"")
return binList
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:24em">
Line 1,593:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">1..^200 -> grep {|n| [2,3].all { n.sumdigits(_).is_prime } }</langsyntaxhighlight>
{{out}}
<pre>
Line 1,602:
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.
 
<langsyntaxhighlight 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
Line 1,639:
LET P = P + T - (T/B)*B
LET T = T/B
GOTO 210 </langsyntaxhighlight>
 
=={{header|Wren}}==
Line 1,645:
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
import "/seq" for Lst
Line 1,659:
System.print("Numbers < 200 whose binary and ternary digit sums are prime:")
for (chunk in Lst.chunks(numbers, 14)) Fmt.print("$4d", chunk)
System.print("\nFound %(numbers.count) such numbers.")</langsyntaxhighlight>
 
{{out}}
Line 1,674:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 1,703:
Text(0, " such numbers found below 200.
");
]</langsyntaxhighlight>
 
{{out}}
10,333

edits