Multi-base primes: Difference between revisions

m
→‎{{header|Pascal}}: Forget to inc number in IncInBaseDigits and to reset all digits to 0 in CnvtoBASE
m (→‎{{header|Pascal}}: Forget to inc number in IncInBaseDigits and to reset all digits to 0 in CnvtoBASE)
Line 644:
{$MODE DELPHI}
{$OPTIMIZATION ON,ALL}
// {$R+,O+}
{$ELSE}
{$APPTYPE CONSOLE}
Line 653 ⟶ 652:
CharOfBase= '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
MINBASE = 2;
MAXBASE = 62;//6236;//3662;
MAXDIGITCOUNT = 5;//6;
type
tdigits = packed record
dgtDgts : array [0..13] of byte;
dgtMaxIdx,
Line 665 ⟶ 664:
var
BoolPrimes: array of boolean;
 
function Out_String(n:Uint64;var s: AnsiString):Uint32;forward;
 
procedure Out_Digits(const dgts:tDigits);
var
s : ANsistring;
i : Int32;
begin
s := '';
with dgts do
Begin
i := dgtMaxIdx;
while i >= 0 do
begin
s := s+CharOfBase[dgtDgts[i]];
dec(i);
end;
end;
write(#13,s);
end;
 
function BuildWheel(primeLimit:Int64):NativeUint;
Line 772 ⟶ 751:
end;
 
procedure CnvtoMAXBASECnvtoBASE(n:Uint64;var dgt:tDigits;n:Uint64;base:NativeUint);
var
q,r: Uint64;
Line 780 ⟶ 759:
with dgt do
Begin
fillchar(dgtDgts,SizeOf(dgtDgts),#0);
dgtNum:= n;
repeat
r := n;
q := n div MAXBASEbase;
r -= q*MAXBASEbase;
n := q;
dgtDgts[i] := r;
Line 803 ⟶ 783:
end;
 
function CnvtoBaseCnvDgtAsInBase(const dgt:tDigits;base:NativeUint):Uint64;
var
tmpDgt,i: NativeInt;
Line 812 ⟶ 792:
i:= dgtMaxIdx;
repeat
//result := base*result + dgtDgts[i];
//this is th reason for speed up.Hide the waiting for reading of dgtDgts[i]
tmpDgt := dgtDgts[i];
result *= base;
Line 822 ⟶ 800:
end;
 
procedure IncMAXBASEDigitsIncInBaseDigits(var dgt:tDigits;base:NativeInt);
var
i,q,tmp :NativeInt;
Line 828 ⟶ 806:
with dgt do
Begin
tmp := dgtMaxIdx;
i := 0;
repeat
q := dgtDgts[i]+1;
q -= (-ORD(q >=MAXBASEbase) AND MAXBASEbase);
dgtDgts[i] := q;
inc(i);
Line 842 ⟶ 820:
dgtMaxIdx := i;
end;
 
i := tmp;
repeat
Line 850 ⟶ 827:
dec(i);
until i <0;
inc(dgtNum);
dgtMaxDgtVal := q;
end;
Line 860 ⟶ 838:
begin
result := 0;
IncMAXBASEDigitsIncInBaseDigits(Digits,MAXBASE);
Basebase := Digits.dgtMaxDgtVal+1;
//divisible by every base
IF Digits.dgtDgts[0] = 0 then
EXIT;
IF base < MINBASE then base := MINBASE;
// if (MAXBASE - Base) <= (max-result) then BREAK;
max := (max+Base-MAXBASE);
Line 871 ⟶ 850:
for base := base TO MAXBASE do
begin
pr := CnvtoBaseCnvDgtAsInBase(Digits,base);
inc(result,Ord(boolprimes[pr]));
//no chance to reach max then exit
Line 917 ⟶ 896:
var
dgt:tDigits;
sl : string[815];
base,i: Int32;
Begin
result := 0;
CnvtoMAXBASECnvtoBASE(dgt,n,dgtMaxBase);
sl := '';
with dgt do
begin
base:= dgtMaxDgtVal+1;
IF base < MINBASE then
base := MINBASE;
i := dgtMaxIdx;
while (i>=0)do
Line 935 ⟶ 916:
end;
For base := base to MAXBASE do
if boolprimes[CnvtoBaseCnvDgtAsInBase(dgt,base)] then
begin
inc(result);
Line 966 ⟶ 947:
T0 : Int64;
i : NativeInt;
lmt,minLmt : UInt32UInt64;
 
begin
T0 := GetTickCount64;
Line 977 ⟶ 957:
Sieve(lmt);
writeln('Prime sieving ',(GetTickCount64-T0)/1000:6:3,' s');
 
CnvtoMAXBASE(0,dgt);
T0 := GetTickCount64;
CnvtoBASE(dgt,0,MAXBASE);
i := 1;
minLmt := 1;
Anonymous user