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

m
→‎{{header|Wren}}: More minor changes.
m (→‎{{header|Wren}}: More minor changes.)
(9 intermediate revisions by 6 users not shown)
Line 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#}}==
Line 817 ⟶ 905:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Numbers_which_binary_and_ternary_digit_sum_are_prime}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Numbers which binary and ternary digit sum are prime 01.png]]
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.
 
[[File:Fōrmulæ - Numbers which binary and ternary digit sum are prime 02.png]]
 
=={{header|Go}}==
Line 921 ⟶ 1,011:
{{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}}==
Line 1,490 ⟶ 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}}==
Line 1,674 ⟶ 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>
 
Line 1,686 ⟶ 1,861:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Int
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascript">import "./mathfmt" for IntFmt
import "/fmt" for Fmt
import "/seq" for Lst
 
var numbers = []
Line 1,700 ⟶ 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.")</syntaxhighlight>
 
9,479

edits