Category:PrimTrial: Difference between revisions

m
added PrimeRange
m (moved the unit to this place)
m (added PrimeRange)
Line 3:
Maybe NativeUint must be typed in older versions to LongWord aka cardinal
 
<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}
{$MODE DELPHI}
Line 14 ⟶ 13:
 
interface
type
ptPrimeList = array of NativeUint;
 
procedure InitPrime;
 
function actPrime :NativeUint;
function isPrime(pr: NativeUint):boolean;
function isAlmostPrime(n: NativeUint;cnt: NativeUint): boolean;
function isSemiprime(n: NativeUint): boolean;
function SmallFactor(pr: NativeUint):NativeUint;
 
//next prime
function NextPrime: NativeUint;
Line 27 ⟶ 30:
//next prime greater equal limit
function PrimeGELimit(Limit:NativeUint):NativeUint;
function PrimeRange(LowLmt,UpLmt:NativeUint): ptPrimeList;
implementation
 
uses
sysutils;
Line 34 ⟶ 38:
cntsmallPrimes = 6;
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);
wheelCircumfence = 2*3*5*7*11*13;
Line 41 ⟶ 45:
WheelIdx : nativeUint;
p,pw : nativeUint;
 
procedure InitPrime;
//initialies wheel and prime to startposition
Line 49 ⟶ 53:
WheelIdx := 0;
end;
 
function actPrime :NativeUint;inline;
Begin
result := p;
end;
 
procedure InitWheel;
//search for numbers that are no multiples of smallprimes
Line 80 ⟶ 84:
until d >= wheelSize;
end;
 
function biggerFactor(p: NativeUint):NativeUint;
//trial division by wheel numbers
Line 105 ⟶ 109:
result := p;
end;
 
function SmallFactor(pr: NativeUint):NativeUint;
//checking numbers omitted by biggerFactor
Line 127 ⟶ 131:
result := biggerFactor(pr);
end;
 
function isPrime(pr: NativeUint):boolean;
Begin
Line 135 ⟶ 139:
isPrime := false;
end;
 
function isAlmostPrime(n: NativeUint;cnt: NativeUint): boolean;
var
Line 148 ⟶ 152:
isAlmostPrime := (n = 1) AND (c = cnt);
end;
 
function isSemiprime(n: NativeUint): boolean;
begin
result := isAlmostPrime(n,2);
end;
 
function NextPosPrim: NativeUint;inline;
var
Line 164 ⟶ 167:
pw := result;
end;
 
function NextPrime: NativeUint;
Begin
Line 183 ⟶ 186:
end;
end;
 
function PrimeGELimit(Limit:NativeUint):NativeUint;
//prime greater or equal limit
Line 209 ⟶ 212:
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
Begin
InitWheel;
InitPrime;
end.</lang>
</lang>
Anonymous user