Extra primes: Difference between revisions

m
Added Easylang
No edit summary
m (Added Easylang)
 
(6 intermediate revisions by 4 users not shown)
Line 787:
 
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Uses Delphi string to examine to sum and test digits.
 
<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 IsExtraPrime(N: integer): boolean;
{Check if 1) The number is prime}
{2) All the digits in the number is prime}
{3) The sum of all digits is prime}
var S: string;
var I,Sum,D: integer;
begin
Result:=False;
if not IsPrime(N) then exit;
Sum:=0;
S:=IntToStr(N);
for I:=1 to Length(S) do
begin
D:=byte(S[I])-$30;
if not IsPrime(D) then exit;
Sum:=Sum+D;
end;
Result:=IsPrime(Sum);
end;
 
 
procedure ShowExtraPrimes(Memo: TMemo);
{Show all extra-primes less than 10,000}
var I: integer;
var Cnt: integer;
var S: string;
begin
Cnt:=0;
S:='';
for I:=1 to 10000 do
if IsExtraPrime(I) then
begin
Inc(Cnt);
S:=S+Format('%5d',[I]);
if (Cnt mod 9)=0 then S:=S+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 23 223 227 337 353
373 557 577 733 757 773 2333 2357 2377
2557 2753 2777 3253 3257 3323 3527 3727 5233
5237 5273 5323 5527 7237 7253 7523 7723 7727
</pre>
 
 
Line 970 ⟶ 898:
35: 7723
36: 7727</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Uses Delphi string to examine to sum and test digits.
 
<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 IsExtraPrime(N: integer): boolean;
{Check if 1) The number is prime}
{2) All the digits in the number is prime}
{3) The sum of all digits is prime}
var S: string;
var I,Sum,D: integer;
begin
Result:=False;
if not IsPrime(N) then exit;
Sum:=0;
S:=IntToStr(N);
for I:=1 to Length(S) do
begin
D:=byte(S[I])-$30;
if not IsPrime(D) then exit;
Sum:=Sum+D;
end;
Result:=IsPrime(Sum);
end;
 
 
procedure ShowExtraPrimes(Memo: TMemo);
{Show all extra-primes less than 10,000}
var I: integer;
var Cnt: integer;
var S: string;
begin
Cnt:=0;
S:='';
for I:=1 to 10000 do
if IsExtraPrime(I) then
begin
Inc(Cnt);
S:=S+Format('%5d',[I]);
if (Cnt mod 9)=0 then S:=S+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 23 223 227 337 353
373 557 577 733 757 773 2333 2357 2377
2557 2753 2777 3253 3257 3323 3527 3727 5233
5237 5273 5323 5527 7237 7253 7523 7723 7727
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
if num mod 2 = 0 and num > 2
return 0
.
i = 3
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
.
return 1
.
func digprim n .
while n > 0
d = n mod 10
if d < 2 or d = 4 or d = 6 or d >= 8
return 0
.
sum += d
n = n div 10
.
return isprim sum
.
p = 2
while p < 10000
if isprim p = 1 and digprim p = 1
write p & " "
.
p += 1
.
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 23 223 227 337 353 373 557 577 733 757 773 2333 2357 2377 2557 2753 2777 3253 3257 3323 3527 3727 5233 5237 5273 5323 5527 7237 7253 7523 7723 7727
</pre>
 
=={{header|F_Sharp|F#}}==
Line 1,985 ⟶ 2,026:
2557 2753 2777 3253 3257 3323 3527 3727 5233
5237 5273 5323 5527 7237 7253 7523 7723 7727</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
using simple circular buffer for last n solutions.With crossing 10th order of magnitude like in Raku.
<syntaxhighlight lang="pascal">program SpecialPrimes;
// modified smarandache
{$IFDEF FPC}{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$ENDIF}
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils;
const
Digits : array[0..3] of Uint32 = (2,3,5,7);
 
var
//circular buffer
Last64 : array[0..63] of Uint64;
cnt,Limit : NativeUint;
LastIdx: Int32;
procedure OutLast(i:Int32);
var
idx: Int32;
begin
idx := LastIdx-i;
If idx < Low(Last64) then
idx += High(Last64)+1;
For i := i downto 1 do
begin
write(Last64[idx]:12);
inc(idx);
if idx > High(Last64) then
idx := Low(Last64);
end;
writeln;
end;
 
function isSmlPrime64(n:UInt32):boolean;inline;
//n must be >=0 and <=180 = 20 times digit 9, uses 80x86 BIT TEST
begin
EXIT(n in [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,
79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,
157,163,167,173,179])
end;
 
function isPrime(n:UInt64):boolean;
const
deltaWheel : array[0..7] of byte =( 2, 6, 4, 2, 4, 2, 4, 6);
var
p : NativeUint;
WheelIdx : Int32;
begin
if n < 180 then
EXIT(isSmlPrime64(n));
 
result := false;
 
if n mod 2 = 0 then EXIT;
if n mod 3 = 0 then EXIT;
if n mod 5 = 0 then EXIT;
 
p := 1;
WheelIdx := High(deltaWheel);
repeat
inc(p,deltaWheel[WheelIdx]);
if p*p > n then
BREAK;
if n mod p = 0 then
EXIT;
dec(WheelIdx);
IF WheelIdx< Low(deltaWheel) then
wheelIdx := High(deltaWheel);
until false;
result := true;
end;
 
procedure Check(n:NativeUint);
 
Begin
if isPrime(n) then
begin
Last64[LastIdx] := n;
inc(LastIdx);
If LastIdx>High(Last64) then
LastIdx := Low(Last64);
inc(cnt);
IF (n < 10000) then
Begin
write(n:5,',');
if cnt mod 10 = 0 then
writeln;
if cnt = 36 then
writeln;
end
else
IF n > Limit then
Begin
OutLast(7);
Limit *=10;
end;
end;
end;
 
var
i,j,pot10,DgtLimit,n,DgtCnt,v : NativeUint;
dgt,
dgtsum : Int32;
Begin
Limit := 100000;
cnt := 0;
LastIdx := 0;
//Creating the numbers not the best way but all upto 11 digits take 0.05s
//here only 9 digits
i := 0;
pot10 := 1;
DgtLimit := 1;
v := 4;
repeat
repeat
j := i;
DgtCnt := 0;
pot10 := 1;
n := 0;
dgtsum := 0;
repeat
dgt := Digits[j MOD 4];
dgtsum += dgt;
n += pot10*Dgt;
j := j DIV 4;
pot10 *=10;
inc(DgtCnt);
until DgtCnt = DgtLimit;
if isPrime(dgtsum) then Check(n);
inc(i);
until i=v;
//one more digit
v *=4;
i :=0;
inc(DgtLimit);
until DgtLimit= 12;
inc(LastIdx);
OutLast(7);
writeln('count: ',cnt);
end.</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>
2, 3, 5, 7, 23, 223, 227, 337, 353, 373,
557, 577, 733, 757, 773, 2333, 2357, 2377, 2557, 2753,
2777, 3253, 3257, 3323, 3527, 3727, 5233, 5237, 5273, 5323,
5527, 7237, 7253, 7523, 7723, 7727,
75577 75773 77377 77557 77573 77773 222337
772573 773273 773723 775237 775273 777277 2222333
7775737 7775753 7777337 7777537 7777573 7777753 22222223
77755523 77757257 77757523 77773277 77773723 77777327 222222227
777775373 777775553 777775577 777777227 777777577 777777773 2222222377
7777772773 7777773257 7777773277 7777775273 7777777237 7777777327 22222222223
77777757773 77777773537 77777773757 77777775553 77777777533 77777777573 {{77777272733 63 places before last}}
count: 107308
Real time: 46.241 s CPU share: 99.38 %
@home: Real time: 8.615 s maybe much faster div ( Ryzen 5600G 4.4 Ghz vs Xeon 2.3 Ghz)
count < 1E
1E5, E6, E7, E8, E9, E10, E11
89,222,718,2498,9058,32189,107308
</pre>
 
=={{header|Perl}}==
Line 2,373 ⟶ 2,577:
</pre>
 
=={{header|RPL}}==
≪ DUP →STR "014689" → n nstring baddigits
≪ 1 CF 0
1 nstring SIZE '''FOR''' j
nstring j DUP SUB
'''IF''' baddigits OVER POS '''THEN''' 1 SF '''END'''
STR→ + '''NEXT'''
'''IF''' 1 FC?
'''THEN IF''' <span style="color:blue">PRIM?</span> '''THEN''' n <span style="color:blue">PRIM?</span> '''ELSE''' 0 '''END'''
'''ELSE''' DROP 0 '''END'''
≫ ≫ ‘<span style="color:blue">XTRA?</span>’ STO
≪ { } 1 10000 '''FOR''' n '''IF''' n <span style="color:blue">XTRA?</span> '''THEN''' n + '''END NEXT''' ≫ EVAL
<code>PRIM?</code> is defined at [[Primality by trial division#RPL|Primality by trial division]] or can be replaced by <code>ISPRIME?</code> when using a RPL 2003+ version.
{{out}}
<pre>
1: { 2 3 5 7 23 223 227 337 353 373 557 577 733 757 773 2333 2357 2377 2557 2753 2777 3253 3257 3323 3527 3727 5233 5237 5273 5323 5527 7237 7253 7523 7723 7727 }
</pre>
=={{header|Ruby}}==
{{trans|Java}}
Line 2,763 ⟶ 2,985:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
var digits = [2, 3, 5, 7] // the only digits which are primes
2,022

edits