Numbers whose binary and ternary digit sums are prime: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 667: | Line 667: | ||
193 |
193 |
||
199</pre> |
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#}}== |
=={{header|F_Sharp|F#}}== |