Numbers whose binary and ternary digit sums are prime: Difference between revisions
Numbers whose binary and ternary digit sums are prime (view source)
Revision as of 00:05, 28 August 2022
, 1 year agosyntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 8:
{{trans|Nim}}
<
I a == 2
R 1B
Line 32:
print(‘#3’.format(n), end' I count % 16 == 0 {"\n"} E ‘ ’)
print()
print(‘Found ’count‘ numbers.’)</
{{out}}
Line 45:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<
BYTE Func IsPrime(INT i BYTE base BYTE ARRAY primes)
Line 74:
OD
PrintF("%E%EThere are %I numbers",count)
RETURN</
{{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}}
<
# 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</
{{out}}
<pre>
Line 131:
=={{header|ALGOL-M}}==
<
integer function mod(a,b);
integer a,b;
Line 167:
end;
end;
end</
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
Line 178:
=={{header|ALGOL W}}==
<
% 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.</
{{out}}
<pre>
Line 229:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
{{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}}==
<
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]</
{{out}}
Line 251:
199</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f NUMBERS_WHICH_BINARY_AND_TERNARY_DIGIT_SUM_ARE_PRIME.AWK
# converted from C
Line 282:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 299:
is to hardcode which ones are prime.
<
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</
{{out}}
<pre> 5 6 7 10 11
Line 324:
=={{header|BCPL}}==
<
let digitsum(n, base) =
Line 348:
$)
wrch('*N')
$)</
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
Line 359:
=={{header|C}}==
<
#include <stdint.h>
Line 392:
printf("\n");
return 0;
}</
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
Line 403:
=={{header|CLU}}==
<
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</
{{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}}==
<
sub prime(n: uint8): (p: uint8) is
Line 471:
end if;
n := n + 1;
end loop;</
{{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#)]
<
// 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>
Line 550:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
lists.lazy math math.parser math.primes sequences ;
Line 569:
"Base 10 Base 2 (sum) Base 3 (sum)" print
l23primes [ 200 < ] lwhile [ 23prime. ] leach</
{{out}}
<pre style="height:24em">
Line 637:
=={{header|Fermat}}==
<
digsum := 0;
while n>0 do
Line 649:
!(p,' ');
nadd := nadd+1;
fi od;</
{{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}}==
<
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</
{{out}}
<pre>= 5= 6= 7= 10= 11= 12= 13= 17= 18= 19
Line 691:
=={{header|FreeBASIC}}==
<
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</
{{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}}
<
import (
Line 737:
}
fmt.Printf("\n\n%d such numbers found\n", len(numbers))
}</
{{out}}
Line 752:
=={{header|GW-BASIC}}==
<
20 B = 2
30 GOSUB 220 : GOSUB 110
Line 779:
260 P = P + XN MOD B
270 XN = XN\B
280 GOTO 250</
{{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}}==
<
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 <>)</
<pre>61 matches in [1..199]
Line 834:
=={{header|J}}==
<
{{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}}==
<
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)))
</
<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}}==
<
INTERNAL FUNCTION(P)
Line 881:
VECTOR VALUES FMT = $I3*$
END OF PROGRAM </
{{out}}
<pre style='height:50ex;'> 5
Line 945:
199</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
Select[
Range@
200, (PrimeQ[Total@IntegerDigits[#, 2]] &&
PrimeQ[Total@IntegerDigits[#, 3]]) &], UpTo[8]] // TableForm</
{{out}}<pre>
Line 963:
=={{header|Nim}}==
<
func isPrime(n: Positive): bool =
Line 989:
stdout.write ($n).align(3), if count mod 16 == 0: '\n' else: ' '
echo()
echo "Found ", count, " numbers."</
{{out}}
Line 1,000:
=={{header|Perl}}==
{{libheader|ntheory}}
<
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;</
{{out}}
<pre>61 matching numbers:
Line 1,022:
=={{header|Phix}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 1,051:
=={{header|PL/M}}==
<
/* CP/M CALLS */
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
Line 1,099:
CALL EXIT;
EOF</
{{out}}
<pre style='height:50ex;'>5
Line 1,166:
=={{header|Plain English}}==
<
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.</
{{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.
<
prime_digit_sums_100H: procedure options (main);
Line 1,294:
END;
EOF: end prime_digit_sums_100H;</
{{out}}
<pre>
Line 1,307:
=={{header|Python}}==
<
Line 1,396:
if __name__ == '__main__':
main()
</syntaxhighlight>
<pre>61 matches in [1..199]
Line 1,409:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
{{out}}
<pre> 5 6 7 10 11 12 13 17 18 19
Line 1,420:
=={{header|REXX}}==
<
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</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,475:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,522:
binList = substr(binList,nl,"")
return binList
</syntaxhighlight>
{{out}}
<pre style="height:24em">
Line 1,593:
=={{header|Sidef}}==
<
{{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.
<
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 </
=={{header|Wren}}==
Line 1,645:
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<
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.")</
{{out}}
Line 1,674:
=={{header|XPL0}}==
<
int N, I;
[if N <= 1 then return false;
Line 1,703:
Text(0, " such numbers found below 200.
");
]</
{{out}}
|