Honaker primes: Difference between revisions

no edit summary
(Added C)
No edit summary
Line 355:
Ten thousandth: (286069, 4043749)
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,SysUtils,StdCtrls}}
Modular version of the algorithm, breaks the problem down into simple pieces, which is a standard technique for solving problems. One of the advantages is that the modules can be used in different combination to solve different aspects of the problem. In this case, it is to find the first 50 and then 10,000th Honaker.
 
<syntaxhighlight lang="Delphi">
function IsPrime(N: integer): boolean;
{Optimised prime test - about 40% faster than the naive approach}
var I,Stop: integer;
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));
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 GetNextPrime(var Start: integer): integer;
{Get the next prime number after Start}
{Start is passed by "reference," so the
{original variable is incremented}
begin
repeat Inc(Start)
until IsPrime(Start);
Result:=Start;
end;
 
 
function SumDigits(N: integer): integer;
{Sum the integers in a number}
var T: integer;
begin
Result:=0;
repeat
begin
T:=N mod 10;
N:=N div 10;
Result:=Result+T;
end
until N<1;
end;
 
 
function IsHonaker(I,N: integer): boolean;
{A Honaker prime is one where the sums of digits}
{of the prime and its position are equal}
begin
Result:=SumDigits(I) = SumDigits(N);
end;
 
procedure ShowHonakerPrimes(Memo: TMemo);
{Test Honaker primes}
var I, N,Cnt: integer;
var S: string;
begin
N:=0; Cnt:=0; S:='';
{Test all numbers to see if they are prime}
for I:=1 to High(integer) do
begin
N:=GetNextPrime(N);
{Test the number if it Honaker}
if IsHonaker(I,N) then
begin
{Display if Honaker}
Inc(Cnt);
S:=S+Format('(%2d%5d%5d) ',[Cnt,I,N]);
if (Cnt mod 3)=0 then S:=S+#$0D#$0A;
if Cnt>=50 then break;
end;
end;
Memo.Lines.Add('First 50 Honaker Primes');
Memo.Lines.Add(S);
Memo.Lines.Add('');
 
{Find the 10,000th Honaker}
Memo.Lines.Add('The 10,000th Honaker Primes');
N:=0; Cnt:=0;
for I:=1 to High(integer) do
begin
N:=GetNextPrime(N);
if IsHonaker(I,N) then
begin
Inc(Cnt);
if Cnt=10000 then
begin
Memo.Lines.Add(Format('(%5d %5d %5d) ',[Cnt,I,N]));
break;
end;
end;
end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
First 50 Honaker Primes
( 1 32 131) ( 2 56 263) ( 3 88 457)
( 4 175 1039) ( 5 176 1049) ( 6 182 1091)
( 7 212 1301) ( 8 218 1361) ( 9 227 1433)
(10 248 1571) (11 293 1913) (12 295 1933)
(13 323 2141) (14 331 2221) (15 338 2273)
(16 362 2441) (17 377 2591) (18 386 2663)
(19 394 2707) (20 397 2719) (21 398 2729)
(22 409 2803) (23 439 3067) (24 446 3137)
(25 457 3229) (26 481 3433) (27 499 3559)
(28 508 3631) (29 563 4091) (30 571 4153)
(31 595 4357) (32 599 4397) (33 635 4703)
(34 637 4723) (35 655 4903) (36 671 5009)
(37 728 5507) (38 751 5701) (39 752 5711)
(40 755 5741) (41 761 5801) (42 767 5843)
(43 779 5927) (44 820 6301) (45 821 6311)
(46 826 6343) (47 827 6353) (48 847 6553)
(49 848 6563) (50 857 6653)
 
The 10,000th Honaker Primes
(10000 286069 4043749)
</pre>
 
 
=={{header|F_Sharp|F#}}==
465

edits