Circular primes: Difference between revisions
Content deleted Content added
m →{{header|AppleScript}}: Very minor edit. |
No edit summary |
||
Line 839: | Line 839: | ||
<pre>First 19 circular primes: |
<pre>First 19 circular primes: |
||
2, 3, 5, 7, 11, 13, 17, 37, 79, 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939, 199933</pre> |
2, 3, 5, 7, 11, 13, 17, 37, 79, 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939, 199933</pre> |
||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
Again note how the code is broken up into easy to distinguish subroutines that can be reused, as opposed to mashing everything together into a mass of code that is difficult to understand, debug or enhance. |
|||
<syntaxhighlight lang="Delphi"> |
|||
procedure ShowCircularPrimes(Memo: TMemo); |
|||
{Show list of the first 19, cicular primes} |
|||
var I,Cnt: integer; |
|||
var S: string; |
|||
procedure RotateStr(var S: string); |
|||
{Rotate characters in string} |
|||
var I: integer; |
|||
var C: char; |
|||
begin |
|||
C:=S[Length(S)]; |
|||
for I:=Length(S)-1 downto 1 do S[I+1]:=S[I]; |
|||
S[1]:=C; |
|||
end; |
|||
function IsCircularPrime(N: integer): boolean; |
|||
{Test if all rotations of number are prime and} |
|||
{A rotation of the number hasn't been used before} |
|||
var I,P: integer; |
|||
var NS: string; |
|||
begin |
|||
Result:=False; |
|||
NS:=IntToStr(N); |
|||
for I:=1 to Length(NS)-1 do |
|||
begin |
|||
{Rotate string and convert to integer} |
|||
RotateStr(NS); |
|||
P:=StrToInt(NS); |
|||
{Exit if number is not prime or} |
|||
{Is prime, is less than N i.e. we've seen it before} |
|||
if not IsPrime(P) or (P<N) then exit; |
|||
end; |
|||
Result:=True; |
|||
end; |
|||
begin |
|||
S:=''; |
|||
Cnt:=0; |
|||
{Look for circular primes and display 1st 19} |
|||
for I:=0 to High(I) do |
|||
if IsPrime(I) then |
|||
if IsCircularPrime(I) then |
|||
begin |
|||
Inc(Cnt); |
|||
S:=S+Format('%7D',[I]); |
|||
if Cnt>=19 then break; |
|||
If (Cnt mod 5)=0 then S:=S+CRLF; |
|||
end; |
|||
Memo.Lines.Add(S); |
|||
Memo.Lines.Add('Count = '+IntToStr(Cnt)); |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
2 3 5 7 11 |
|||
13 17 37 79 113 |
|||
197 199 337 1193 3779 |
|||
11939 19937 193939 199933 |
|||
Count = 19 |
|||
Elapsed Time: 21.234 ms. |
|||
</pre> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
This task uses [http://www.rosettacode.org/wiki/Repunit_primes#F.23 rUnitP] and [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)] |
This task uses [http://www.rosettacode.org/wiki/Repunit_primes#F.23 rUnitP] and [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)] |