Concatenate two primes is also prime: Difference between revisions
Content added Content deleted
(Added C) |
No edit summary |
||
Line 545: | Line 545: | ||
Found 128 such concatenated primes. |
Found 128 such concatenated primes. |
||
</pre> |
</pre> |
||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
<syntaxhighlight lang="Delphi"> |
|||
function Compare(P1,P2: pointer): integer; |
|||
{Compare for quick sort} |
|||
begin |
|||
Result:=Integer(P1)-Integer(P2); |
|||
end; |
|||
procedure ConcatonatePrimes(Memo: TMemo); |
|||
{Show concatonated pairs of primes that are also prime} |
|||
var List: TList; |
|||
var I,P1,P2,ConCat: integer; |
|||
var Sieve: TPrimeSieve; |
|||
const Max =100; |
|||
var S: string; |
|||
function ConcatNums(I1,I2: integer): integer; |
|||
begin |
|||
Result:=StrToInt(IntToStr(I1)+IntToStr(I2)); |
|||
end; |
|||
begin |
|||
{Create sieve to for fast prime generation} |
|||
Sieve:=TPrimeSieve.Create; |
|||
try |
|||
List:=TList.Create; |
|||
try |
|||
{Sieve first 1,000 primes} |
|||
Sieve.Intialize(1000); |
|||
{Generate all combinations of primes} |
|||
{ P1 and P2, from 2 to 100} |
|||
P1:=2; |
|||
while P1<Max do |
|||
begin |
|||
P2:=2; |
|||
while P2<Max do |
|||
begin |
|||
{Concatonates the two primes} |
|||
ConCat:=ConcatNums(P1,P2); |
|||
{Test if it is prime and only store unique primes} |
|||
if IsPrime(ConCat) then |
|||
if List.IndexOf(Pointer(ConCat))<0 then |
|||
List.Add(Pointer(ConCat)); |
|||
P2:=Sieve.NextPrime(P2); |
|||
end; |
|||
P1:=Sieve.NextPrime(P1); |
|||
end; |
|||
{Sort list in numerical order} |
|||
List.Sort(Compare); |
|||
{Display the result} |
|||
Memo.Lines.Add('Concatonated Primes Found: '+IntToStr(List.Count)); |
|||
for I:=0 to List.Count-1 do |
|||
begin |
|||
S:=S+Format('%5d',[integer(List[I])]); |
|||
if (I mod 10)=9 then S:=S+CRLF; |
|||
end; |
|||
Memo.Lines.Add(S); |
|||
finally List.Free; end; |
|||
finally Sieve.Free; end; |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Concatonated Primes Found: 128 |
|||
23 37 53 73 113 137 173 193 197 211 |
|||
223 229 233 241 271 283 293 311 313 317 |
|||
331 337 347 353 359 367 373 379 383 389 |
|||
397 433 523 541 547 571 593 613 617 673 |
|||
677 719 733 743 761 773 797 977 1117 1123 |
|||
1129 1153 1171 1319 1361 1367 1373 1723 1741 1747 |
|||
1753 1759 1783 1789 1913 1931 1973 1979 1997 2311 |
|||
2341 2347 2371 2383 2389 2917 2953 2971 3119 3137 |
|||
3167 3719 3761 3767 3779 3797 4111 4129 4153 4159 |
|||
4337 4373 4397 4723 4729 4759 4783 4789 5323 5347 |
|||
5923 5953 6113 6131 6143 6173 6197 6719 6737 6761 |
|||
6779 7129 7159 7331 7919 7937 8311 8317 8329 8353 |
|||
8389 8923 8929 8941 8971 9719 9743 9767 |
|||
Elapsed Time: 3.990 ms. |
|||
</pre> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |