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 17:03, 7 January 2024
, 4 months ago→{{header|Wren}}: More minor changes.
(→{{header|Ruby}}: Add Ruby) |
m (→{{header|Wren}}: More minor changes.) |
||
(6 intermediate revisions by 5 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}}
'''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}}==
Line 1,548 ⟶ 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,735 ⟶ 1,842:
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
require 'prime'
p (1..200).select{|n| [2, 3].all?{|base| n.digits(base).sum.prime?} }</syntaxhighlight>
Line 1,742 ⟶ 1,850:
[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>
Line 1,752 ⟶ 1,861:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Int
var numbers = []
Line 1,766 ⟶ 1,873:
}
System.print("Numbers < 200 whose binary and ternary digit sums are prime:")
Fmt.tprint("$4d", numbers, 14)
System.print("\nFound %(numbers.count) such numbers.")</syntaxhighlight>
|