Category:PrimTrial: Difference between revisions

Content added Content deleted
m (moved the unit to this place)
m (added PrimeRange)
Line 3: Line 3:
Maybe NativeUint must be typed in older versions to LongWord aka cardinal
Maybe NativeUint must be typed in older versions to LongWord aka cardinal


<lang pascal>
<lang pascal>unit primTrial;
// NativeUInt: LongWord 32-Bit-OS/ Uint64 64-Bit-OS
unit primTrial;
//NativeUInt: LongWord 32-Bit-OS/ Uint64 64-Bit-OS
{$IFDEF FPC}
{$IFDEF FPC}
{$MODE DELPHI}
{$MODE DELPHI}
Line 14: Line 13:


interface
interface
type
ptPrimeList = array of NativeUint;

procedure InitPrime;
procedure InitPrime;

function actPrime :NativeUint;
function actPrime :NativeUint;
function isPrime(pr: NativeUint):boolean;
function isPrime(pr: NativeUint):boolean;
function isAlmostPrime(n: NativeUint;cnt: NativeUint): boolean;
function isAlmostPrime(n: NativeUint;cnt: NativeUint): boolean;
function isSemiprime(n: NativeUint): boolean;
function SmallFactor(pr: NativeUint):NativeUint;
function SmallFactor(pr: NativeUint):NativeUint;

//next prime
//next prime
function NextPrime: NativeUint;
function NextPrime: NativeUint;
Line 27: Line 30:
//next prime greater equal limit
//next prime greater equal limit
function PrimeGELimit(Limit:NativeUint):NativeUint;
function PrimeGELimit(Limit:NativeUint):NativeUint;
function PrimeRange(LowLmt,UpLmt:NativeUint): ptPrimeList;
implementation
implementation

uses
uses
sysutils;
sysutils;
Line 34: Line 38:
cntsmallPrimes = 6;
cntsmallPrimes = 6;
smallPrimes : array[0..cntsmallPrimes-1] of NativeUint = (2,3,5,7,11,13);
smallPrimes : array[0..cntsmallPrimes-1] of NativeUint = (2,3,5,7,11,13);

wheelSize = (2-1)*(3-1)*(5-1)*(7-1)*(11-1)*(13-1);
wheelSize = (2-1)*(3-1)*(5-1)*(7-1)*(11-1)*(13-1);
wheelCircumfence = 2*3*5*7*11*13;
wheelCircumfence = 2*3*5*7*11*13;
Line 41: Line 45:
WheelIdx : nativeUint;
WheelIdx : nativeUint;
p,pw : nativeUint;
p,pw : nativeUint;

procedure InitPrime;
procedure InitPrime;
//initialies wheel and prime to startposition
//initialies wheel and prime to startposition
Line 49: Line 53:
WheelIdx := 0;
WheelIdx := 0;
end;
end;

function actPrime :NativeUint;inline;
function actPrime :NativeUint;inline;
Begin
Begin
result := p;
result := p;
end;
end;

procedure InitWheel;
procedure InitWheel;
//search for numbers that are no multiples of smallprimes
//search for numbers that are no multiples of smallprimes
Line 80: Line 84:
until d >= wheelSize;
until d >= wheelSize;
end;
end;

function biggerFactor(p: NativeUint):NativeUint;
function biggerFactor(p: NativeUint):NativeUint;
//trial division by wheel numbers
//trial division by wheel numbers
Line 105: Line 109:
result := p;
result := p;
end;
end;

function SmallFactor(pr: NativeUint):NativeUint;
function SmallFactor(pr: NativeUint):NativeUint;
//checking numbers omitted by biggerFactor
//checking numbers omitted by biggerFactor
Line 127: Line 131:
result := biggerFactor(pr);
result := biggerFactor(pr);
end;
end;

function isPrime(pr: NativeUint):boolean;
function isPrime(pr: NativeUint):boolean;
Begin
Begin
Line 135: Line 139:
isPrime := false;
isPrime := false;
end;
end;

function isAlmostPrime(n: NativeUint;cnt: NativeUint): boolean;
function isAlmostPrime(n: NativeUint;cnt: NativeUint): boolean;
var
var
Line 148: Line 152:
isAlmostPrime := (n = 1) AND (c = cnt);
isAlmostPrime := (n = 1) AND (c = cnt);
end;
end;

function isSemiprime(n: NativeUint): boolean;
function isSemiprime(n: NativeUint): boolean;
begin
begin
result := isAlmostPrime(n,2);
result := isAlmostPrime(n,2);
end;
end;

function NextPosPrim: NativeUint;inline;
function NextPosPrim: NativeUint;inline;
var
var
Line 164: Line 167:
pw := result;
pw := result;
end;
end;

function NextPrime: NativeUint;
function NextPrime: NativeUint;
Begin
Begin
Line 183: Line 186:
end;
end;
end;
end;

function PrimeGELimit(Limit:NativeUint):NativeUint;
function PrimeGELimit(Limit:NativeUint):NativeUint;
//prime greater or equal limit
//prime greater or equal limit
Line 209: Line 212:
end;
end;
end;
end;

function PrimeRange(LowLmt,UpLmt:NativeUint): ptPrimeList;
var
i,newP : NativeUint;
Begin
IF LowLmt>UpLmt then
Begin
setlength(result,0);
EXIT;
end;
i := 0;
setlength(result,100);
newP := PrimeGELimit(LowLmt);

while newP<= UpLmt do
Begin
result[i]:= newP;
inc(i);
IF i>High(result) then
setlength(result,i*2);
newP := NextPrime;
end;
setlength(result,i);
end;

//initialization
//initialization
Begin
Begin
InitWheel;
InitWheel;
InitPrime;
InitPrime;
end.</lang>
end.
</lang>